Struts2学习(三)【主配置文件配置_常量配置_动态方法配置_action书写方式】

一、主配置文件 struts.xml 配置

1.1 struts.xml配置文件示例

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
        "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
    <package name="test" namespace="/test" extends="struts-default">
        <action name="TestDemo1Action" class="com.qwm.struts2_1.testdemo.TestDemo1Action" method="test1">
            <result name="success">/hello.jsp</result>
        </action>
    </package>
    <include file="com/qwm/struts2_1/dynamic/struts.xml"/>
</struts>

1.2 package

<!--
    package:将action配置封装,就是可以在packege中配置很多action
        name:给包起个名字,起到标识的作用,可以任意取名,不能与其他包重名
        namespace:给actionde的访问路径定义一个命名空间
        extends:继承一个指定包 默认继承 struts-default
        abstract:包是否为抽象的,标示性属性,标识该包不能独立运行,专为继承
-->
<package name="test" namespace="/test" extends="struts-default">
</package>

1.3 action

<!--
    action元素:配置action类
        name:决定了action访问资源名
        class:action的完成类名
        method:制定调用action中的哪个方法来处理
-->
<action name="TestDemo1Action" class="com.qwm.struts2_1.testdemo.TestDemo1Action" method="test1">
    <result name="success">/hello.jsp</result>
</action>

1.4 result

<!--
    action元素:配置action类
        name:决定了action访问资源名
        class:action的完成类名
        method:制定调用action中的哪个方法来处理
-->
<action name="TestDemo1Action" class="com.qwm.struts2_1.testdemo.TestDemo1Action" method="test1">
    <!--
       result元素:结果配置
          name:标识结果处理的名字,与action方法的返回值对应
          type:制定调用哪一个result类来处理结果,默认使用转发
          标签体:填写页面的相对路径
    -->
    <result name="success">/hello.jsp</result>
</action>

二、常量配置

2.1 Struts2默认常量配置文件的位置

Struts2默认常量配置文件的位置 是在我们的Struts2的核心包内。比如我的版本是 2.5.13

我们的核心包是struts2-core-2.5.13.jar,我的Struts2默认常量配置文件的位置是

struts2-core-2.5.13.jar!\org\apache\struts2\default.properties

图示

image

2.2 常量配置方式一struts.xml中配置

配置常量的标签是 constant

比如我我们现在要来配置 国际化(i18n)的编码为utf-8。

打开我们的 src/struts.xml 文件,在struts标签下配置 i18n 的编码。配置如下

<!--国际化编码配置,可以解决POST请求乱码-->
<constant name="struts.i18n.encoding" value="UTF-8"/>

我们的 struts.xml 配置文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
        "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
    <!--开启开发模式-->
    <constant name="struts.devMode" value="true"/>
    <!--国际化编码配置,可以解决POST请求乱码-->
    <constant name="struts.i18n.encoding" value="UTF-8"/>
    <!--
        package:将action配置封装,就是可以在packege中配置很多action
            name:给包起个名字,起到标识的作用,可以任意取名,不能与其他包重名
            namespace:给actionde的访问路径定义一个命名空间
            extends:继承一个指定包 默认继承 struts-default
            abstract:包是否为抽象的,标示性属性,标识该包不能独立运行,专为继承
    -->
    <package name="test" namespace="/test" extends="struts-default">
        <!--
            action元素:配置action类
                name:决定了action访问资源名
                class:action的完成类名
                method:制定调用action中的哪个方法来处理
        -->
        <action name="TestDemo1Action" class="com.qwm.struts2_1.testdemo.TestDemo1Action" method="test1">
            <!--
               result元素:结果配置
                  name:标识结果处理的名字,与action方法的返回值对应
                  type:制定调用哪一个result类来处理结果,默认使用转发
                  标签体:填写页面的相对路径
            -->
            <result name="success">/hello.jsp</result>
        </action>
    </package>
    <!--引入其他sttuts配置文件-->
    <include file="com/qwm/struts2_1/dynamic/struts.xml"/>
</struts>

2.3 常量配置方式二struts.properties中配置

在 src 下创建 struts.properties,配置如下

struts.i18n.encoding=UTF-8

2.4 常量配置方式三web.xml中配置

打开我们项目的 web.xml 文件,在下配置 。配置如下

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

    <context-param>
        <param-name>struts.i18n.encoding</param-name>
        <param-value>UTF-8</param-value>
    </context-param>

    <!--struts2核心过滤器配置-->
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

</web-app>

2.5 常量配置的加载顺序

