Struts2的体系结构_struts2开发框架介绍
Struts2的体系结构由刀豆文库小编整理,希望给你工作、学习、生活带来方便,猜你可能喜欢“struts2开发框架介绍”。
•Struts2的体系结构
定义常量
常量可以在struts.xml或struts.properties中配置,建议在struts.xml中配置,两种配置方式如下: struts.xml
struts.properties
struts.action.extension=do
通常,struts2按如下搜索顺序加载struts2常量: struts-default.xml struts-plugin.xml struts.xml
struts.properties web.xml
如果在多个文件中配置了同一个常量,则后一个文件中配置的常量值会覆盖前面文件中配置的常量值.常用的常量
如果用户需要指定多个请求后缀,则多个后缀之间以英文逗号(,)隔开。-->
Struts2的Action(业务逻辑控制器)
相对与Struts1而言,struts2采用了低侵入式的设计,struts不要求Action类继承任何的Struts2基类,或者实现任何Struts2接口,在这种设计下,Struts2的Action类是一个普通的POJO,通常只需要包含一个无参的execute方法,从而有很好的代码复用性。
但为了让用户开发Action类更加规范,Struts2提供了一个Action接口,这个接口定义了Struts2的Action处理规范。下边是一个标准Action接口的代码。
Public interface Action{
Public static final String ERROR=”error”;
Public static final String INPUT=”input”;
Public static final String SUCCESS=”succe”;
Public static final String NONE=”none”;
Public static final String LOGIN=”login”;
//定义处理用户请求的execute方法
Public String execute()throw Exception;}
另外Struts2还提供了一个实现类:ActionSupport(Action的实现类),该类中提供了许多默认的方法,这些默认的方法包括国际化信息的方法,数据校验的方法,默认的处理用户请求的方法。继承该类能大大的简化Action的开发。
三、使用struts2封装表单元素的值:
1.属性驱动:直接在Action中添加属性和对应的getter和setter方法 2.模型驱动:
a)必须实行ModelDriven接口,并且重新getModel方法 b)初始化属性 private Userinfo user = new Userinfo();c)在页面上表单元素的名称为实体类的属性名称
四、如何获取servlet API中的对象(request,seion,application)等
1. 使用ActionContext获取对象(Map)
2.使用ServletActionContext对象获取
3.使用DI注入的方式(IoC方式)
a)实行相应的接口(xxxAware),request对象的接口(ServletRequestAware),response(ServletResponseAware),seion(SeionAware)等
b)在Action中添加相应的属性和setter方法(setter注入)