structs2配置文件详解

下面是structs2的配置文件structs.xml的代码示例:

<?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>

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

    <include file="example.xml"/>



    <package name="default" namespace="/" extends="struts-default">
        <default-action-ref name="index" />
        <action name="index">
            <result type="redirectAction">
                <param name="actionName">HelloWorld</param>
                <param name="namespace">/example</param>
            </result>
        </action>
    </package>

    <!-- Add packages here -->

</struts>

不论是以前的servlet还是structs都是以servlet作为控制器的,而structs2是用filter作为控制器的,structs2的在web.xml配置了一个filter,用于拦截请求。配置如下:

filter

拦截到的请求都会被转发到structs.xml,structs.xml作为控制器负责页面转向。在这个filter里定义了一个filter,filter-name 是structs2,filter-class必须包含包的全限名,url-pattern为”/*”表示拦截所有的请求。

package标签

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

package是structs.xml组织模块的一种方式,每个package可以包含若干个action标签,为了维护方便,一般我们一个package用于包含一类action。package标签里的name属性定义了该package的名字,namespace是命名空间,该命名空间在URL栏起作用,一般URL的格式如下:

http://localhost:port/contextPath/namespace/actionName.action

多数URL的namespace默认为’/’所以在地址栏显示如下:

http://localhost:port/contextPath/actionName.action

extends规定该package继承的父类,这里一般要继承structs-default类。

action标签

<action name="index" class="com.opensymphony.xwork2.ActionSupport" method="execute">
    ...
</action>

action标签是真正负责跳转的标签,name是必需属性,用于指定调用哪个action,与拦截的请求的action name是对应的。class规定拦截到请求后调用哪个类,有默认值为com.opensymphony.xwork2.ActionSupport。method是规定调用类中的哪个方法,默认方法是execute方法。要注意一个action对应一个action请求,而一个action类可以处理多个action请求。

action类的特点

  1. action类中的命名规则要遵循JavaBean的命名规则,因为action类中属性赋值取值的方法也是通过getter和setter方法,这种方法要严格按照命名规则来操作。
  2. action类要有一个无参构造器。
  3. action类应当有处理action请求的方法,可以是一个也可以是多个。
  4. 针对每一个请求都会创建一个action实例,所以action类是线程安全的。

result标签

 <result name="success" type="dispatcher">
     ...
 </result>

result根据action方法返回的字符串调用页面,action方法返回的字符串与result的name对应,action节点可以对应多个result子节点,根据name属性区分,默认的name值为success。type指定结果类型,默认是dispatcher。


下面是web.xml的代码示例

<?xml version="1.0" encoding="UTF-8"?>
<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">
  <display-name>StructsTest1_1</display-name>

    <!-- 配置过滤器 -->
    <filter>
        <filter-name>struts2</filter-name><!-- 指定过滤器名称 -->
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter><!-- 指定过滤器执行的类 -->

    <filter-mapping>
        <filter-name>struts2</filter-name><!-- 此处名称要与上述名称相同 -->
        <url-pattern>/*</url-pattern>
    </filter-mapping>

  <!-- 配置欢迎页,当有多个欢迎页面时会自上而下筛选 -->
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>

  <!-- 配置异常跳转 -->
  <error-page>
    <error-code>404</error-code><!-- 捕获异常状态码 -->
    <location>/exception.jsp</location><!-- 跳转到异常处理页面 -->
  </error-page>
  <error-page>
    <exception-type>java.lang.exception</exception-type><!-- 捕获异常类型 -->
    <location>/exception.jsp</location><!-- 跳转到异常处理页面 -->
  </error-page>

  <!-- 配置会话时间 -->
  <session-config>
    <session-timeout>30</session-timeout><!-- 指定当会话超过30min自动失效 -->
  </session-config>

</web-app>

下面是default.properties文件示例,.properties文件主要是配置常量的配置文件,其中default.properties是默认的配置,要修改默认的配置的话我们需要自己在src文件夹中定义一个struct.properties文件用于覆盖默认配置。


### 指定web应用编码集
struts.i18n.encoding=UTF-8

### 指定structs2捕获的请求后缀
struts.action.extension=action,,

### 指定structs框架是否支持动态方法调用
struts.enable.DynamicMethodInvocation = false

### 指定在structs2标签中是否使用表达式语法
struts.tag.altSyntax=true

### 指定structs2是否使用开发者模式
struts.devMode = false

### 指定视图中structs2标签的主题
struts.ui.theme=xhtml

### 指定web应用的端口
struts.url.http.port = 80
发布了35 篇原创文章 · 获赞 4 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/jiujiuming/article/details/52583273