动态方法调用(DMI):
动态方法调用(DMI): dynamic_method_invocation。 功能:在一个Action中处理多个功能,从而减少系统Action的数量。 DMI 开发过程: a Action extends ActionSupport。 b Action提供多个方法完成不同的功能方法的签名(修饰符,返回值,参数表) 和execute方法一致,方法名字可以随意书写。 c struts.xml 配置文件。
DMI 开发第一种方式(不推荐):在struts.xml中的action标签中添加method属性。 <action name="add" class="day5.DMIAction" method="addUser"> <result name="success">/day3/ok.jsp</result> </action>
DMI 开发第二种方式:在struts.xml中的action标签中使用通配符。 <action name="user_*" method="{1}" class="day5.DMIAction" > <result name="success">/day3/ok.jsp</result> </action> DMI 开发第三种方式:在jsp页面中使用!的形式发出请求。

 

<a href=" user!add">添加用户</a> <a href=" user!add.action">添加用户</a> user为请求名,add为动态调用action中的方法名,注意:请求后缀要写在最后。

、<result>标签、自定义结果类型、json结果类型
1.Struts2结果类型内置(可以参考struts-default.xml): <action> <result name="" type="dispatcher" ----- forward jsp <result name="" type="redirect" ----- redirect jsp <result name="" type="chain" ----- Action forward Action <result name="" type="redirectAction" ----- Action redirect Action </action> 2.自定义结果类型 a类 implmenets Result实现execute()方法; b配置struts.xml: <package> <result-types> <result-type name="pdf" class=""/> </result-types> <action> <result type="pdf"/> </action> </package> 3.Json的Result响应类型的使用 a. 引入struts2-json-plugin.jar b. struts.xml配置<package extends="jso

c. result响应使用 <result name="success" type="json"></result> d. 如果action中的属性不需要返回: 可以在get方法前使用@JSON(serialize=false)注解。

 

7、struts.xml中其他的配置:
1.全局的异常配置: <global-exception-mappings> <exception-mapping exception="xxxException" result="error"/> <global-exception-mappings> 2.局部(action中)的异常配置: <action name="lg" class="day3.LoginAction"> <exception-mapping result="xxx" exception="xxxException"/> <result name="success">/loginOk.jsp</result> <result name="error">/loginError.jsp</result>

</action> 3. 全局的result配置: <package ....> <global-results> <result name="message">/message.jsp</result> </global-results> </package> 4.常量配置(参考struts2.core.jar中的struts-default.xml文件编写): 同时这些文件也可以写在struts.properties文件中,放到src下: A.<constant name="struts.action.extension" value="do"/> <!--该属性指定需要Struts 2处理的请求后缀,该属性的默认值是action,即所有匹配*.action的请求都由Struts2处理。如果用户需要指定多个请求后缀,则多个后缀之间以英文逗号(,)隔开。--> B.<constant name="struts.custom.i18n.resources" value="appRes"/> <!--配置国际化文件。--> C.<constant name="struts.i18n.encoding" value="uft-8"/> <!--指定默认编码集,作用于HttpServletRequest的setCharacterEncoding方法和freemarker 、velocity的输出--> D. <constant name="struts.serve.static.browserCache" value="false"/> <!--设置浏览器是否缓存静态内容,默认值为true(生产环境下使用),开发阶段最好关闭--> E.<constant name="struts.configuration.xml.reload" value="true"/> <!--当struts的配置文件修改后,系统是否自动重新加载该文件,默认值为false(生产环境下使用),开发阶段最好打开--> F.<constant name="struts.devMode" value="true" /> <!--开发模式下使用,这样可以打印出更详细的错误信息--> G.<constant name="struts.enable.DynamicMethodInvocation" value="false"/> <!–该属性设置Struts 2是否支持动态方法调用,该属性的默认值是true。如果需要关闭动态方法调用,则可设置该属性为false。--> H.<constant name="struts.multipart.maxSize" value=“10701096"/> <!--上传文件的大小限制-->
8、Struts2的中文乱码解决:
中文乱码问题一般是指当请求参数有中文时,无法在Action中得到正确的中文。Struts2中有2种办法可以解决这个问题: 1、设置JSP页面的pageEncoding=”utf-8”,就不会出现中文乱码; 2、如果JSP页面的pageEncoding=”GBK”,那么需要修改struts.i18n.encoding=GBK,在struts.xml中加入如下语句进行修改。
<struts> <constant name="struts.i18n.encoding" value="GBK"/> …… </struts>
上面2种方法可以解决POST请求中的中文参数,但是GET请求中的中文参数不能解决,
 
GET请求中的中文参数的乱码需要通过修改Tomcat的server.xml文件来解决,修改如下内容,加入URIEncoding=”GBK”:
<Connector port="8080" …… URIEncoding="GBK"/>
如果还没有把乱码问题解决,还可以加一个过滤器(Filter)在doFilter()方法中写如下代码: HttpServletRequest request = (HttpServletRequest)arg0; HttpServletResponse response = (HttpServletResponse)arg1; request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); arg2.doFilter(arg0, arg1); 然后,注意要在web.xml最上端配置这个过滤器。