`

传智播客——Struts2转发类型 【转】

 
阅读更多

    原文转自:http://blog.csdn.net/zhchzh1000/article/details/4644049

    今天黎老师给我们讲Action中result的各种转发类型,他讲得真得好经典,听他讲课就是一种享受!根本不用下课用多长时间就可以消化!
action name="helloworld" class="cn.itcast.action.HelloWorldAction">
 <result name="success">/WEB-INF/page/hello.jsp</result>
</action>
result配置类似于struts1中的forward,但struts2中提供了多种结果类型,常用的类型有: dispatcher(默认值)、 redirect 、 redirectAction 、 plainText。

在result中还可以使用${属性名}表达式访问action中的属性,表达式里的属性名对应action中的属性。如下:
<result type="redirect">/view.jsp?id=${id}</result>

下面是redirectAction 结果类型的例子,如果重定向的action中同一个包下:
<result type="redirectAction">helloworld</result>
如果重定向的action在别的命名空间下:
<result type="redirectAction">
 <param name="actionName">helloworld</param>
 <param name="namespace">/test</param>
</result>
plaintext:显示原始文件内容,例如:当我们需要原样显示jsp文件源代码 的时候,我们可以使用此类型。
<result name="source" type="plainText ">
 <param name="location">/xxx.jsp</param>
 <param name="charSet">UTF-8</param><!-- 指定读取文件的编码 -->


2.多个Action共享一个视图--全局result配置

当多个action中都使用到了相同视图,这时我们应该把result定义为全局视图。struts1中提供了全局forward,struts2中也提供了相似功能:
<package ....>
 <global-results>
  <result name="message">/message.jsp</result>
 </global-results>
</package>

3.为Action的属性注入值

Struts2为Action中的属性提供了依赖注入功能,在struts2的配置文件中,我们可以很方便地为Action中的属性注入值。注意:属性必须提供setter方法。
public class HelloWorldAction{
 private String savePath;

 public String getSavePath() {
  return savePath;
 }
 public void setSavePath(String savePath) {
  this.savePath = savePath;
 }
       ......
}

<package name="itcast" namespace="/test" extends="struts-default">
 <action name="helloworld" class="cn.itcast.action.HelloWorldAction" >
  <param name="savePath">/images</param>
  <result name="success">/WEB-INF/page/hello.jsp</result>
 </action>
</package>
上面通过<param>节点为action的savePath属性注入“/images”


4.指定需要Struts 2处理的请求后缀

前面我们都是默认使用.action后缀访问Action。其实默认后缀是可以通过常量”struts.action.extension“进行修改的,例如:我们可以配置Struts 2只处理以.do为后缀的请求路径:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "
http://struts.apache.org/dtds/struts-2.0.dtd ">
<struts>
    <constant name="struts.action.extension" value="do"/>
</struts>

如果用户需要指定多个请求后缀,则多个后缀之间以英文逗号(,)隔开。如:
 <constant name="struts.action.extension" value="do,go"/>

5.细说常量定义

常量可以在struts.xml或struts.properties中配置,建议在struts.xml中配置,两种配置方式如下:
在struts.xml文件中配置常量
<struts>
    <constant name="struts.action.extension" value="do"/>
</struts>

在struts.properties中配置常量
struts.action.extension=do

因为常量可以在下面多个配置文件中进行定义,所以我们需要了解struts2加载常量的搜索顺序:
struts-default.xml
struts-plugin.xml
struts.xml
struts.properties
web.xml
如果在多个文件中配置了同一个常量,则后一个文件中配置的常量值会覆盖前面文件中配置的常量值.

6.常用的常量介绍

<!-- 指定默认编码集,作用于HttpServletRequest的setCharacterEncoding方法 和freemarker 、velocity的输出 -->
    <constant name="struts.i18n.encoding" value="UTF-8"/>
    <!-- 该属性指定需要Struts 2处理的请求后缀,该属性的默认值是action,即所有匹配*.action的请求都由Struts2处理。
    如果用户需要指定多个请求后缀,则多个后缀之间以英文逗号(,)隔开。 -->
    <constant name="struts.action.extension" value="do"/>
    <!-- 设置浏览器是否缓存静态内容,默认值为true(生产环境下使用),开发阶段最好关闭 -->
    <constant name="struts.serve.static.browserCache" value="false"/>
    <!-- 当struts的配置文件修改后,系统是否自动重新加载该文件,默认值为false(生产环境下使用),开发阶段最好打开 -->
    <constant name="struts.configuration.xml.reload" value="true"/>
    <!-- 开发模式下使用,这样可以打印出更详细的错误信息 -->
    <constant name="struts.devMode" value="true" />
     <!-- 默认的视图主题 -->
    <constant name="struts.ui.theme" value="simple" />
    <!– 与spring集成时,指定由spring负责action对象的创建 -->
    <constant name="struts.objectFactory" value="spring" />
 <!–该属性设置Struts 2是否支持动态方法调用,该属性的默认值是true。如果需要关闭动态方法调用,则可设置该属性为false。 -->
<constant name="struts.enable.DynamicMethodInvocation" value="false"/>
 <!--上传文件的大小限制-->
<constant name="struts.multipart.maxSize" value=“10701096"/>

7.为应用指定多个struts配置文件

在大部分应用里,随着应用规模的增加,系统中Action的数量也会大量增加, 导致struts.xml配置文件变得非常臃肿。为了避免struts.xml文件过于庞大、臃肿,提高struts.xml文件的可读性,我们可以将一 个struts.xml配置文件分解成多个配置文件,然后在struts.xml文件中包含其他配置文件。下面的struts.xml通 过<include>元素指定多个配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "
http://struts.apache.org/dtds/struts-2.0.dtd ">
<struts>
 <include file="struts-user.xml"/>
 <include file="struts-order.xml"/>
</struts>

通过这种方式,我们就可以将Struts 2的Action按模块添加在多个配置文件中。

8.动态方法调用
如果Action中存在多个方法时,我们可以使用!+方法名调用指定方法。如下:
public class HelloWorldAction{
 private String message;
 ....
 public String execute() throws Exception{
  this.message = "我的第一个struts2应用";
  return "success";
 }
 
 public String other() throws Exception{
  this.message = "第二个方法";
  return "success";
 }
}
假设访问上面action的URL路径为: /struts/test/helloworld.action
要访问action的other() 方法,我们可以这样调用:
/struts/test/helloworld!other.action
如果不想使用动态方法调用,我们可以通过常量struts.enable.DynamicMethodInvocation关闭动态方法调用。
<constant name="struts.enable.DynamicMethodInvocation" value="false"/>


9. 使用通配符定义action

<package name="itcast" namespace="/test" extends="struts-default">
 <action name="helloworld_*" class="cn.itcast.action.HelloWorldAction" method="{1}">
  <result name="success">/WEB-INF/page/hello.jsp</result>
 </action>
</package>
public class HelloWorldAction{
 private String message;
 ....
 public String execute() throws Exception{
  this.message = "我的第一个struts2应用";
  return "success";
 }
 
 public String other() throws Exception{
  this.message = "第二个方法";
  return "success";
 }
}

要访问other()方法,可以通过这样的URL访问:/test/helloworld_other.action

10. 接收请求参数

采用基本类型接收请求参数(get/post)
在Action类中定义与请求参数同名的属性,struts2便能自动接收请求参数并赋予给同名属性。
请求路径:
http://localhost:8080/test/view.action?id=78
public class ProductAction {
      private Integer id;
      public void setId(Integer id) {//struts2通过反射技术调用与请求参数同名的属性的setter方法来获取请求参数值
             this.id = id;
      }
      public Integer getId() {return id;}
  }
采用复合类型接收请求参数
请求路径:
http://localhost:8080/test/view.action?product.id=78
 public class ProductAction {
   private Product product;
   public void setProduct(Product product) {  this.product = product;  }
   public Product getProduct() {return product;}
}
Struts2首先通过反射技术调用Product的默认构造器创建product对象,然后再通过反射技术调用product中与请求参数同名的属性的setter方法来获取请求参数值。


11.关于struts2.1.6接收中文请求参数乱码问题

struts2.1.6版本中存在一个Bug,即接收到的中文请求参数为乱码 (以post方式提交),原因是struts2.1.6在获取并使用了请求参数后才调用HttpServletRequest的 setCharacterEncoding()方法进行编码设置 ,导致应用使用的就是乱码请求参数。这个bug在struts2.1.8中已经被解决,如果你使用的是struts2.1.6,要解决这个问题,你可以这 样做:新建一个Filter,把这个Filter放置在Struts2的Filter之前,然后在doFilter()方法里添加以下代码

public void doFilter(...){
 HttpServletRequest req = (HttpServletRequest) request;
 req.setCharacterEncoding("UTF-8");//应根据你使用的编码替换UTF-8
 filterchain.doFilter(request, response);
}


12.自定义类型转换器

java.util.Date类型的属性可以接收格式为2009-07-20的请求参数值。但如果我们需要接收格式为20091221的请求参数,我们必须定义类型转换器,否则struts2无法自动完成类型转换。

import java.util.Date;
public class HelloWorldAction {
 private Date createtime;

 public Date getCreatetime() {
  return createtime;
 }

 public void setCreatetime(Date createtime) {
  this.createtime = createtime;
 }
}

13.自定义类型转换器
public class DateConverter extends DefaultTypeConverter {
                @Override  public Object convertValue(Map context, Object value, Class toType) {
 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
 try {
  if(toType == Date.class){//当字符串向Date类型转换时
   String[] params = (String[]) value;// Request.getParameterValues()
   return dateFormat.parse(params[0]);
  }else if(toType == String.class){//当Date转换成字符串时
   Date date = (Date) value;
   return dateFormat.format(date);
  }
 } catch (ParseException e) {}
 return null;
 }
}
将上面的类型转换器注册为局部类型转换器:
在 Action类所在的包下放置ActionClassName-conversion.properties文件,ActionClassName是 Action的类名,后面的-conversion.properties是固定写法,对于本例而言,文件的名称应为HelloWorldAction- conversion.properties 。在properties文件中的内容为:
属性名称=类型转换器的全类名
对于本例而言, HelloWorldAction-conversion.properties文件中的内容为:
createtime= cn.itcast.conversion.DateConverter

15.自定义全局类型转换器
将上面的类型转换器注册为全局类型转换器:
在WEB-INF/classes下放置xwork-conversion.properties文件 。在properties文件中的内容为:
待转换的类型=类型转换器的全类名
对于本例而言, xwork-conversion.properties文件中的内容为:
java.util.Date= cn.itcast.conversion.DateConverter

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics