struts2的优点为:
1,相对于struts1没有servletAPI,是无侵入的一种设计
2,他是有拦截器的,实现了AOP编程理念
3,支持多各种表现层的技术,jsp,freemarker,velocity等等
配置struts2
所用基本jar包:
struts2-core-2.1.8.jar,xwork-core-2.1.6.jar,freemarker-2.3.15.jar,
ognl-2.7.3.jar,commons-logging-1.0.4.jar,commons-fileupload-1.2.1.jar
SRC下配置struts.xml
<package name="xxxx" extends="struts-default" namespace="/xxxx" abstract="true">
<action name="xxxx" class="com.xx.xxAction" method="execute">
<param name="xxx">abc</param>
<result name="success" type="redirect 重定向(可以重定向到action redirectAction)">/WEB-INF/xxx.jsp</result>
</action>
</package>
abstract属性表示此包只能被继承不能定义action,
namespace别忘记加/ 它的默认值是"/",
action中class默认值是:ActionSupport method默认值是:execute result的name默认值是: success type默认值是dispatcher
web中增加过滤器配置
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
配置处理的请求后缀
struts.xml <constant name="struts.action.extension" value="do,action"></constant>
对于常量,可以放入以下五个配置文件中的任何一个
struts-default.xml
struts-plgin.xml
struts.xml
struts.properties
web.xml
与struts1不同,struts2对用户请求每次都会创建一个action, 以struts2是线程安全的
指定多个配置文件
<include file="xx.xml"></include>
动态方法调用
1, http://localhost:8080/struts2.1/hello/he!add.action 调用add方法(不建议使用了)
2, <action name="test_*" class="com.xxxAction" method={1}>
...
</action>
通过com.opensymphony.xwork2.ActionContext类可以向request,session,servletContext中存储数据
ActionContext ac=ActionContext.getContext();
ac.getApplication().put("app", "application");
ac.getSession().put("session", "打印session");
ac.put("request", "打印request");
通过org.apache.struts2.ServletActionContext类可以得到javaee中的各个需要的对像
HttpServletRequest httpRequest=ServletActionContext.getRequest();
HttpSession se=httpRequest.getSession();
ServletContext sc=ServletActionContext.getServletContext();
HttpServletResponse httpResponse=ServletActionContext.getResponse();
文件上传:
<constant name="struts.multipart.maxSize" value="10701096"/>
<form action="${pageContext.request.contextPath }/hello/upload.action" enctype="multipart/form-data" method="post">
文件1:<input type="file" name="uploadFile"><Br>
文件2:<input type="file" name="uploadFile"><Br>
文件3:<input type="file" name="uploadFile"><Br>
<input type="submit" value="上传">
</form>
action中定义File的名字为表单中file类型标签的name属性 文件的名字字段为uploadFileFileName 文件类型字段为uploadFileContentType
将上传的文件保存在站点的固定位置FileUtils.copyFile(srcFile, destFile);
struts2的拦截器:
继承Interceptor接口
在struts.xml文件的<package>标签中定义拦截器标签<interceptors>
<interceptors>
<!-- 自定义一个拦截器,定义拦截器栈,将defaultStack放在前面,自定义的放在后面-->
<interceptor name="permission" class="com.dmt.studystruts2.intercepptor.PermissionInterceptor"/>
<interceptor-stack name="permissionStack">
<interceptor-ref name="defaultStack"/>
<interceptor-ref name="permission"/>
</interceptor-stack>
</interceptors>
如果想要达到在整个包中都默认使用一个拦截器栈,使用<default-interceptor-ref>标签
输入校验(手工编写代码):
1,继承ActionSupport类,重写validate()方法
this.addFieldError("username", "用户名不能为空");
2,struts.xml中定义result标签中的name属性为input,在这个页面进行错误提示
<result name="input">/index.jsp</result>
3,通过struts2标签库进行错误显示
<%@ taglib prefix="s" uri="/struts-tags"%>
<s:fielderror/>
注:对action中某一个方法进行校验,定义对应方法的验证方法validateXxx()
流程:
1,类型转换器对请求的参数进行类型转换,如果转换失败会将错误信息保存到ActionContext,conversionError拦截器会将错误信息存放到fieldErrors里.
2,无论会不会出现类型转换的异常,都会调用validateXxx()方法,再调用validate()方法
3,将信息输出到input属性指向的视图中,如果没有任何错误,将会执行action中的处理方法,进行正常流程.
输入校验(配置XML):
在xwork-core-2.1.6.jar包中可以找到校验文件的dtd文件,
在com.opensymphony.xwork2.validator.validators包下可以找到default.xml里面定义了系统定制的各种校验
定义ActionName-validation.xml
注:可以参考com.opensymphony.xwork2.validator.validators包中的源码进行学习
如果只对action中某一个方法进行校验,XML的文件名为ActionName-Action访问的名(并不是方法名)-validation.xml
流程:
1,如果以上两种XML同时出现,系统会将这两个XML里的校验规则进行汇总后进行校验,
2,如果两个XML中定义了相同的校验内容,会按照具体方法的校验规则进行校验
3,如果是父子类的action则会先搜索出父类的校验文件,再搜索出子类的校验文件,然后将四个文件汇总
国际化:
定义xxx_zh_CN.properties xxx_en_US.properties类似的国际化文件
要struts.xml中定义资源文件的名称
<constant name="struts.custom.i18n.resources" value="xxx"></constant>
1,在页面中通过struts2的标签库进行访问
<s:text name="welcome">
<s:param>李明</s:param>
<s:param>struts</s:param>
</s:text>
2,在action通过getText(String,List)来访问
ActionContext.getContext().put("xxx", this.getText("xxxx",new String[]{"liming","struts"}));
3,在表单中通过struts2的标签库进行访问
<s:textfield key="welcome"></s:textfield>
定义包范围的资源文件
资源文件的名称定义规则 package_zh_CN.properties
<s:i18n name="com/dmt/action/package">
<s:text name="welcome"></s:text>
</s:i18n>
定义action范围的资源文件
资源文件的名称定义规则 ActionName_zh_CN.properties
不通过struts.xml配置常量可通过以下方式实现国际化
<s:i18n name="dmt">
<s:text name="welcome"></s:text>
</s:i18n>