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


    4:配置各项默认值:详解如下所示  配置全局跳转视图,先去action的result找,如果没有就去全局视图找:

    <global-results>
          <result name="success">success.jsp</result>
    </global-results>
     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>

    5:Struts2的Action的开发的几种方式
        (1):方式一,继承ActionSupport,最经常使用的。
               如果用struts的数据校验功能,必须继承此类;
        (2):方式二,实现Action接口
        (3):方式三,手动写;


     6:使用通配符进行配置Action(在struts中配置信息中,可以用*和{1}优化配置;),如下所示:

     1 package com.bie.lesson03;
     2 
     3 import com.opensymphony.xwork2.ActionSupport;
     4 
     5 /** 
     6 * @author  Author:别先生 
     7 * @date Date:2017年9月10日 下午10:22:34 
     8 *
     9 *
    10 */
    11 public class StrutsConfig extends ActionSupport{
    12 
    13     /**
    14      * 
    15      */
    16     private static final long serialVersionUID = 1L;
    17 
    18     public String login() {
    19         
    20         System.out.println("模拟的登陆的方法");
    21         
    22         return SUCCESS;
    23     }
    24     
    25     public String register(){
    26         
    27         System.out.println("模拟的注册的方法");
    28         
    29         return SUCCESS;
    30     }
    31     
    32 }

     然后配置strutsConfig.xml配置文件,注意这里的Action使用了占位符进行配置:

     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="strutsPackage03" extends="struts-default">
     9         <!-- 
    10             定义action
    11             name:访问路径
    12             class:类的路径
    13             method:方法名称 
    14          -->
    15         <action name="user_*" class="com.bie.lesson03.StrutsConfig" method="{1}">
    16             <result name="success">success.jsp</result>
    17         </action>
    18         
    19     </package>
    20 
    21 </struts>

    然后在总的配置文件struts.xml中引入配置文件strutsConfig.xml即可,如下所示:

     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     <!-- struts在允许的时候会加载这个总配置文件:src/struts.xml -->
     8     
     9     <!-- 在总配置文件中引入其他所有的配置文件 -->
    10     
    11     <include file="com/bie/lesson03/strutsConfig.xml"></include>
    12 </struts>

    访问的时候如下所示:

    对比一下可以很清晰的发现他们之间的区别,这就是使用了占位发简化开发;


    7:Struts的常量
            Struts1中默认访问后缀是*.do
            Struts2中默认访问后缀是*action

      如何修改默认访问后缀:
          (1):Struts的访问后缀在哪里定义:
               在default.properties文件中
              默认访问后缀:struts.action.extension=action,,

     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     <!-- struts在允许的时候会加载这个总配置文件:src/struts.xml -->
     8     
     9     <!-- 全局配置 -->
    10     <!-- 修改Struts的默认访问后缀 -->
    11     <constant name="struts.action.extension" value="action,do,struts"></constant>
    12     
    13     
    14 </struts>

     8:配置的顺序书写,?代表0或者1,*代表0或者多:

    The content of element type "package" must match "(

      result-types?,

      interceptors?,

      default-interceptor-ref?,

      default-action-ref?,

      default-class-ref?,

      global-results?,

      global-exception-mappings?,

      action*)".

     

  • 相关阅读:
    hbase 学习笔记一---基本概念
    hdu 4496 (并差集)
    rqnoj-105-核电站问题-dp
    面试之BI-SQL--table转换
    Android:从程序员到架构师之路Ⅲ_高焕堂
    Linux 的进程组、会话、守护进程
    Oracle创建dblink报错:ORA-01017、ORA-02063解决
    JSP 指令
    JSP 生命周期
    JSP 结构
  • 原文地址:https://www.cnblogs.com/biehongli/p/6420594.html
Copyright © 2011-2022 走看看