Struts2的动态Action和全局跳转视图以及配置各项默认值

1:Struts2的默认访问后缀是.action(特别需要注意的是改了配置文件web.xml或者struts.xml需要重启服务器)


 2:Struts2中常用的常量介绍:
<!-- 一:全局配置 -->

<!--1.请求数据编码  -->
<constant name="struts.i18n.encoding" value="UTF-8"/>
<!--2.修改struts2默认的自定义后缀 -->
<constant name="struts.action.extension" value="action,do,"/>
<!--3.设置浏览器是否缓存静态内容,默认值为true(生产环境下使用),开发阶段最好关闭   -->
<constant name="struts.serve.static.browserCache" value="false"/>
<!--4.当struts的配置文件修改后,系统是否自动重新加载该文件,默认值为false(生产环境下使用),开发阶段最好打开   -->
<constant name="struts.configuration.xml.reload" value="true"/>
<!--5.开发模式下使用,这样可以打印出更详细的错误信息  -->
<constant name="struts.devMode" value="true" />
<!--6.默认的视图主题  -->
<constant name="struts.ui.theme" value="simple" />
<!--7.与spring集成时,指定由spring负责action对象的创建   -->
<constant name="struts.objectFactory" value="spring" />
<!--8.该属性设置Struts 2是否支持动态方法调用,该属性的默认值是true。如果需要关闭动态方法调用,则可设置该属性为 false -->
<constant name="struts.enable.DynamicMethodInvocation" value="true"/>
<!--9.上传文件的大小限制 -->
<constant name="struts.multipart.maxSize" value="10701096"/>


 3:Struts2的动态Action的简单应用和多个.xml的使用:

第一步:引包,略去

第二步:配置web.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
 3   <display-name>struts2_20170219</display-name>
 4   <welcome-file-list>
 5     <welcome-file>index.html</welcome-file>
 6     <welcome-file>index.htm</welcome-file>
 7     <welcome-file>index.jsp</welcome-file>
 8     <welcome-file>default.html</welcome-file>
 9     <welcome-file>default.htm</welcome-file>
10     <welcome-file>default.jsp</welcome-file>
11   </welcome-file-list>
12   
13   <!-- 引入struts2的核心过滤器 -->
14   <filter>
15       <!-- 过滤器的名称 -->
16       <filter-name>struts2</filter-name>
17       <!-- 过滤器类 -->
18       <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
19   </filter>
20   <filter-mapping>
21       <!-- 过滤器名称 -->
22       <filter-name>struts2</filter-name>
23       <!-- 过滤器映射 -->
24       <url-pattern>/*</url-pattern>
25   </filter-mapping>
26 </web-app>

第三步:开发第一个Action,配置第一个struts01.xml文件

 1 package com.bie.struts01;
 2 
 3 import com.opensymphony.xwork2.ActionSupport;
 4 
 5 /** 
 6 * @author BieHongLi 
 7 * @version 创建时间:2017年2月19日 下午3:08:53 
 8 * 开发action,处理请求
 9 */
10 public class HelloAction extends ActionSupport{
11     
12     private static final long serialVersionUID = 1L;
13     
14     /**
15      * 重写execute,处理请求的方法
16      */
17     @Override
18     public String execute() throws Exception {
19         System.out.println("访问到了action,正在 处理请求");
20         System.out.println("hello world!!! struts2");
21         return SUCCESS;
22     }
23     
24 }
 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE struts PUBLIC
 3     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
 4     "http://struts.apache.org/dtds/struts-2.0.dtd">
 5 
 6 <struts>
 7     <!-- 声明包 -->
 8     <package name="helloPackage" abstract="false" extends="struts-default">
 9         <action name="helloAction" class="com.bie.struts01.HelloAction">
10             <result name="success">success.jsp</result>
11         </action>
12     </package>
13     
14     
15 </struts>

第四步:开发第二个Action,配置第二个struts02.xml文件

 1 package com.bie.struts02;
 2 
 3 import com.opensymphony.xwork2.ActionSupport;
 4 
 5 /** 
 6 * @author BieHongLi 
 7 * @version 创建时间:2017年2月20日 下午4:05:38 
 8 * 
 9 */
