Struts2知识总结2

六、Action中result的各种转发类型
<action name="helloword" class="com.rlcc.action.HelloWordAction" method="excute">
    <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.jspid=${id}</result>

下面是redirectAction结果类型的例子,如果重定向的action在同一个 包下:
<result type="redirectAction">helloworld</result>
如果重定向的action在别的命名空间下:
<result type="redirectAction">
    <param name="actionName">helloworld</param>
    <param name="namespace">/test</param>
  </result>

注意:
1、重定向(浏览器重定向),是不能访问到WEB-INF目录里面的文件的,所以在做该项配置时,重定向的页面不能放在/WEB-INF/目录里面。
2、/view.jspid=${id},参数值为中文时的编码问题
id 如果是中文,则先进行URL编码:URLEncoder.encode(“用户名”,"UTF-8");,在获取该参数时,进行解码。tomcat接收url中的中文参数串时,先把该中文参数串进行ISO8859-1编码,所以显示时解码时得先用ISO8859-1编码方式把参数串还原成原始的字节数组,再组成UTF-8编码方式的字符串,然后进行URL解码;
<%=URLDecoder.decode(new String(request.getParameter("username").getBytes("ISO8859-1"),"UTF-8"),"UTF-8")%>
  plaintext:显示原始文件内容,例如:当我们需要原样显示jsp文件源代码的时候,我们可以使用此类型。
<result name="source" type="plainText">
    <param name="location">/xxx.jsp</param>
    <param name="charSet">UTF-8</param><!--指定读取文件的编码-->
</result>

七、为Action的属性注入值
Struts2为Action中的属性提供了依赖注入功能,在struts2的配置文件中,我们可以很方便地为Action中的属性注入值。注意:属性必须提供setter方法。
public calss HelloWorldAction{
      private String savePath;
      public String getSavePath(){
        return savePath;
      }
      public void setSavePath(String savePath){
        this.savePath = savePath;
      }
      ......
    }

    <package name="one" namespace="/first" extends="struts-default">
      <action name="helloworld" class="com.rlcc.action.HelloWorldAction">
        <param name="savePath">/images</param>
<result name="success">/page/hello.jsp</result>
      </action>
    </package>

八、指定需要Struts2处理的请求后缀
1、前面我们都是默认使用.action后缀访问Action。其实默认后缀是可以通过常量“struts.action.extension”进行修改的,例如:我们可以配置Struts2只处理以.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"/>

     2、细说常量定义
       常量可以在struts.xml或struts.properties中配置,建议在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
如果在多个文件中配置了同一个常量,则后一个文件中配置的常量值会覆盖前面文件中配置的常量值。

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

九、struts2的处理流程

  用户请求------>StrutsPrepareAndExecuteFilter-->Interceptor-->Action-->Result-->Jsp/html------>响应
StrutsPrepareAndExecuteFilter是Struts2框架的核心控制器,它负责拦截由<url-pattern>/*</url-pattern>指定的所有用户请求,当用户请求到达时,该Filter会过滤用户的请求。默认情况下,如果用户请求的路径不带后缀或者后缀以.action结尾,这时请求将被转入Struts2框架处理,否则Struts2框架将略过该请求的处理。当请求转入Struts2框架处理时会先经过一系列的拦截器,然后再到Action。与Strutss1不同,Struts2对用户的每一次请求都会创建一个Action,所以Struts2中的Action是线程安全的。

十、为应用指定多个struts配置文件
  在大部分应用理,随着应用规模的增加,系统中Action的数量也会大量增加,导致struts.xml配置文件变得非常臃肿。为了避免struts.xml文件分解成多个配置文件,然后在struts.xml文件中包含其他配置文件。
<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>
通过这种方式,我们就可以将Struts2的Action按模块添加在多个配置文件中。

十一、动态方法调用
1、如果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"/>

附:建议不使用动态方法调用。

2、使用通配符定义action
    <package name="first" namespace="/first" extends="base">
      <action name="helloworld_*" class="com.rlcc.action.HelloWorldAction" method="{1}">
        <result name="success">/WEB-INF/page/hello.jsp</result>
    </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访问:/first/helloworld_other.action

十二、接收请求参数
1、采用基本类型接收请求参数(get/post)
    在Action类中定义与请求参数同名的属性,struts2便能自动接收请求参数并赋予同名属性。
    请求路径:http://localhost/test/view.actionid=78
    public class ProductAction{
      private Integer id;
      public void setId(Integer id){
        //struts2通过反射技术调用与请求参数同名的属性的setter方法来获取请求参数值
this.id = id;
      }
      public Integer getId(){
        return id;
      }
    }

  2、采用复合类型接收请求参数
    请求路径:http://localhost/test/view.actionid=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方法来获取请求参数值。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值