struts2中Action名称的搜索顺序举例说明

来源:互联网 发布:亚马逊数据分析师 编辑:程序博客网 时间:2024/06/05 20:10
 

Action名称的搜索顺序举例说明

   

Struts.xml:

<?xml version="1.0" encoding="GBK"?>

<!DOCTYPE struts PUBLIC

    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"

    "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

    <package name="test" extends="struts-default" namespace="/cs">

       <action name="hello" class="cn.csdn.hr.action.HelloAction"

           method="add">

           <result>../hello.jsp</result>

       </action>

    </package>

 

    <package name="ast" extends="struts-default" namespace="/cs/test">

       <action name="hello" class="cn.csdn.hr.action.HelloAction"

           method="add">

           <result>../../hello.jsp</result>

       </action>

    </package>

</struts>

 

从以上的代码中可以看出:

第一个package的命名空间为namespace="/cs",action的名字为hello,所访问的类为:cn.csdn.hr.action.HelloAction,访问的类中的方法为add,返回的结果在hello.jsp页面。

第二个page的命名空间为namespace="/cs/test",action的name为hello1,所访问的类为:"cn.csdn.hr.action.HellosAction",访问的类中的方法为:add,返回的页面也是hello.jsp,只是访问的路径不一样.

当我们访问的路径为:

    http://localhost:8080/cs/hello.action  的时候可以跳转到../hello.jsp 这个页面。

    当我们访问路径为:

    http://localhost:8080/cs/test/hello.action  的时候,它可以跳转到../../hello.jsp这个页面。

    当我们访问的路径为:

    http://localhost:8080/cs/test1/hello.action  的时候,因为没有test1这个路径,所以struts2会自动的找http://localhost:8080/cs   路径下看有没有name为hello的action,如果有的话就直接执行cs下的package下的result,去跳转。

    Action访问的路径是逐个寻找的

 

    注:

        当HelloAction继承ActionSupport之后,里面的方法只有默认的方法时,可以省略class   如果想要访问HelloAction中的其他方法,用method来访问,访问时必须要自定class的路径

 

 

Java类:

package cn.csdn.hr.action;

 

import com.opensymphony.xwork2.ActionSupport;

 

//action为普通的java类        可以继承接口  Action

public class HelloAction extends ActionSupport{

    private static final long serialVersionUID = 1L;

    //默认的方法是execute,方法必须是string类型的

    @Override

    public String execute() throws Exception {

       System.out.println("你 执行了!吧!");

       //直接返回success

       return SUCCESS;

    }

    public String add() throws Exception {

       System.out.println("你 执行了!吧!=============");

       //直接返回success

       return SUCCESS;

    }

}

原创粉丝点击