zoukankan      html  css  js  c++  java
  • JavaWeb学习:Struts2配置

    一、Struts2配置

      通过Struts2入门了解到Struts2需要使用过滤器,那么就必须配置过滤器的配置(web.xml),既然使用的是Struts2,当然也少不了Struts2本身的配置了(struts.xml)

      关于配置过滤器(web.xml)可参照https://www.cnblogs.com/WarBlog/p/13434453.html

    二、struts.xml配置

      ①、package:包与Java中的包的概念不一致。它为了更好管理action的配置。

        <!--name随意,extends自struts-default -->
        <package name="strutsTest" extends="struts-default">
            <!-- action节点,name为地址栏输入时用的名字,class为class文件的位置 -->
            <action name="hello" class="com.struts2.demo.HelloAction">
                <!-- name 为刚才编写的action类中,execute返回的值       index.jsp的意思是返回到index.jsp页面 -->
                <result name="success">success.jsp</result>
            </action>
        </package>
      • name   :包的名称,只有在一个项目中不重名即可。
      • extends:继承哪个包,通常值为struts-default。
      • namespace:名称空间,与<action>标签中的name属性共同决定访问路径。
        • 名称空间有三种写法:
          • 带名称的名称空间:namespace=”/aaa”
          • 跟名称空间:namespance=”/”
          • 默认名称空间 :namespace=””
      • abstract                      :抽象的,用于其他包的继承。

      ②、action:配置相应的Action类

      • name  :与namespace共同决定访问路径
      • class  :Action类的全路径
      • method  :执行Action中的哪个方法的方法名,默认值execute
      • converter  :用于设置类型转换器

    三、常量配置

      ①、常量默认值配置在default.properties

        struts.i18n.encoding=UTF-8                                       ----Struts2中所有的post请求的中文乱码不用处理。

        struts.action.extension=action,,                                  ----Struts2请求的默认的扩展名。默认扩展名是.action或者什么都不写

      ②、修改常量值,三个地方

        2.1、struts.xml中进行修改      

    <constant name="struts.action.extension" value="action" />

        2.2、struts.properties:在src下新建一个struts.properties文件,用于常量修改

    struts.action.extension=action

        2.3、web.xml中修改常量,通过过滤器中的初始化参数

        <filter>
            <filter-name>struts2</filter-name>
            <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
    
            <init-param>
                <param-name>struts.action.extension</param-name>
                <param-value>action</param-value>
            </init-param>
        </filter>
    
        <filter-mapping>
            <filter-name>struts2</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>

    四、includ标签:用于引入不同功能模块的xml文件

      

       ①、struts_hello.xml内容

    <?xml version="1.0" encoding="UTF-8" ?>
    
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">
    
    <struts>
        <!--新建一个package,name随意,extends自struts-default -->
        <package name="strutsTest" extends="struts-default" namespace="/">
            <!-- 编写action,name为地址栏输入时用的名字,class为class文件的位置 -->
            <action name="hello" class="com.struts2.demo.HelloAction">
                <!-- name 为刚才编写的action类中,execute返回的值       index.jsp的意思是返回到index.jsp页面 -->
                <result name="success">success.jsp</result>
            </action>
        </package>
    </struts>

      ②、struts.xml内容

    <?xml version="1.0" encoding="UTF-8" ?>
    
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">
    
    <struts>
        <constant name="struts.action.extension" value="action" />
        
        <include file="com/struts2/demo/struts_hello.xml"></include>
    </struts>

    五、Action创建方式

      

      ①、POJO(简单Java类)

        实现方式:

          1.1、创建类,定义一个方法(public String execute())

    public class ActionDemo1 {
        public String execute() {
        System.out.println("ActionDemo1 执行... ...");
        return null;//return null 就是没有页面跳转
        }
    }

          1.2、配置模块的xml文件(struts_demo2.xml)

    <?xml version="1.0" encoding="UTF-8" ?>
    
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">
    
    <struts>
        <package name="strutsdemo" extends="struts-default" namespace="/">
            <action name="actionDemo1" class="com.struts2.demo2.ActionDemo1"></action>
        </package>
    </struts>

          1.3、配置struts.xml文件

    <?xml version="1.0" encoding="UTF-8" ?>
    
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">
    
    <struts>
        <constant name="struts.action.extension" value="action" />
        
        <include file="com/struts2/demo/struts_hello.xml"></include>
        <include file="com/struts2/demo2/struts_demo2.xml"></include>
    </struts>

      ②、实现Action接口的类(struts.xml的配置和①中配置一样)   

        实现Action接口提供了五个常量(五个逻辑视图名)

          SUCCESS:成功跳转

          ERROR:失败跳转

          LOGIN:登录失败跳转

          INPUT:表单校验时,出错

          NONE:不跳转

        2.1、创建类

    import com.opensymphony.xwork2.Action;
    
    /**
    * @Title: ActionDemo2
    * @Description: 
    * @author: marw
    * @date 2020/11/23 15:57:41
    */
    public class ActionDemo2 implements Action {
    
        @Override
        public String execute() throws Exception {
        System.out.println("ActionDemo2 执行... ...");
        return NONE;
        }
    }

        2.2、配置struts_demo2.xml

    <?xml version="1.0" encoding="UTF-8" ?>
    
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">
    
    <struts>
        <package name="strutsdemo" extends="struts-default" namespace="/">
            <action name="actionDemo1" class="com.struts2.demo2.ActionDemo1"></action>
            <action name="actionDemo2" class="com.struts2.demo2.ActionDemo2"></action>
        </package>
    </struts>   

      ③、继承ActionSupport的类(struts.xml的配置和①中配置一样)

        3.1、创建类

    import com.opensymphony.xwork2.ActionSupport;
    
    /**
     * @Title: ActionDemo3
     * @Description:
     * @author: marw
     * @date 2020/11/23 16:10:31
     */
    public class ActionDemo3 extends ActionSupport {
        @Override
        public String execute() throws Exception {
        System.out.println("ActionDemo3 执行... ...");
        return NONE;
        }
    }

        3.2、配置struts_demo2.xml

    <?xml version="1.0" encoding="UTF-8" ?>
    
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">
    
    <struts>
        <package name="strutsdemo" extends="struts-default" namespace="/">
            <action name="actionDemo1" class="com.struts2.demo2.ActionDemo1"></action>
            <action name="actionDemo2" class="com.struts2.demo2.ActionDemo2"></action>
            <action name="actionDemo3" class="com.struts2.demo2.ActionDemo3"></action>
        </package>
    </struts>

    六、Action访问

      

        访问地址:http://localhost:8080/Struts2Demo/demo/index.jsp

      struts.xml

    <struts>
        <include file="com/struts2/demo3/struts_demo3.xml"></include>
    </struts>

      ①、method

        1.1、index.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
        <h1>method</h1>
        <h3><a href="${ pageContext.request.contextPath}/userfind.action">查询用户</a></h3>
        <h3><a href="${ pageContext.request.contextPath}/userupdate.action">修改用户</a></h3>
    </body>
    </html>

        1.2、新建UserAction

    import com.opensymphony.xwork2.ActionSupport;
    
    /**
     * @Title: UserAction
     * @Description:
     * @author: marw
     * @date 2020/11/23 17:07:58
     */
    public class UserAction extends ActionSupport {
        
        public String find() {
        System.out.println("UserAction find ");
        return NONE;
        }
        
        public String update() {
        System.out.println("UserAction update ");
        return NONE;
        }
    }

        1.3、struts_demo3.xml

    <?xml version="1.0" encoding="UTF-8" ?>
    
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">
    
    <struts>
        <package name="strutsdemo" extends="struts-default" namespace="/">
            <action name="userfind" class="com.struts2.demo3.UserAction" method="find"></action>
            <action name="userupdate" class="com.struts2.demo3.UserAction" method="update"></action>
        </package>
    </struts>

      ②、通配符(推荐)

        2.1、index.jsp

    <h1>通配符</h1>
        <h3><a href="${ pageContext.request.contextPath}/product_find.action">查询商品</a></h3>
        <h3><a href="${ pageContext.request.contextPath}/product_update.action">修改商品</a></h3>

        2.2、创建ProductAction类

    import com.opensymphony.xwork2.ActionSupport;
    
    /**
    * @Title: ProductAction
    * @Description: 
    * @author: marw
    * @date 2020/11/23 17:41:13
    */
    public class ProductAction extends ActionSupport {
        public String find() {
        System.out.println("ProductAction find ");
        return NONE;
        }
        
        public String update() {
        System.out.println("ProductAction update ");
        return NONE;
        }
    }

        2.3、配置struts_demo3.xml

    <!-- * :任意字符 ,{n}:第n个*的任意字符 -->
            <action name="product_*" class="com.struts2.demo3.ProductAction" method="{1}"></action>

        出现异常:There is no Action mapped for namespace [/] and action name [product_find] associated with context path [/Struts2Demo].

        调查结果:是因为struts2.5为了提高安全性,所以需要添加 <global-allowed-methods>regex:.*</global-allowed-methods>

      <package name="strutsdemo" extends="struts-default" namespace="/">
            <global-allowed-methods>regex:.*</global-allowed-methods>
            <action name="userfind" class="com.struts2.demo3.UserAction" method="find"></action>
            <action name="userupdate" class="com.struts2.demo3.UserAction" method="update"></action>
            <!-- * :任意字符 ,{n}:第n个*的任意字符 -->
            <action name="product_*" class="com.struts2.demo3.ProductAction" method="{1}">
            </action>
        </package>

          如果不想使用上面的方式也可以在action标签中添加<allowed-methods>标签,指定具体可访问的方法

            <action name="product_*" class="com.struts2.demo3.ProductAction" method="{1}">
                <allowed-methods>find,update</allowed-methods>
            </action>

        注意:

          1、<global-allowed-methods>regex:.*</global-allowed-methods>需要添加在所有action标签之前

          2、<allowed-methods>当前Action(如:ProductAction)中的方法名</allowed-methods>

    *** 抽象通配符(所有的Action都不用配置了)

      <!--<h3><a href="${ pageContext.request.contextPath}/ProductAction_find.action">查询商品</a></h3> -->
            <action name="*_*" class="com.struts2.demo3.{1}" method="{2}"></action>

      ③、动态方法访问

        3.1、在org.apache.struts2包找到default.properties中的struts.enable.DynamicMethodInvocation = false

        3.2、修改struts.enable.DynamicMethodInvocation = true(在struts.xml中添加constant标签内容如下)   

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

        3.3、新建CustomerAction

    import com.opensymphony.xwork2.ActionSupport;
    
    /**
    * @Title: CustomerAction
    * @Description: 
    * @author: marw
    * @date 2020/11/24 10:07:49
    */
    public class CustomerAction extends ActionSupport {
        public String find() {
           System.out.println("CustomerAction find ");
           return NONE;
           }
           
           public String update() {
           System.out.println("CustomerAction update ");
           return NONE;
           }
    }

        3.4、配置struts_demo3.xml

    <action name="customer" class="com.struts2.demo3.CustomerAction"></action>

        3.5、index.jsp编写访问方式

        <h1>动态方法访问</h1>
        <h3><a href="${ pageContext.request.contextPath}/customer!find.action">查询客户</a></h3>
        <h3><a href="${ pageContext.request.contextPath}/customer!update.action">修改客户</a></h3>

        出现异常:Method find for action customer is not allowed!

        解决方案:和②异常的解决方案一样

  • 相关阅读:
    从迷宫终点出发——Leo鉴书36
    OCP-1Z0-053-V13.02-238题
    OCP-1Z0-053-V13.02-233题
    OCP-1Z0-053-V13.02-232题
    OCP-1Z0-053-V13.02-228题
    OCP-1Z0-053-V13.02-226题
    OCP-1Z0-053-V13.02-225题
    OCP-1Z0-053-V13.02-221题
    OCP-1Z0-053-V13.02-219题
    OCP-1Z0-053-V13.02-216题
  • 原文地址:https://www.cnblogs.com/WarBlog/p/14012239.html
Copyright © 2011-2022 走看看