Struts2应用开发步骤:
1.将Struts2的lib文件夹下的commons-fileupload-1.2.1.jar、commons-io-1.3.2.jar、freemarker- 2.3.16.jar、javassist-3.7.ga.jar、ognl-3.0.jar、struts2-core-2.2.1.jar、xwork-core-2.2.1.jar复制到 Web应用WEB-INF/lib路径下
2.在web.xml中配置核心Filter:
<filter> <filter-name>struts2</filter-name> <filterclass>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter </filter-class> </filter> <!-- 让Struts 2的核心Filter拦截所有请求 --> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
3.定义发送请求的表单
4.定义处理用户请求的Action
5.配置Action,放在类加载路径的struts.xml中配置:(也可使用约定)
struts.xml:
<action name="login" class="...."> ... </action>
6.配置处理结果与视图资源的对应:
struts.xml:
<action name="login" class="org.wang.app.action.LoginAction"> <result name="input">/login.jsp</result> <result name="success">/welcome.jsp</result> ... </action>
7.编写视图资源(如果Action需要将一些数据传入视图资源,可借助与OGNL表达式)
常量配置:
struts.xml:(一般处于开发模式应,配置常量为开发模式更方便)
<constant name="struts.devMode" value="true"/> <!--指定了国际化资源文件的baseName为mess--> <constant name="struts.custom.i18n.resources" value="mess"/>
web.xml:
<?xml version="1.0" encoding="GBK"> <web-app ... version="3.0"> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> <init-param> <param-name>struts.custom.i18n.resources</param-name> <param-value>mess</param-value> </init-param> </filter> .... </web-app>
包含其他配置:
struts.xml:
<include file="struts-part1.xml"/>
实现Action:(无需继承任何类,实现任何接口,如果包含HTTP请求参数,重要的是包含getter,setter方法,可封装请求参数,和处理结果,
包含execute()方法处理用户请求,可通过<s:property..>输出处理结果,
通常继承ActionSupport,ActionSupporty已经提供了许多默认方法,直接继承该类将简化开发,如果没有提供Action,默认使用ActionSupport)
Action访问ServletAPI:(可通过ActionContext来访问ServletAPI,操作request、sessio、application范围的属性)
创建ActionContext的方式通常为:
ActionContext ctx = ActionContext.getContext();
Action直接访问ServletAPI:(通过实现接口):
>ServletContextAware:(可以访问到ServletContext)
>ServletRequestAware:(可以访问到HttpServletRequest)
>ServletResponseAware:(可以访问到HttpServletResponse)(即使获取了HttpServletResponse也不可以生成服务器响应,Action只是业务控制器)
使用ServletActionContext访问ServletAPI:(Struts提供了ServletActionContext工具类)
配置Action:
包和命名空间:
1.每个package配置一个包,使用包来组织Action,<action../>在<package../>下
2.Struts2的核心是Action、拦截器等
3.每个包是多个Action、多个拦截器、多个拦截器引用的集合
4.name属性该包唯一标识
5.extends属性指定该包继承另一个包,可以继承到拦截器、拦截器栈、action等配置
6.abstract="true"指定抽象包
7.namespace属性指定该包的命名空间
8.由于Struts2的配置文件是从上到下处理,所以父包应该在子包前面定义
9.通常继承struts-default包
Action的基本配置:(虽然Action的name命名可以非常灵活,但如果name属性中包含(.)或(-)则可能引发一些未知异常)
<package> <action name="login" class="lee.LoginAction"/> .... </package>
Action的动态方法调用:(用户通常不同按钮提交同一个表单)(开启系统动态调用通过设置struts.enable.DynamicMethodInvocation常量)
<form action="login"> ... <input type="submit" value="登录"/> <input type="submit" value="注册" onclick="regist()"/> </form> <script type="text/javascript"> function regist() /*action="ActionName!methodName"(ActionName指定提交到那个Action,而methodName指定提交到那个方法)*/ {... //或target.action=regist targetForm.action="login!regist"; } </script>
public class LoginRegistAction extends ActionSupport { .... public String regist() {.... return .. } public String execute() { .... return ... } }
<action name="login" class="wang.app.action.LoginRegistAction"> .... </action> <action name="regist" class="wang.app.action.LoginRegistAction" method="regist"> .... </action>
通配符:
<action name="*Action" class="wang.app.action.LoginRegist" method={1}> ... </action> <action name="*Action" class="wang.app.action.{1}Action"> ... </action> <action name="*_*" method="{2}" class="wang.app.{1}"> <result>/{2}.jsp</result> </action> <action name="*"> <result>/{1}.jsp</result> </action>
(对于只是简单的超级链接,可以定义名为"*"的Action(该Action应该放在最后定义))
(如果URL为abcAction.action的请求,如果struts.xml中有abcAction则由它处理,否则对于通配符匹配则按顺序,先找到先处理)
配置默认Action:
<package name="wang" extends="action_default"> <default-action-ref name="simpleViewResultAction"> ... <action name="simpleViewResultAction" class="...."> <result../> ... </action> ... </package>
配置Action的默认处理类:
在struts-default.xml中
<package name="struts-default" abstract="true"> ... <default-class-ref class="com.opensymphony.xwork2.ActionSupport"> </package>
(完全可以在struts.xml中配置自己想要的,覆盖掉它)
配置处理结果:
>局部结果:(<result../>作为<action../>元素的子元素)
>全局结果:(<result../>作为<global-results../>元素的子元素)(对所有Action都有效,如果局部与之同名,全局会被覆盖表)
<global-results> <!-- 配置Result,使用OGNL表达式来指定视图资源 --> <result name="success">/${target}.jsp</result> </global-results>
局部:(默认使用dispatcher类型,返回success视图)
>name:逻辑视图名
>type:结果类型
<action name="login" class="wang.LoginAction"> <result name="success" type="dispatcher">/thank_you.jsp</result> </action>
通常使用方式:
<action name="login" class="wang.LoginAction"> <result name="success">/thank_you.jsp</result> ... </action>
最简洁方式:
<action name="login" class="wang.LoginAction"> <result>/thank_you.jsp</result> </action>
Struts2内建的支持结果类型:
>chain:Action链式处理
>dispatcher:JSP作为视图
>freemarker:FreeMarker模板作为视图
>httpheader:用于控制特殊的HTTP行为
>redirect:直接跳转到其他URL结果类型(会都是所有请求参数、请求属性)
>redirectAction:直接跳转到其他Action
>stream:向浏览器返回一个InputStream(一般用于文件下载)
>velocity:Velocity模板作为视图
>xslt:用于与XML/XSLT整合的结果类型
>plainText:显示某个页面原始代码的结果类型
Action属性值决定物理视图资源:
${属性名.属性名.属性名}
public class MyAction extends ActionSupport { private String target; ... public String execute() { ... return ... } }
<action name="MyAction" class="wang.app.MyAction"> <result>/${target}.jsp</result> </action>
动态结果:
<action name="*"> <result>/{1}.jsp</result> </action>
配置异常处理:(局部异常与全局异常配置同一异常类型时,局部会覆盖全局异常)
(全局异常的result属性值通常不要使用局部结果,局部异常属性值既可使用全局也可使用局部异常结果)
异常的配置:
<global-results> <!-- 定义当sql、root两个逻辑异常都对应exception.jsp页 --> <result name="sql">/exception.jsp</result> <result name="root">/exception.jsp</result> </global-results> <!-- 定义全局异常映射 --> <global-exception-mappings> <!-- 当Action中遇到SQLException异常时, 系统将转入name为sql的结果中--> <exception-mapping exception="java.sql.SQLException" result="sql"/> <!-- 当Action中遇到Exception异常时, 系统将转入name为root的结果中--> <exception-mapping exception="java.lang.Exception" result="root"/> </global-exception-mappings> <action name="login" class="org.crazyit.app.action.LoginAction"> <!-- 定义局部异常映射, 当Action中遇到MyException异常时, 系统将转入name为my的结果中--> <exception-mapping exception="org.crazyit.app.exception.MyException" result="my"/> <!-- 定义三个结果映射 --> <result name="my">/exception.jsp</result> <result name="error">/error.jsp</result> <result name="success">/welcome.jsp</result> </action>
异常信息的输出:
<!-- 输出异常对象本身--> <s:property value="exception"/> <!--输出异常堆栈信息--> <s:property value="exceptionStack"/> <!--输出异常的message信息--> <s:property value="exception.message"/>