常量的顺序如下

1. struts.xml

2. struts.properties

3. web.xml

那么很容易得出,如果我们在三个文件中配置了同一个常量,那么最后我们获取到的绝对是 web.xml 配置的值。


三、 动态方法调用配置

有的时候,我们的action方法很多,如果我们为每个方法都添加一个action那么挺麻烦的。这个时候,我们就可以使用动态配置的方法是了。

3.1 创建我们的类和配置文件

我们创建一个包 com.qwm.struts2_1.dynamic,在这个包下创建DynamicDemo1Action.javastruts.xml(可以取别的名称)。写我们的方法,和配置我们的配置文件。

DynamicDemo1Action.java

package com.qwm.struts2_1.dynamic;

/**
 * @author: wiming
 * @date: 2017-09-18 13:41:20  星期一
 * @decription:
 * 动态调用方法配置
 */
public class DynamicDemo1Action {
    public String add(){
        System.out.println("添加用户!");
        return "success";
    }
    public String delete(){
        System.out.println("删除用户!");
        return "success";
    }
    public String update(){
        System.out.println("修改用户!");
        return "success";
    }
    public String find(){
        System.out.println("查找用户!");
        return "success";
    }
}

com\qwm\struts2_1\dynamic\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>
    <package name="dynamic" namespace="/dynamic" extends="struts-default">

        <action name="DynamicAction" class="com.qwm.struts2_1.dynamic.DynamicDemo1Action" method="add">
            <result name="success">/hello.jsp</result>
        </action>
    </package>

</struts>

我们需要把我们上面的配置文件,添加到主配置文件 src\struts.xml中。

<!--引入其他sttuts配置文件-->
<include file="com/qwm/struts2_1/dynamic/struts.xml"/>

这个时候,我们能够访问我们的 add 方法了,这是毫无疑问的。但是我们需要动态调用。

3.2 动态方法调用方式1 配置常量

我们现在开始第一种方式的配置,在我们的 com\qwm\struts2_1\dynamic\struts.xml配置文件中,添加动态方法调用的常量配置即可

<!-- 配置动态方法调用是否开启常量
            默认是关闭的,需要开启
 -->
<constant name="struts.enable.DynamicMethodInvocation" value="false"/>

我们的配置文件就成为以下这样了

<?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.enable.DynamicMethodInvocation" value="false"/>

    <package name="dynamic" namespace="/dynamic" extends="struts-default">
        <!--全局允许方法调用-->
        <global-allowed-methods>regex:.*</global-allowed-methods>
        <action name="DynamicAction" class="com.qwm.struts2_1.dynamic.DynamicDemo1Action">
            <result name="success">/hello.jsp</result>
        </action>
    </package>

</struts>

现在配置好了,部署我们的服务器,我们现在就可以在浏览器中访问我们action的方法了。

访问方式

!方法名

例如:

我们访问 http://localhost:8080/s2_1/dynamic/DynamicAction!add

后台打印结果

添加用户!

我们访问 http://localhost:8080/s2_1/dynamic/DynamicAction!delete

后台打印结果

删除用户!

我们访问 http://localhost:8080/s2_1/dynamic/DynamicAction!update

后台打印结果

修改用户!

我们访问 http://localhost:8080/s2_1/dynamic/DynamicAction!find

后台打印结果

查找用户!

3.3 动态方法调用方式2 通配符匹配

这种方式的配置,我们不需要配置使用上面提到的常量。只需要对aciton的name和method修改就行了。例如

 <action name="DynamicAction_*" class="com.qwm.struts2_1.dynamic.DynamicDemo1Action" method="{1}">

使用{1} 取出第一个星号通配的内容。其中DynamicAction_*中的 也可以是 #,或者其他符号,只是习惯使用 ,那么我们的配置文件就成为下面这样的了

<?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="dynamic" namespace="/dynamic" extends="struts-default">
        <!--全局允许方法调用-->
        <global-allowed-methods>regex:.*</global-allowed-methods>
        <!-- 动态方法调用方式2:通配符方式
                 使用{1} 取出第一个星号通配的内容
         -->
        <action name="DynamicAction_*" class="com.qwm.struts2_1.dynamic.DynamicDemo1Action" method="{1}">
            <result name="success">/hello.jsp</result>
        </action>
    </package>

</struts>

访问方式 (“_”是我们配置的)

_方法名

例如:

我们访问 http://localhost:8080/s2_1/dynamic/DynamicAction_add

后台打印结果

添加用户!

我们访问 http://localhost:8080/s2_1/dynamic/DynamicAction_delete

