Search code examples
htmlformsjspstruts2javabeans

Struts2 - How to bind back the value to POJO which is rendered through <s:property> on jsp page


I am working on Struts 2 and displaying a POJO/bean property value onto the JSP through <s:property> tag.

But when I submit back to the action, I am not getting this value binded to POJO.

<s:property value="name" />

I am getting the values of editable fields(ex: text field) back onto the action, but non-editable values are not binding back to the pojo.

At present I am using <s:hidden name="name" /> tag to bind back the data to pojo.

I am implementing the ModelDriven interface to action class.

But is there any other way to bind the non-editable values back onto pojo?


Actually i have a "List" of objects in the form, which i am iterating in jsp and displaying the values using and tags as per my requirement.

When i submit the form, i am navigating to another action class(which is using the same form bean/pojo) and here i am getting the values of only textfields but not those which are displayed with tag.

To get those values i am using the tag.

But is there any way to bind the form values back to pojo using <s:property>? Please Suggest.


Solution

  • The values that are rendered via s:property tag doesn't need to submit to the action because these values never change. Between requests the session scope is available to the action. So you won't carry them all in JSP with hidden fields. Hope you know how to implement a session map with your actions. Put the values to the session and forget about passing them to the action.

    I found that many scopes are available to you. Lets look at all of them.

    Now you have to learn a little bit about session map from this answer.

    And finally you learn how to put values into session map.

    A lot of more answers related to this topic could be extracted but you can find all of them in my answers.