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
  • 782
  • 5
  • 15
  • 43
  • @theunlucky, :D I was adding the code please uncheck the negative vote thanks – Daniel Morgan Feb 12 '13 at 06:06
  • Please ask question properly. Provide the code related to the question. –  Feb 12 '13 at 06:06
  • @NikhilAgrawal, sorry question is updated – Daniel Morgan Feb 12 '13 at 06:07
  • myclass.Xclass(obj) ,what are you trying to get here exactly and is this "myclass.Xclass(obj)" returning anything, check first – arvin_codeHunk Feb 12 '13 at 06:12
  • @arvin_codeHunk, it does, the question is updated – Daniel Morgan Feb 12 '13 at 06:15
  • @DanielMorgan : Is code giving any runtime exceptions ? – ATR Feb 12 '13 at 06:19
  • no it does not, you know the aim is to send the values of form as an object. – Daniel Morgan Feb 12 '13 at 06:21
  • "output will be shown without checking the condition" - what does this mean? Means it that the string "message goes here" is never displayed? – Uooo Feb 12 '13 at 06:27
  • @DanielMorgan: Can you tell us how are you creating the myclass object? – me_digvijay Feb 12 '13 at 06:45
  • We can't help, because we don't know anything about the object under test. `if` statements themselves work, however. That said, this is terrible work to be doing in the JSP. Right now it looks like you're just throwing random code at various problems instead of understanding the environment you're working in. I'd take a step back before proceeding much further. – Dave Newton Feb 12 '13 at 12:40
  • @DaveNewton, lets discuss it in http://stackoverflow.com/questions/14843807/how-to-send-values-of-a-form-from-jsp-to-java – Daniel Morgan Feb 12 '13 at 23:29

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,136
  • 12
  • 51
  • 73
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,328
  • 8
  • 32
  • 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