简体   繁体   English

我可以在Struts 2的隐藏字段中保留对象引用吗?

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

Can I keep a object reference in a hidden field in Struts2? 我可以在Struts2的隐藏字段中保留对象引用吗? my JSP eg: <s:hidden name="employee"/> 我的JSP,例如: <s:hidden name="employee"/>

employee is a reference which refer employee object in the action class that invoke above JSP file. employee是在上面的JSP文件中调用的操作类中引用employee对象的引用。

No You can't transfer object using s:hidden name="employee", all the parameter, what are transferred with HTTP should be string. 否不能使用s:hidden name =“ employee”传输对象,所有参数(通过HTTP传输的参数)应为字符串。 You can send object id else and retrieve object later. 您可以发送其他对象ID,以后再检索对象。

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/ http://www.java4s.com/struts-tutorials/example-on-struts-2-sessionaware-interface/

When s:hidden tag is rendered it put the value in the value attribute after evaluating OGNL expression in the name attribute. 呈现s:hidden标签时,在评估name属性中的OGNL表达式后,它将值放入value属性中。

Struts2 support basic type conversions for types other than String . Struts2支持除String其他类型的基本类型转换。 It has also support for writing custom converters for types it doesn't know as basic , but configured via XML. 它还编写自定义转换器,它不知道为基本类型的支持,而是通过XML配置。

So, you can create a new object for the type you reference in the name attribute. 因此,您可以为在name属性中引用的类型创建一个对象。 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. 但是该值不是对象引用,它是OGNL表达式求值的结果,如果它是一个Object则调用toString()方法来获取该值。

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. 您可以尝试将该字符串值传递给服务器,并实例化一个对象,该对象将没有相同的引用,但可能具有相同的哈希码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM