Struts2生命周期的Action对象

时间:2013-01-28 17:38:59

标签: struts2

我是Struts2框架的新手,我没有太多时间阅读任何Struts2书籍。我只是从YouTube上学习它。无论如何,这是我的问题。

据说我的struts.xml如下:

<struts>
<package name="p1" namespace="/" extends="struts-default">
    <action name="login" class="org.tutorial.struts2.action.LoginAction">
        <result name="success"  type="redirect" >searchTutorialForm</result>
        <result name="error">/login.jsp</result>
    </action>
    <action name="searchTutorialForm">
        <result>/searchForm.jsp</result>
    </action>
</package>  
</struts>

让我们谈谈包p1。

如果URL是[http:// localhost:8080 / Struts2 / login],则会调用org.tutorial.struts2.action.LoginAction,成功后会将其重定向到调用searchForm的searchTutorialForm操作标记。 JSP。

所以客户端看到的URL是[http:// localhost:8080 / Struts2 / searchTutorialForm] (目的不是让客户看到[http:// localhost:8080 / Struts2 / searchForm.jsp])

现在LoginAction中有一些成员变量,它们使用tag在searchForm.jsp中显示。但是使用这种方法它们不会显示,因为我认为对象LoginAction不再在ValueStack中(重定向之后,我认为??)。

当然,如果我不使用上述方法,而是按照以下方式使用:

<struts>
<package name="p1" namespace="/" extends="struts-default">
    <action name="login" class="org.tutorial.struts2.action.LoginAction">
        <result name="success">/searchForm.jsp</result>
        <result name="error">/login.jsp</result>
    </action>
</package>  
</struts>

然后使用标记在success.jsp中显示LoginAction对象中的成员变量(但是然后用户将看到URL [http:// localhost:8080 / Struts2 / searchForm.jsp])

基本上我的意图是让用户不要看到任何特定的内部文件或调用.jsp或.action。

重要说明: 动作标签searchTutorialForm中没有动作类 - 基本上是一个虚拟动作。

问题:
1.如何使用第一种方法在LoginAction对象中显示成员变量?
2. Value Stack中LoginAction对象的生命周期是多少?

感谢。

1 个答案:

答案 0 :(得分:7)

  • 根据请求创建操作。
  • 重定向会导致新请求。
  • 因此以前的操作中的对象不再可用。

问题:

  1. 您将其置于会话中或将其作为参数包含在重定向中。
  2. 请求结束时,操作会消失。
相关问题