struts 2 简单笔记

struts 2是对MVC模式的封装:

struts 2 简单笔记

一.其基本特性例子如下:

1. 配置struts.xml

<package name = "struts" namespace="" extends="struts-default">

    <action name = "login" class="com.LoginAction">

        <result name = "LOG">/index.jsp</result>

    </action>

</package>

2.实现com.LoginAction

public class LoginAction extends ActionSupport {

    private String name;

    private String mail;

    public String execute()throws Exception{

        

        System.out.println(name);

        System.out.println(mail);

        

        return "LOG";

    }

3. 前端调用jsp如下:

       <s:form action = "login">

       <s:textfield label = "id" name = "id"></s:textfield>

       <s:textfield label = "name" name = "name"></s:textfield>

       <s:submit value = "submit"/>

       </s:form>

二.同时struts 2也支持领域模型驱动,类似于将前端界面封装成VO的POJO类,此时需要实现Action类如下:

public class LoginAction extends ActionSupport implements ModelDriven<User> {



    private final User usr = new User();

        

    public String execute()throws Exception{

            

        

        System.out.println(getModel().getId());

        System.out.println(getModel().getName());

        return "LOG";

    }

    

    public User getModel() {

        return usr;

    }

其中必须继承并实现ModelDriven<User>接口
三. 通过action中方法的跳转,可以实现类似business业务流的封装。典型的应用就是功能向导,此时struts 2设置如下:

    <action name = "step1" class = "com.StepAction" method = "stepMethod1">    

         <result name = "step2">step2.jsp</result>

         <interceptor-ref name = "testinter"></interceptor-ref>

     </action>

    <action name = "step2" class = "com.StepAction" method = "stepMethod2">

         <result name = "step3">step3.jsp</result>

     </action>    

     <action name = "step3" class = "com.StepAction" method = "stepMethod3">

         <result name = "index">index.jsp</result>

     </action>
public class StepAction extends ActionSupport {

    private String step1Value;

    private String step2Value;

    private String step3Value;

    

    public String stepMethod1(){

        ActionContext.getContext().getSession().put("stepvalue1", step1Value);

        return "step2";

    }

    

    public String stepMethod2(){

        ActionContext.getContext().getSession().put("stepvalue2", step2Value);

        return "step3";

    }

    

    public String stepMethod3(){

        ActionContext.getContext().getSession().put("stepvalue3", step3Value);

        return "index";

    }

四. struts 2的拦截器也是比较典型的应用,类似于servlet的过滤器,实现是struts 2配置如下:

      <interceptors>

     <interceptor name = "testinter" class  ="com.MyIntercept"/>

   </interceptors>

     <action name = "step1" class = "com.StepAction" method = "stepMethod1">    

         <result name = "step2">step2.jsp</result>

         <interceptor-ref name = "testinter"></interceptor-ref>

     </action>

实现interceptor类如下:

public class MyIntercept extends AbstractInterceptor {



    @Override

    public String intercept(ActionInvocation arg0) throws Exception {

        // TODO Auto-generated method stub

        Object action = arg0.getAction();

        

        System.out.println("intercept");

        String result = arg0.invoke();

        return result;

    }

}

五.struts 2的内部机制整理如下:

  1. 接收到客户请求后,FilterDispatcher调用ActionMapper看是否存在调用的Action
  2. 若存在,FilterDispater调用其方法serviceAction;
  3. 在serviceAction方法中首先调用createContextMap,把application,session,request的keyvalue拷贝到map中;
  4. 通过StrutsObjectFactory创建代理类actionProxy,执行ActionProxy的invoke方法;
  5. 在invoke方法中创建defaultActionInvocation,该类的init函数中createAction创建action;
  6. 然后defalutActionInvocation的invode函数中,会首先遍历执行interceptor,然后执行invokeActionOnly; 注:action中的属性是在拦截器执行完后才赋值的,防止拦截器对属性值有修改,没看到这段代码。
  7. 在此函数中正真的action方法被通过反射的手段调用;
  8. 执行完毕后会由defaultActionInvocation调用executeResult,返回需要的视图;

 

 

你可能感兴趣的:(struts)