struts2流程 应用

来源:互联网 发布:黑人爆炸头软件 编辑:程序博客网 时间:2024/06/05 06:00

1、 多个Action共享一个视图--全局result配置

1)当多个action中都使用到了相同视图,这时我们应该把result定义为全局视图。(2struts1中提供了全局forwardstruts2中也提供了相似功能:

<package ....>

       <global-results>

              <resultname="message">/message.jsp</result>

       </global-results>

</package>

2、 Action的属性注入值

1Struts2Action中的属性提供了依赖注入功能,在struts2的配置文件中,我们可以很方便地为Action中的属性注入值。注意:属性必须提供set方法。

2<paramname="savePath">/images</param>:通过<param>节点为actionsavePath属性注入“/images”

3、指定需要Struts 2处理的请求后缀

1)前面默认使用.action后缀访问Action

2)默认后缀是可以通过常量”struts.action.extension“进行修改的

<struts>

    <constantname="struts.action.extension" value="do"/>

</struts>

3)如果用户需要指定多个请求后缀,则多个后缀之间以英文逗号(,)隔开

4常量定义

1)常量可在struts.xmlstruts.properties中配置,建议在struts.xml中配置

2)两种配置方式如下:

①在struts.xml文件中配置常量

<struts>

             <constantname="struts.action.extension" value="do"/>

</struts>

②在struts.properties中配置常量

struts.action.extension=do

3)因为常量可以在下面多个配置文件中进行定义,

所以需要了解struts2加载常量的搜索顺序:

struts-default.xml

struts-plugin.xml

struts.xml

struts.properties

web.xml

4)如果在多个文件中配置了同一个常量,则后一个文件中配置的常量值会覆盖前面文件中配置的常量值.

5、常用的常量

1<!--指定默认编码集,作用于HttpServletRequestsetCharacterEncoding方法和freemarkervelocity的输出 -->

    <constantname="struts.i18n.encoding" value="UTF-8"/>

2 <!--该属性指定需要Struts 2处理的请求后缀,该属性的默认值是action,即所有匹配*.action的请求都由Struts2处理。

3)如果用户需要指定多个请求后缀,则多个后缀之间以英文逗号(,)隔开。 -->

    <constantname="struts.action.extension" value="do"/>

4 <!--设置浏览器是否缓存静态内容,默认值为true(生产环境下使用),开发阶段最好关闭 -->

    <constantname="struts.serve.static.browserCache" value="false"/>

5<!--struts的配置文件修改后,系统是否自动重新加载该文件,默认值为false(生产环境下使用),开发阶段最好打开 -->

    <constantname="struts.configuration.xml.reload" value="true"/>

6  <!--开发模式下使用,这样可以打印出更详细的错误信息 -->

    <constantname="struts.devMode" value="true" />

7 <!--默认的视图主题 -->

    <constantname="struts.ui.theme" value="simple" />

8<!–与spring集成时,指定由spring负责action对象的创建 -->

    <constantname="struts.objectFactory" value="spring" />

9<!–该属性设置Struts 2是否支持动态方法调用,该属性的默认值是true。如果需要关闭动态方法调用,则可设置该属性为false -->

<constant name="struts.enable.DynamicMethodInvocation"value="false"/>

10<!--上传文件的大小限制-->

<constant name="struts.multipart.maxSize" value=10701096"/>

6、Struts2的处理流程

流程说明:

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

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

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

struts.xml通过<include>元素指定多个配置文件:

<struts>

       <includefile="struts-user.xml"/>

       <includefile="struts-order.xml"/>

</struts>

8动态方法调用

1)如果Action中存在多个方法时,我们可以使用!+方法名调用指定方法。

例子:

public classHelloWorldAction{

       private String message;

       ....

       public String execute() throws Exception{

              this.message = "我的第一个struts2应用";

              return "success";

       }

       public String other() throws Exception{

              this.message = "第二个方法";

              return "success";

       }

}

假设actionURL路径为: /struts/test/helloworld.action

要访问actionother()方法,我们可以这样调用:

/struts/test/helloworld!other.action 这就是动态方法调用。

如不想使用动态方法调用,可以通过常量struts.enable.DynamicMethodInvocation关闭动态方法调用。

<constantname="struts.enable.DynamicMethodInvocation"value="false"/>

9使用通配符定义action

<packagename=csdn namespace="/test"extends="struts-default">

       <action name="helloworld_*"class="cn.csdn.action.HelloWorldAction" method="{1}">

              <resultname="success">/WEB-INF/page/hello.jsp</result>

       </action>

</package>

public classHelloWorldAction{

       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