Struts Notes(2)--------Action, redirect, OGNL

来源:互联网 发布:2017淘宝内容营销 编辑:程序博客网 时间:2024/04/30 01:03

1. Action 的搜索顺序

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"    "http://struts.apache.org/dtds/struts-2.0.dtd"><struts><package name="itcast" namespace="/test" extends="struts-default"><action name="helloworld" class="com.ssh.test.HelloWorldAction" method="execute"><result name="success">/WEB-INF/page/hello.jsp</result></action></package></struts>

1.1 搜索路径含有不存在的namespace

在Action 搜索时,标签<struts> 的 package 有name, namespace 属性, 其中namespace 包含了Action 的搜索路径。如上图,helloworld 的正确搜索路径为:http:localhost:8080/ ProjectName/test/helloworld. namespace的优点是方便了相同类型Action 的书写。不必重复书写相同路径。

http://localhost:8080/ProjectName/test/sdf/34/d/helloword (例1)

在这种情况下,系统首先寻找 /test/sdf/34/d/ 目录下是否有helloworld Action.如果没有,会一次向上一层寻找,直到找到 http://localhost:8080/ProjectName/test/helloword。 所以例一情况有效。

1.2 namespace中没有要找到action

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"    "http://struts.apache.org/dtds/struts-2.0.dtd"><struts><package name="itcast" namespace="/test" extends="struts-default"></package><package name="it" namespace="" extends="struts-default"><action name="helloworld" class="com.ssh.test.HelloWorldAction" method="execute"><result name="success">/WEB-INF/page/hello.jsp</result></action></package></struts>

如图所示,如果存在相应的namespace,但是在这个namespace中没有要找的action, 系统还会自动去默认的namespace中寻找(namespace="", 或没有namespace ), 如上图,任然可以在package "it" 中找到action helloworld

2.  Action中设置请求的转发

如果需要在Action中不做任何操作,直接转发请求,应当如下配置struts.xml

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"    "http://struts.apache.org/dtds/struts-2.0.dtd"><struts><package name="itcast" namespace="/test" extends="struts-default"></package><package name="itcast" namespace="" extends="struts-default"><action name="helloworld" ><result >/WEB-INF/page/hello.jsp</result></action></package></struts>

如果没有为action 指定class, 默认为 ActionSupport : 
<action name="helloworld" class="ActionSupport">
如果没有为action指定method, 默认执行action的execute() 方法。
如果没有指定result的name属性,默认为 success。 打开ActionSupport类的源码,发现如下:

 /**     * A default implementation that does nothing an returns "success".     * <p/>     * Subclasses should override this method to provide their business logic.     * <p/>     * See also {@link com.opensymphony.xwork2.Action#execute()}.     *     * @return returns {@link #SUCCESS}     * @throws Exception can be thrown by subclasses.     */    public String execute() throws Exception {        return SUCCESS;    }

所以,它返回值为,success, 而在<result> 中没指定name时默认为,success, 所以会跳转到指定的路径去。


3. Result 的各种转发类型(type属性可为:dispatcher,redirect, redirectAction, plaintext)

3.1 dispatcher 内部请求转发

dispatcher 可以在页面中直接使用${ }得到action中的属性值。

3.2 redirect 浏览器重定向

引导浏览器访问某个路径。注:无法访问到WEB-INFO目录下的jsp页面。

<struts><package name="itcast" namespace="/test" extends="struts-default"><action name="helloworld" class="com.ssh.test.HelloWorldAction" method="execute"><result name="success">/WEB-INF/page/hello.jsp</result></action><action name="test" ><result type="redirect">/hello.jsp</result></action></package></struts>

如上,hello.jsp的路径必须在WEB-INFO目录外。redirect 请求的页面不能直接用 ${} 去取得Action中的值。
step 1: 
<result type="redirect">/hello.jsp?username=$(myname)</result>
step2: 
在页面中 使用$(param.username)去得到传递过来的值

3.3 redirectAction 重定向到action

如上图,将 redirect 改为 redirectAction, 将/hello.jsp改为action 的名称,如“test”

如果需要重定向的action 在另外一个包中,需要使用<result  type="redirectAction"> 内的<param>注入需要寻找到action 的信息。<param name="actionName">需要重定向的Action名称</param>, <param name="namespace“>需要重定向的action 的namespace</param>
<action name="helloworld" ><result name="success" type="redirect"><param name="actionName">xxx</param><param name="namespace">/control/test</param></result></action>


4.OGNL表达式在result 转发中的应用 ${}

当客户需要查询用户数据时,在a页面输入username,请求会在相应的action中execute相应操作,跳转到修改界面,如果,客户输入密码错误,重定向时,可以携带action 的属性。

<struts><package name="itcast" namespace="/test" extends="struts-default"><action name="helloworld" class="com.ssh.test.HelloWorldAction" method="execute"><result name="success" type="redirect">/hello.jsp?username={username}</result></action></package></struts>

在转向后的页面中如果想得到传来的值: 在页面中使用OGNL表达式: ${param.username}

5.Struts2 的配置文件: sturts.xml

通常以模块划分一个配置文件,一个项目中经常会含有多个配置文件。例如 employee.xml, department.xml. struts.xml.
在struts.xml中会配置: 

<struts><constant name="struts.action.extension" value="do,action"></constant><include file="department.xml"></include><include file="employee.xml"></include></struts>





原创粉丝点击