后台打印结果

删除用户!

我们访问 http://localhost:8080/s2_1/dynamic/DynamicAction_update

后台打印结果

修改用户!

我们访问 http://localhost:8080/s2_1/dynamic/DynamicAction_find

后台打印结果

查找用户!

3.4 struts2.5动态调用配置额外的必须的配置

struts2.5 为了提升安全性,添加了allomethod这个配置。我们的动态调用想要成功,必须加上这个配置

“`xml

    <global-allowed-methods>regex:.*</global-allowed-methods>

    <action ....>
        ...
    </action>


“`

或者,针对action,在 action 块中添加

<allowed-methods>regex:.*</allowed-methods>

同样也支持在你的 action 上使用 @AllowedMethods 注解

默认的设置我们可以来到 struts2-core-2.5.13.jar!\struts-default.xml 中查看,如下

<global-allowed-methods>execute,input,back,cancel,browse,save,delete,list,index</global-allowed-methods>

四、Struts2 默认配置

default-action-ref 标签,可以设置我们默认的 action,再找不到我们 包下的action的时候,会调用我们默认的 ation。
如果我们不配置,Struts2默认是 com.opensymphony.xwork2.ActionSupport

<!-- 找不到包下的action,会使用Demo2Action作为默认action处理请求 -->
<default-action-ref name="Demo2Action"></default-action-ref>

下面是一些默认值。

action中的默认值

method属性: execute

class属性:com.opensymphony.xwork2.ActionSupport

result的name属性:success

result的type属性:dispatcher 转发

我们由上面知道,我们的Struts2的默认配置文件是:struts2-core-2.5.13.jar!\struts-default.xml

....
 <package name="struts-default" abstract="true" strict-method-invocation="true">
        <result-types>
            <result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult"/>
            <!--默认是转发-->
            <result-type name="dispatcher" class="org.apache.struts2.result.ServletDispatcherResult" default="true"/>
            <result-type name="freemarker" class="org.apache.struts2.views.freemarker.FreemarkerResult"/>
            <result-type name="httpheader" class="org.apache.struts2.result.HttpHeaderResult"/>
            <result-type name="redirect" class="org.apache.struts2.result.ServletRedirectResult"/>
            <result-type name="redirectAction" class="org.apache.struts2.result.ServletActionRedirectResult"/>
            <result-type name="stream" class="org.apache.struts2.result.StreamResult"/>
            <result-type name="velocity" class="org.apache.struts2.result.VelocityResult"/>
            <result-type name="xslt" class="org.apache.struts2.views.xslt.XSLTResult"/>
            <result-type name="plainText" class="org.apache.struts2.result.PlainTextResult" />
            <result-type name="postback" class="org.apache.struts2.result.PostbackResult" />
        </result-types>
   ....
    <default-class-ref class="com.opensymphony.xwork2.ActionSupport" />
    <global-allowed-methods>execute,input,back,cancel,browse,save,delete,list,index</global-allowed-methods>
 </package>
....

ActionSupport.java

public class ActionSupport implements Action, Validateable, ValidationAware, TextProvider, LocaleProvider, Serializable {
    .....
    public String execute() throws Exception {
        return SUCCESS;
    }
    ....
}

Action.java

public interface Action {

    public static final String SUCCESS = "success";


    public static final String NONE = "none";

    public static final String ERROR = "error";

    public static final String INPUT = "input";

    public static final String LOGIN = "login";

    public String execute() throws Exception;

}

五、action书写方式

5.1 action书写方式一、简单的类

例如

//方式1: 创建一个类.可以是POJO
//POJO:不用继承任何父类.也不需要实现任何接口.
//使struts2框架的代码侵入性更低.
public class Demo1Action {
  .....
}

5.2 action书写方式二、实现Action接口

例如

import com.opensymphony.xwork2.Action;

//方式2: 实现一个接口Action
// 里面有execute方法,提供action方法的规范.
// Action接口预置了一些字符串.可以在返回结果时使用.为了方便
public class Demo2Action implements Action {

    @Override
    public String execute() throws Exception {
        return null;
    }
    ....
}

5.3 action书写方式三、继承ActionSupport

常用这种。

例如

import com.opensymphony.xwork2.ActionSupport;

//方式3: 继承一个类.ActionSupport
// 帮我们实现了 Validateable, ValidationAware, TextProvider, LocaleProvider .
//如果我们需要用到这些接口的实现时,不需要自己来实现了.
public class Demo3Action  extends ActionSupport{
    ....
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值