Struts2基本配置

访问HelloWorld应用的路径的设置

HelloWorldAction文件:

package cn.itcast.primer;

import com.opensymphony.xwork2.ActionSupport;

public class HelloWorldAction extends ActionSupport{

    public String execute() throws Exception {
        System.out.println("HelloWorldAction ************* execute()");
        return "success";
    }
}

struts.xml

 <package name="primer" namespace="/primer"   extends="struts-default">
      <action name="helloWorldAction" class="cn.itcast.primer.HelloWorldAction">
        <result name="success" type="dispatcher">/primer/success.jsp</result>
      </action>
 </package>

在struts2中,访问struts2中action的URL路径由两部份组成:
包的命名空间+action的名称
例如: 访问本例子HelloWorldAction的URL路径为: /primer/helloWorldAction.action (注意:完整路径为:http://localhost:端口/内容路径/primer/helloWorldAction.action)。另外我们也可以加上.action后缀访问此Action。

 <package name="primer" namespace=“/primer“   extends="struts-default">
      <action name="helloWorldAction" class="cn.itcast.primer.HelloWorldAction">
        <result name="success" type="dispatcher">/success.jsp</result>
      </action>
 </package>

指定多个struts配置文件

在大部分应用里,随着应用规模的增加,系统中Action的数量也会大量增加,导致struts.xml配置文件变得非常臃肿。为了避免struts.xml文件过于庞大、臃肿,提高struts.xml文件的可读性,我们可以将一个struts.xml配置文件分解成多个配置文件,然后在struts.xml文件中包含其他配置文件。下面的struts.xml通过元素指定多个配置文件:

<struts>
    <include file="struts-user.xml"/>
    <include file="struts-order.xml"/>
</struts>

通过这种方式,我们就可以将Struts 2的Action按模块添加在多个配置文件中。

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>

    <!-- 引入自定义配置文件 -->
    <include file="cn/itcast/primer/struts_primer.xml"></include>

</struts>

自定义配置文件struts_primer.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>
        <!-- /primer/helloWorldAction.action
        package:包
            * name:包名,唯一的,必选项
            * namespace:命名空间,唯一的,相当于房间号。可选项,省略情况下是"/"。页面中请求连接的前半部分
            * extends:继承
                * extends="struts-default":struts2框架底层提供的核心包struts2-core-2.3.3.jar下的struts-default.xml文件
                * 为什么要继承这个struts-default.xml文件?
                    * 因为struts2框架底层提供的struts-default.xml声明了所有的拦截器和拦截器栈,
                         知道在struts2框架运行时执行struts-default.xml文件中的拦截器栈。
                    * 如果不继承struts-default.xml文件,就没有办法使用struts2框架提供的所有拦截器
     -->

    <package name="primer" namespace="/primer" extends="struts-default">
        <!-- 
            如果找不到对应的action名的时候,配置默认要执行的action 
                * name:指定action的名称
        -->
        <default-action-ref name="helloWorldAction"></default-action-ref>


        <!-- 
            action:
                * name:对应页面中请求连接的后面半部分
                * class:对应要执行的类的完整路径
         -->


         <action name="helloWorldAction" class="cn.itcast.primer.HelloWorldAction">
             <!-- 
                result:结果类型
                    * name:对应的是执行的类的方法的返回值
                        public String execute() throws Exception {
                            System.out.println("HelloWorldAction ************* execute()");
                            return "success";
                        }
                    * 后半部分的文本内容:要转向到的页面
             -->
            <result name="success">/primer/success.jsp</result>
        </action>

        <!-- 
            没有为action指定class
                * 在struts2框架底层的struts-default.xml文件中,配置了默认执行的类
                    com.opensymphony.xwork2.ActionSupport
                        public String execute() throws Exception {
                            return SUCCESS;
                        }
                * 实际上,默认执行的是底层提供的ActionSupport类的execute()方法
                * result结果类型,默认是根据struts2框架底层提供的ActionSupport类的execute()方法返回值,进行跳转
         -->
        <action name="actionNoClass">
            <result name="success">/primer/success.jsp</result>
        </action>

    </package>

</struts>

自定义配置文件需要放置在对应的包内,如图所示:
这里写图片描述
struts.xml文件配置中还需要引入自定义配置文件


Action名称的搜索顺序

test.jsp文件:

<%@ page language="java" pageEncoding="utf-8" contentType="text/html; charset=utf-8"%>
<%@ taglib uri="/struts-tags"   prefix="s"%>
<html>
  <head>
    <title>My JSP 'index.jsp' starting page</title>
    </head>
  <body>
       入门的路径:<br>  
      <a href="${pageContext.request.contextPath}/primer/helloWorldAction.action">helloWorld</a><br>


       测试Action名称的搜索顺序:<br>
        <a href="${pageContext.request.contextPath}/primer/primer/aaa/primer/helloWorldAction.action">helloWorld</a><br>
        <a href="${pageContext.request.contextPath}/primer/primer/helloWorldAction.action">helloWorld</a><br>
        <a href="${pageContext.request.contextPath}/primer/helloWorldAction.action">helloWorld</a><br>

        没有为action指定class<br>
           <a href="${pageContext.request.contextPath}/primer/actionNoClass.action">helloWorld</a><br>


      测试struts2 输出没有命名空间helloworld:<br>
        <a href="${pageContext.request.contextPath}/primer/userAction.action">helloWorld</a><br>






  </body>
</html>

这里写图片描述

  1. 测试第一个超链接
    这里写图片描述
  2. 测试第二个超链接
    这里写图片描述
  3. 测试第三个超链接
    这里写图片描述
  4. 测试第四个超链接
    这里写图片描述
  5. 测试第五个超链接
    这里写图片描述
  6. 测试第六个超链接
    这里写图片描述

总结:
1.获得请求路径的URI,例如url是:
http://server/struts2/path1/path2/path3/test.action

2.首先寻找namespace为/path1/path2/path3的package,
如果存在这个package,则在这个package中寻找名字为test的action,
如果不存在这个package则转步骤3;

3.寻找namespace为/path1/path2的package,
如果存在这个package,则在这个package中寻找名字为test的action,
如果不存在这个package,则转步骤4;

4.寻找namespace为/path1的package,
如果存在这个package,则在这个package中寻找名字为test的action,
如果仍然不存在这个package,就去默认的namaspace的package下面去找名
字为test的action(默认的命名空间为空字符串“/” ),
如果还是找不到,页面提示找不到action。


Action配置中的各项默认值

问题如果没有为action指定class,默认是com.opensymphony.xwork2.ActionSupport执行ActionSupport中的execute方法 ,由struts-default.xml文件<default-class-ref class="com.opensymphony.xwork2.ActionSupport" />决定

<package name="primer" namespace="/primer" extends="struts-default">

<!-- 
    没有为action指定class
        * 在struts2框架底层的struts-default.xml文件中,配置了默认执行的类
        com.opensymphony.xwork2.ActionSupport
        public String execute() throws Exception {
            return SUCCESS;
           }
    * 实际上,默认执行的是底层提供的ActionSupport类的execute()方法
    * result结果类型,默认是根据struts2框架底层提供的ActionSupport类的execute()方法返回值,进行跳转
 -->
    <action name="actionNoClass">
        <result name="success">/primer/success.jsp</result>
    </action>
</package>
  1. 如果没有为action指定class,默认是ActionSupport。
  2. 如果没有为action指定method,默认执行action中的execute() 方法。ActionSupport的execute方法里面就一句话return “success”;
  3. 如果没有指定result的name属性,默认值为success。
    问题:如果请求的路径查找不到action的情况下,程序运行会抛出异常 ,可以通过配置当找不到action的情况下,会执行默认的action
<package name="primer" namespace="/primer" extends="struts-default">
<!-- 
    如果找不到对应的action名的时候,配置默认要执行的action 
        * name:指定action的名称
-->
<default-action-ref name="helloWorldAction"></default-action-ref>

</package>

ActionSupport

ActionSupport类是默认的 Action 类. 在编写 Action 类时, 通常会对这个类进行扩展

这里写图片描述


Struts 2处理的请求后缀

StrutsPrepareAndExecuteFilter是Struts 2框架的核心控制器,它负责拦截由<url-pattern>/*</url-pattern>指定的所有用户请求,当用户请求到达时,该Filter会过滤用户的请求。默认情况下,如果用户请求的路径不带后缀或者后缀以.action结尾,这时请求将被转入Struts 2框架处理,否则Struts 2框架将略过该请求的处理。

根据配置文件:struts2-core-2.3.31.jar包下的 org.apache.struts2/default.properties文件定义的常量决定
struts.action.extension=action,,
这里写图片描述
这里写图片描述

默认处理的后缀是可以通过常量”struts.action.extension“进行修改的,如下面配置Struts 2只处理以.do为后缀的请求路径:

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

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:指定的是struts2框架底层提供的default.properties资源文件中配置的"常量"
            * value:指定的是配置常量的值
            * 在struts.xml文件中,配置的常量的值会覆盖底层提供的default.properties资源文件中配置的常量的值

        * 配置struts2框架的页面中请求连接的后缀名,如果指定多个的话,用","隔开
        * 如果在struts.xml中和struts.properties资源文件中同时进行配置,struts.properties的配置起作用
        * 因为常量可以在多个配置文件中进行定义,所以我们需要了解下struts2加载常量的搜索顺序:
            1 struts-default.xml
            2 struts-plugin.xml
            3 struts.xml
            4 struts.properties(自己创建)
            5 web.xml
     -->
    <!-- <constant name="struts.action.extension" value="do,love"></constant> -->




    <!-- 引入自定义配置文件 -->
    <include file="cn/itcast/primer/struts_primer.xml"></include>



</struts>

细说常量定义

常量可以在struts.xml或struts.properties中配置,建议在struts.xml中配置,两种配置方式如下:
1.在struts.xml文件中配置常量

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

2.在struts.properties中配置常量, (struts.properties文件放置在src下)

struts.action.extension=do

因为常量可以在多个配置文件中进行定义,所以我们需要了解下struts2加载常量的搜索顺序:
1 struts-default.xml
2 struts-plugin.xml
3 struts.xml
4 struts.properties
5 web.xml
如果在多个文件中配置了同一个常量,则后一个文件中配置的常量值会覆盖前面文件中配置的常量值.


常用的常量介绍

  • 指定默认编码集,作用于HttpServletRequest的setCharacterEncoding方法 和freemarker 、velocity的输出
    <constant name="struts.i18n.encoding" value="UTF-8"/>

  • 该属性指定需要Struts 2处理的请求后缀,该属性的默认值是action,即所有匹配*.action的请求都由Struts2处理。
    如果用户需要指定多个请求后缀,则多个后缀之间以英文逗号(,)隔开 <constant name="struts.action.extension" value="do"/>

  • 设置浏览器是否缓存静态内容,默认值为true(生产环境下使用),开发阶段最好关闭
    <constant name="struts.serve.static.browserCache" value="false"/>
  • 当struts的配置文件修改后,系统是否自动重新加载该文件,默认值为false(生产环境下使用),开发阶段最好
    <constant name="struts.configuration.xml.reload" value="true"/>

  • 开发模式下使用,这样可以打印出更详细的错误
    <constant name="struts.devMode" value="true" />

  • 默认的视图
    <constant name="struts.ui.theme" value="simple" />

  • 与spring集成时,指定由spring负责action对象的
    <constant name="struts.objectFactory" value="spring" />

  • 该属性设置Struts 2是否支持动态方法调用,该属性的默认值是true。如果需要关闭动态方法调用,则可设置该属性为 false
    <constant name="struts.enable.DynamicMethodInvocation" value="false"/>

  • 上传文件的大小限制
    <constant name="struts.multipart.maxSize" value=“10701096"/>

    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:指定的是struts2框架底层提供的default.properties资源文件中配置的"常量"
            * value:指定的是配置常量的值
            * 在struts.xml文件中,配置的常量的值会覆盖底层提供的default.properties资源文件中配置的常量的值

        * 配置struts2框架的页面中请求连接的后缀名,如果指定多个的话,用","隔开
        * 如果在struts.xml中和struts.properties资源文件中同时进行配置,struts.properties的配置起作用
        * 因为常量可以在多个配置文件中进行定义,所以我们需要了解下struts2加载常量的搜索顺序:
            1 struts-default.xml
            2 struts-plugin.xml
            3 struts.xml
            4 struts.properties(自己创建)
            5 web.xml
     -->
    <!-- <constant name="struts.action.extension" value="do,love"></constant> -->

    <!-- 配置国际化资源文件修改时,是否重新加载。默认是false为不加载,true是加载 -->
    <!-- <constant name="struts.i18n.reload" value="true"></constant> -->

    <!-- 配置struts2框架的配置文件修改时,是否重新加载。默认是false为不加载,true是加载 -->
    <!-- <constant name="struts.configuration.xml.reload" value="true"></constant> -->

    <!-- 
        配置struts2框架的模式
            * 默认值是false,是生产模式
            * true是开发模式,需要更多的调试信息
                ### includes:
                ### - struts.i18n.reload = true
                ### - struts.configuration.xml.reload = true
     -->
    <constant name="struts.devMode" value="true"></constant>

    <!-- 配置动态方法调用,设置成不开启。默认是true是开启状态;false是不开启状态 -->
    <constant name="struts.enable.DynamicMethodInvocation" value="false"></constant>

    <!-- 
        配置所有资源文件,省略后缀名,如果配置多个资源文件时,用","隔开。不仅是国际化资源文件
        * 类型转换器的错误提示资源文件
     -->
    <constant name="struts.custom.i18n.resources" 
            value="cn.itcast.converter.converter,
                    cn.itcast.i18n.resources">
    </constant>

    <!-- 配置文件上传的总大小 -->
    <constant name="struts.multipart.maxSize" value="2097152000"></constant>

    <!-- 引入自定义配置文件 -->
    <include file="cn/itcast/primer/struts_primer.xml"></include>


</struts>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值