0

Can I keep a object reference in a hidden field in Struts 2?

My JSP:

e.g.: <s:hidden name="employee"/>

employee is a reference which refer employee object in the action class which invokes above JSP file.

Roman C
  • 49,761
  • 33
  • 66
  • 176
Sampath
  • 21
  • 3
  • Please be more specific about your question. What do you exactly need to do ? What is an employee ? And why do you want to keep the reference in hidden field ? – Saif Asif Nov 28 '13 at 07:17

2 Answers2

2

No You can't transfer object using s:hidden name="employee", all the parameter, what are transferred with HTTP should be string. You can send object id else and retrieve object later.

or You can put your object into session, so that you can access it anytime you want. here is an example::

http://www.java4s.com/struts-tutorials/example-on-struts-2-sessionaware-interface/

dev
  • 715
  • 5
  • 21
1

When s:hidden tag is rendered it put the value in the value attribute after evaluating OGNL expression in the name attribute.

Struts2 support basic type conversions for types other than String. It has also support for writing custom converters for types it doesn't know as basic, but configured via XML.

So, you can create a new object for the type you reference in the name attribute. But the value is not the object reference, it is the result of the OGNL expression evaluation, and if it is an Object then toString() method is called to get the value.

You may try to pass that string value to server and instantiate an object that will not have the same reference but might have the same hash code.

Roman C
  • 49,761
  • 33
  • 66
  • 176