1

I have a form which has 30 different fields. As passing them all to the controller need to have an attribute for each along with a pair of getter, setters.

I am going to make the form fields as an object and send the object to the controller.

I am using the following code *but some people suggest its a bad practice to call a java method from jsp and use JSTL instead, but do not know how to implement it using JSTL. Is there any other method to do it?*

My JSP

 <s:form>
 code to implement form goes here
 </s:form> 

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

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

      <%
         String myoutput = myController.Xclass(obj);
         out.print(myoutput);
         if(myController.Xclass(obj).equals("output"))
            {
               out.print("The form is successfully submitted.");
            }
      %>

The controller

  public String Xclass(User obj){
           return "output";
        }

To clarify my class diagram is a following:

User Class {
 all the attributes and getters setters
}

myController class extends User {

    public String XClass(User obj){
       ... work on the inputes ...
      return "output";
    }
}
Daniel Morgan
  • 782
  • 5
  • 15
  • 43
  • 1
    Why do you have to do that logic inside the JSP? Why can't you do it inside the controller? – Daniel Kaplan Feb 12 '13 at 23:32
  • @tieTYT, as I explained I have 30 different fields, if I do not send them as an object I have to have all the attributes in my controller, but in this case I can extend a class which has all the attributes. – Daniel Morgan Feb 12 '13 at 23:34
  • 2
    Have a look at : http://stackoverflow.com/questions/3177733/how-to-avoid-java-code-in-jsp-files – Bdloul Feb 12 '13 at 23:35
  • @Bdloul, I know JSTL is better but not sure how to implement it, I mean send the inputs to java in other way. – Daniel Morgan Feb 12 '13 at 23:44
  • You don't send objects to a controller. This is HTTP; you send strings. Because S2 does type conversion it *looks* like you're sending an object--but you're not. As I said, if your concern is the number of properties in your controller, you can make a POJO and expose it manually or via ModelDriven. – Dave Newton Feb 12 '13 at 23:46
  • I also think you're misunderstanding how JSP works: it is evaluated *on the server* before ever getting to the client. The only thing the client knows about is HTML, JavaScript, CSS, etc. – Dave Newton Feb 12 '13 at 23:48
  • would you give me an example of exposing the pojo ? I am confused ! – Daniel Morgan Feb 12 '13 at 23:48

1 Answers1

1

If the number of action properties is the issue, expose a POJO manually, or use ModelDriven.

Doing it manually is simple, for example:

public class UserController {
    private User user; // Plus public getter and setter
}

Then in the JSP you can refer to User properties by name:

<s:form ...>
  <s:textfield key="user.firstName"/>
  ...

Using ModelDriven is theoretically even easier, since it's put on the stack automagically. It can be tricky to make sure new models are instantiated only when required, but basically (from memory):

public class UserController implements ModelDriven<User> {
    private User user;
    public User getModel() { return user; }
}

Use the User properties directly in the JSP since the User is pushed on the stack:

<s:form ...>
  <s:textfield key="firstName"/>
  ...

Similarly, on form submission, a model is created and used as the first target of methods.

Please remember that you never send objects to the Java side: you always, and only, send strings (from normal HTTP form submissions). There may be magic on the server side that transforms those strings into objects, but it's just that: magic. Magic and hope.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302