10 public class TestAction extends ActionSupport{
11 
12     private static final long serialVersionUID = 1L;
13     
14     public String test(){
15         System.out.println("测试的方法!!!");
16         return SUCCESS;
17     }
18     
19 }
 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE struts PUBLIC
 3     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
 4     "http://struts.apache.org/dtds/struts-2.0.dtd">
 5 
 6 <struts>
 7     <!-- 声明包 -->
 8     <package name="testPackage" abstract="false" extends="struts-default">
 9         <!-- 动态方法调用的格式如:http://localhost:8080/struts2_20170219/testAction!test -->
10         <action name="testAction" class="com.bie.struts02.TestAction">
11             <result name="success">success.jsp</result>
12         </action>
13     </package>
14     
15     
16 </struts>

第五步:配置struts2的全局变量以及总struts.xml文件;

需要注意的是动态Action默认是不使用的,将false改为true即可使用动态Action。

<?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>
    
    <!-- 一:全局配置 -->
    
    <!--1.请求数据编码  -->
    <constant name="struts.i18n.encoding" value="UTF-8"/>
    <!--2.修改struts2默认的自定义后缀 -->
    <constant name="struts.action.extension" value="action,do,"/>
    <!--3.设置浏览器是否缓存静态内容,默认值为true(生产环境下使用),开发阶段最好关闭   -->
    <constant name="struts.serve.static.browserCache" value="false"/>
    <!--4.当struts的配置文件修改后,系统是否自动重新加载该文件,默认值为false(生产环境下使用),开发阶段最好打开   -->
    <constant name="struts.configuration.xml.reload" value="true"/>
       <!--5.开发模式下使用,这样可以打印出更详细的错误信息  -->
    <constant name="struts.devMode" value="true" />
    <!--6.默认的视图主题  -->
    <constant name="struts.ui.theme" value="simple" />
    <!--7.与spring集成时,指定由spring负责action对象的创建   -->
    <constant name="struts.objectFactory" value="spring" />
       <!--8.该属性设置Struts 2是否支持动态方法调用,该属性的默认值是true。如果需要关闭动态方法调用,则可设置该属性
        为 false -->
    <constant name="struts.enable.DynamicMethodInvocation" value="true"/>
    <!--9.上传文件的大小限制 -->
    <constant name="struts.multipart.maxSize" value="10701096"/>
    
</struts>
 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE struts PUBLIC
 3     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
 4     "http://struts.apache.org/dtds/struts-2.0.dtd">
 5 
 6 <struts>
 7     
 8     <!-- Struts2的全局配置,必须写在最上面,格式如下所示 -->
 9     <include file="constant.xml"></include>
10     
11     <!-- 总配置文件,引入其他所有的配置文件 ,引入其他的配置文件需要注意的是/不是.  -->
12     <include file="com/bie/struts01/struts01.xml"></include>
13     <include file="com/bie/struts02/struts02.xml"></include>
14     
15     
16 </struts>

运行效果如下所示:(注意:动态Action的访问是action的name属性加!后面是方法名即可访问。)详细如下图所示:


配置各项默认值:详解如下所示

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE struts PUBLIC
 3     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
 4     "http://struts.apache.org/dtds/struts-2.0.dtd">
 5 
 6 <struts>
 7     <!-- 声明包 -->
 8     <package name="helloPackage" abstract="false" extends="struts-default">
 9         <!-- 配置全局跳转视图 -->
10         <global-results>
11             <result name="success">success.jsp</result>
12         </global-results>
13         
14         <action name="helloAction" class="com.bie.HelloAction">
15             <!-- <result name="success">success.jsp</result> -->
16         </action>
17         
18         <action name="worldAction" class="com.bie.WorldAction">
19             <!-- <result name="success">success.jsp</result> -->
20             <!-- 返回结果标记success对应的页面在当前action中没有配置,那么会
21                  会去找全局配置是否有success标记对应的页面 ,如果全局配置也没有
22               success标记对应的页面,那么就报404错误。-->
23         </action>
24         
25         
26         <!-- 配置各项默认值 -->
27         <!-- 
28             1:name 只配置了访问路径名称
29             2:class 默认执行得action在struts-default有配置
30                 <default-class-ref class="com.opensymphony.xwork2.ActionSupport" />
31              3:method默认是execute
32                  默认得方法execute返回值为success,对应页面去全局视图找,
33                  如果全局试图没,那么报404错误。
34          -->
35         <action name="test01"></action>
36     </package>
37     
38     
39 </struts>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值