博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
struts 2 简单笔记
阅读量:6991 次
发布时间:2019-06-27

本文共 2865 字,大约阅读时间需要 9 分钟。

struts 2是对MVC模式的封装:

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

1. 配置struts.xml

/index.jsp

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如下:

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

public class LoginAction extends ActionSupport implements ModelDriven
{ 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设置如下:

step2.jsp
step3.jsp
index.jsp
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配置如下:

step2.jsp

实现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,返回需要的视图;

 

 

转载于:https://www.cnblogs.com/Fredric-2013/archive/2013/03/28/2987387.html

你可能感兴趣的文章
iOS speex
查看>>
模块和包
查看>>
js_js流程控制
查看>>
asp.net mvc全局错误处理
查看>>
[摘录]第四章 工作本身就是一种激励
查看>>
【目录】java学习路径
查看>>
11G、12C Data Guard Physical Standby Switchover转换参考手册
查看>>
root.sh脚本支持checkpoints文件实现重复运行
查看>>
Algs4-2.4.20证明:基于下沉的堆构造方法的比较次数、交换次数
查看>>
16进制的简单运算http://acm.nyist.net/JudgeOnline/problem.php?pid=244
查看>>
leetcode3. Longest Substring Without Repeating Characters
查看>>
Jmeter之Bean shell使用
查看>>
C#中泛型的使用笔记
查看>>
【bzoj4009 hnoi2015】接水果
查看>>
@property专题
查看>>
LNMP结合discuz的配置
查看>>
js中ul与li的使用
查看>>
实验二
查看>>
jquery.artDialog.source.js学习
查看>>
PDF去除签名
查看>>