码迷,mamicode.com
首页 > 其他好文 > 详细

Struts2的动态Action和默认后缀.action

时间:2017-02-20 18:24:30      阅读:239      评论:0      收藏:0      [点我收藏+]

标签:als   大小   static   factory   时间   name   3.0   constant   print   

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属性加!后面是方法名即可访问。)详细如下图所示:

技术分享

技术分享

技术分享

 

Struts2的动态Action和默认后缀.action

标签:als   大小   static   factory   时间   name   3.0   constant   print   

原文地址:http://www.cnblogs.com/biehongli/p/6420594.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!