我正在做一个关于 Struts 2 的项目,我正在一点一点地获得知识..
我有这样的 Action
<action name="backaction" class="HandlerAction">
<result name="user_profile" type="redirect">hai.jsp</result>
在类 Handler 操作中,我有对象 userprofile
,其中 name
和 age
是成员。
在执行函数中
log.info(userprofile.getName())//it is giving name xyz
return "user_profile"
我正在获取 hai.jsp,但无法在 jsp 中检索该对象 userprofile
中的值。
hai.jsp如下..
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
<body>
<s:textfield name="user_name" value="%{userprofile.name}"/>
</body>
</html>
我也尝试将其设置为 value="%{name}"
但我没有得到值 xyz..
最佳答案
问题在于
<result name="user_profile" type="redirect">hai.jsp</result>
因为您使用的是重定向结果类型,这意味着框架将创建一个新的请求和响应对象并丢弃旧的请求/响应对象。因此,当您从操作返回时,您的用户对象在值堆栈中是他们的,直到您告诉 S2 您要使用redirect
结果类型。
在看到重定向结果类型时,它将丢弃任何现有数据,并为您创建一个新请求,将其内容放入值堆栈中,这就是为什么这对您不起作用。我不确定您为什么使用 redirect
结果类型,因为您可以使用任何内置结果类型(例如 success
)执行相同操作。
如果您仍想使用重定向结果,我建议您将用户配置文件数据保存在 Session
中,并在下一步操作中检索它或使用 scope-interceptor
关于java - 从action类访问jsp页面中class的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9930808/