Struts 的action请求形式

Struts 的action请求方式
在struts2中关于action的请求url请求基本上由三种情况:

首先要先提下struts在发送请求的处理流程:

客户端请求(http://localhost:8080/HelloWorld/stu/stuadd)->tomcat接收请求->根据HelloWorld这个项目来加载处理web.xml->把请求交给strutstfilter来处理,strutsfilter交给doFilter处理->doFilter根据struts.xml找到对应的namespace、action,及action处理完成返回的信息来显示调用对应的result页面->返回给客户端

struts.xml配置如下:

<package name="student" namespace='/stu' extends="struts-default">
    

        #1:不指定 <action method="methodName">由StudentAction中的默认函数execute来处理

        <action name="default" class="com.hupu.action.StudentAction" >
            <result>/Student_index.jsp</result>
        </action>

        #2:指定<action method="methodName"> 来处理
        <action name="index" class="com.hupu.action.StudentAction" method="index"  >
            <result>/Student_index.jsp</result>
        </action>
    
             <!-- method 请求时 回调用 method=add的方法 -->
        <action name="stuadd" class="com.hupu.action.StudentAction" method="add"  >
            <result name='add_student'>/Student_add.jsp</result>
            <result name='add_student_error'>/Student_add_error.jsp</result>
        </action>

          #3: <!-- DMI 动态方法调用 user!add  这样 就可以只配置这一样action 就能处理相似的请求-->
        <action name="user" class="com.hupu.action.UserAction">
            <result name='add_user'>/user_add.jsp</result>
        </action>

        #4:通配符处理
        <action name="*_*" class="com.hupu.action.{1}Action" method="{2}">
            <result>/{1}_{2}.jsp</result>
        </action>
    </package>




参数传递:struts中参数传递有基本三种方法:

1:Action接收参数:
    1:由Action中的getXXX(),setXXX()来处理参数的接收,其中的Action的属性名称不一定要传递的参数的名称一样,但是getXXX后面的名称一定要一致。

代码:


public class UserAction extends ActionSupport {
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    //参数和成员变量时一一对应的 类型自动转换
    private String name;
    private int age;

    public String add(){
        return "add_user";
    }
}

    2:域模型对象

创建一个域模型对象接收参数此时参数的传递:user.name=.....

同时由于可能域模型字段比要接收的字段少比如password的输入的验证,这时可以使用DTO(数据传输对象)

作为中间的数据接收然后再传递来处理

    DTO:数据传输对象 
    DO:数据对象
    3:modeldriven 模型驱动
代码:使用的是com.opensymphony.xwork2.ModelDriven;模型驱动类

这是请求的url地址:http://localhost:8080//HelloWorld/stu/stuadd?name=liujijun&age=27 就能填充到

Student类中,这是需要单独的new出来一个student对象

public class StudentAction extends ActionSupport implements ModelDriven<Student> {
   
    private Student student = new Student();
   
    public String index(){
       
        return SUCCESS;
    }
   
    public String add(){
       
        if(true){
            this.addFieldError("name", "name is error");
            this.addFieldError("name", "name is too long");
            return "add_student_error";
        }
        return "add_student";
    }

    @Override
    public Student getModel() {
        // TODO Auto-generated method stub
        return student;
    }
   
    @Override
    public String execute(){
        return SUCCESS;
    }

}