Martin Dvoracek
Martin Dvoracek

Reputation: 1738

Getting back the object from JSP

I can send an object via model from a controller to a jsp page, but how to get that object back to the controller, after saving some data to it?

My form looks like this:

    <form:form method="POST" modelAttribute="user" action="/GENEPI/userChangePassword" commandName="user">      
                        <spring:message code="label.newPassword" />
                        <br>
                        <input id="password" type="password"
                            pattern=".{8,30}" class="input-block-level"
                            onFocusOut="passwordValidation();"
                            title="ERROR" />
            </form:form>

And my controller like this:

@RequestMapping(value = "/userChangePassword", method = RequestMethod.POST)
public String changePasswordPOST(@ModelAttribute("user") UserEntity user,
        @ModelAttribute("newPassword") String newPassword, Model model) {
    System.out.println("USERNAME: " + user.getUsername());

}

Nevertheless user is always null. How to make it to pass that user object from jsp to controller with the data user has filled there?

edit: with other words - is there some way, how to fill in the model map with pairs key-value from jsp page and post it then back to controller?

Upvotes: 1

Views: 3203

Answers (4)

hekomobile
hekomobile

Reputation: 1388

can you use spring taglib form and hidden into i.e. table, something like that:

<table align="center" style="position: relative; left: 0px; top: -20px; width: 100%">
  <tr>
   <td width="40%" class="">
    <form:label path="user">Username: </form:label></td>
   <td width="60%" class="">
    <form:input path="usuario" cssClass="" readonly="true"/></td>
  </tr>
    <form:hidden path="password" id="pwdDB" />
  <tr>
   <td width="40%" class="">
    <form:label path="password">Password: </form:label></td>
   <td width="60%" class=""><form:password path="" id="password" cssClass="" maxlength="14"/></td>
  </tr>
  <tr>
   <td colspan="2"><form:hidden path="username" /></td>
  </tr>
  <tr>
   <td colspan="2"><form:hidden path="otherattributePOJO" /></td>
  </tr>
  <tr>
   <td colspan="2"><form:hidden path="anotherattributePOJO" /></td>
  </tr>
</table>

And you put attribute from your POJO onto path.

I hope help you :)

Upvotes: 1

beinghuman
beinghuman

Reputation: 2136

Spring uses the concept of Data Binding if you use it with Spring Form tag.

Check this link: Using Spring Form Library

The logic is you pass your Bean or Model to JSP,user will enter the values and then the model is passed to the controller. You have to use @ModelAttribute to get the Model and then after you have got the model you can use it.

I see two mistakes in your JSP and Controlller. 1.You have used the modelAttribute and commandName on the same Model ie. user. Either user modelAttribute or commandName.

2.There is no Model named as newPassword in your JSP and you are trying to get it in your Controller.

3.I assume that this User Bean has property of Password which you are trying to set it from JSP.So you have to get that User Bean not the individual property.

Let me know if you still have doubt.

Upvotes: 1

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 279970

A jsp gets rendered as HTML which is sent as the body of an HTTP response to some client.

There is no concept of Object in HTML. What you are doing with the form is serializing some input fields as request parameters and sending those in a new HTTP request. In this case, you are sending a POST.

Spring's uses an interface called HandlerMethodArgumentResolver to construct arguments for your @Controller handler methods. It uses an implementation called ModelAttributeMethodProcessor to resolve parameters that have the @ModelAttribute annotation. The javadoc states

Model attributes are obtained from the model or if not found possibly created with a default constructor if it is available. Once created, the attributed is populated with request data via data binding and also validation may be applied if the argument is annotated with @javax.validation.Valid.

In other words, if you don't have an attribute in the model that is accessible through a named key (@ModelAttribute value attribute value), Spring will construct it from request parameters whose names are equal to the names of your Class' fields. In this case, there are none.

If you want Spring to serialize a UserEntity object for you, you need to put input fields in the form that have the same name attribute value as the UserEntity class fields, and give those inputs a value, probably the ones it had previously.

Take a look at the documentation for Spring's tag libraries.

Upvotes: 1

Scary Wombat
Scary Wombat

Reputation: 44834

The basic idea is that using a HTTP Post, you are usually setting your input data to Strings or primitive data types and the data will be posted to a servlet.

This basic idea remains the same for all of the frameworks which extend the basic http framework. E.g. Struts, Spring MVC, StripesFramework etc.

I suggest that you get comfortable with how HTTP works.

In your particular case the key information for looking up the user should also be able to be posted back to the servlet either by having the user eneter the user information (usually userid/password) or be storing the information as a hidden field.

Upvotes: -1

Related Questions