Daniel Morgan
Daniel Morgan

Reputation: 772

if condition in jsp does not work

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.

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";
        }

Upvotes: 1

Views: 1349

Answers (2)

arvin_codeHunk
arvin_codeHunk

Reputation: 2390

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()
  {
     ...............
  }

}

Upvotes: 1

subodh
subodh

Reputation: 6158

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
}

Upvotes: 1

Related Questions