1

I am using the following if condition but it does not work.

When the jsp page is loaded the output will be shown without checking the condition.

  • The code is expect to receive the values of a form put them in obj property and send them to Xclass when the result "o" was received it should should the message.

my.jsp

    <jsp:useBean id="obj" class="com.User"/>

    <jsp:setProperty property="*" name="obj"/>

      <%
         String myoutput = myclass.Xclass(obj);
         out.print(myoutput);
         if(myclass.Xclass(obj).equals("output"))
            {
               out.print("message goes here");
            }
      %>

myclass.class

       public String Xclass(User obj){
           return "output";
        }
Daniel Morgan
  • 762
  • 5
  • 15
  • 42

2 Answers2

1

In version2 you have missed the semicolon ;

 String output = myclass.Xclass(obj);

try to print output values.Try below code

if(output.trim().equalsIgnoreCase(¨o¨))
{
// your message goes here
}
subodh
  • 6,004
  • 12
  • 48
  • 69
1

Why are you putting logic on your presentation layer, Its not a good practice to have your BL on view. I guess you are using struts2 frameworks. Then You should use s:if

Update Section :

It does not matter how long is your form, the struts2 value stack holds any amount of your data along with your action and render your request data to your class, try to use DTO's or separate beans and POJO classes for your respective form-elements.

Because This is how Struts2 are designed to work and doing this only you can achieve MVC pattern by separating your view from your Business Logic.

Your JSP-Page

 <s:form action="yourAction">

                <s:textfield name="name" label="Name"/>
                   .....
                <s:submit ></s:submit>
    </s:form>

In Your Action CLass

@Action
  public class XYZ{
    private form-elements-name;

    getters & setters for form-elements-name

    ..............

    your Business-Logic

   public String YourLogic()
  {
     ...............
  }

}

arvin_codeHunk
  • 2,250
  • 8
  • 31
  • 47
  • Yes I am using struts2 whats your suggestion ? I need to send the values of the form as an object to the controller. – Daniel Morgan Feb 12 '13 at 06:20
  • why dont you do these operations on server-side ,i mean struts2-framework is action-oriented frameworks, check conditions on your server-side and t based on condition shows the respective view. This is how mvc works. – arvin_codeHunk Feb 12 '13 at 06:24
  • I am doing this because the form that I have is a long form ( more than 20 fields) so in this way its easier to send an object of all fields to controller rather than sending them separately. – Daniel Morgan Feb 12 '13 at 06:25
  • 2
    @DanielMorgan All fields will be sent separately. It's an HTTP request, which sends only strings, and knows nothing of objects. If your objection is the number of action properties, use ModelDriven, or expose a single POJO and reference its fields in the form. – Dave Newton Feb 12 '13 at 12:36