structs2配置文件详解

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




<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">HelloWorldparam>
                <param name="namespace">/exampleparam>
            result>
        action>
    package>

    

struts>

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

structs2配置文件详解_第1张图片

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

package标签

"default" namespace="/" extends="struts-default">
    ...

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标签

"index" class="com.opensymphony.xwork2.ActionSupport" method="execute">
    ...

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标签

 "success" type="dispatcher">
     ...
 

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


下面是web.xml的代码示例


<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_1display-name>

    
    <filter>
        <filter-name>struts2filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilterfilter-class>
    filter>

    <filter-mapping>
        <filter-name>struts2filter-name>
        <url-pattern>/*url-pattern>
    filter-mapping>

  
  <welcome-file-list>
    <welcome-file>index.htmlwelcome-file>
    <welcome-file>index.htmwelcome-file>
    <welcome-file>index.jspwelcome-file>
    <welcome-file>default.htmlwelcome-file>
    <welcome-file>default.htmwelcome-file>
    <welcome-file>default.jspwelcome-file>
  welcome-file-list>

  
  <error-page>
    <error-code>404error-code>
    <location>/exception.jsplocation>
  error-page>
  <error-page>
    <exception-type>java.lang.exceptionexception-type>
    <location>/exception.jsplocation>
  error-page>

  
  <session-config>
    <session-timeout>30session-timeout>
  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

你可能感兴趣的:(Structs2)