Struts2的配置文件

Struts2共有5类配置文件,分别是struts.xml、struts.properties、Web.xml、struts-plugin.xml和struts_default.xml。

1、struts.xml文件

      struts.xml定义应用本身使用的action映射及result,但我们一般将应用的各个模块分到不同的配置文件中。在拦截器中,必须在使用前进行注册,struts配置文件可以支持继承,默认的配置文件包在struts 2-core-VESION.jar中。struts-default.xml文件是那些默认配置文件之一。它的主要功能就是用来注册默认的结果类型和拦截器。所以,在使用的时候没必要再struts.xml文件里进行注册,就可以使用默认的结果类型和拦截器。

       struts.xml文件代码如下:

<!-- 省略部分代码 -->
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
    "http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
  <constant name="struts.i18n.encoding" value="gbk" />
  <include file="struts-default.xml" />
  ……
</struts>
<!-- 省略部分代码 -->

在struts节点下有很多的子节点,而这些子节点则是配置框架的主要因素。接下来讲解struts节点下的子节点的含义。

1> include元素

      在struts.xml中我们经常会发现include元素。一个大的应用程序可能会有许多的包,为了使struts.xml便于管理,我们可以把它拆分成几个县的配置文件,然后再使用include元素来应用这些拆分的配置文件。

2>package元素

     为了方便使用,struts可以把各种动作分门别类的组织成不同的包,即package。在此可以把它当做struts.xml文件典型的模板。详细代码如下:

<!-- 省略部分代码 -->
  <package name="userinfo" namespace="/userinfo" extends="struts-default">
    <global-results>
      <result name="error">/exception.jsp</result>
    </global-results>
    <global-exception-mappings>
      <exception-mapping result="error" exception="java.lang.RuntimeException">
      </exception-mapping>
    </global-exception-mappings>
    <action name="loginuser" class="com.itzcn.action.Userinfo">
      <result name="loginout">/loginout.jsp</result>
    </action>
  </package>
<!-- 省略部分代码 -->

在上诉配置文件中,package可以有一个或多个,每个package都必须有一个不同的name属性。其中namespace属性是可有可无的属性,如果没有设置该属性则默认是“/”,如果设置了该属性的值,那么在使用这个包里面的动作时,就必须把该属性的命名空间添加到有关的URI字符串中。glbal节点是用来捕获异常的,在设置了java.lang.RuntimeException后运行出现异常,这样捕捉到异常之后转向指定的error页面。

3>constant元素

如果不需要创建一个新的文件,那么可以再struts.xml文件里使用constant元素。详细代码如下:

<!-- 省略部分代码 -->
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
    "http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
  <constant name="struts.i18n.encoding" value="gbk" />
  <constant anme="struts.devMode" value="true/>"
</struts>
<!-- 省略部分代码 -->
其中struts.xml文件主要负责管理应用中的Action映射以及该Action包含的Result定义等。除此之外,Struts2框架还包含了一个struts.propertise文件,该文件定义了Struts2框架的大量属性,可以通过修改这些属性来满足我们的需求。

 

2、struts.propertise文件

struts.propertise用来定义框架自身的全局变量,该文件定义的全局属性也可以再struts.xml中定义。struts.propertise文件时一个标准的propertise文件,该文件包含了一系列的key-value对象,每个key就有一个Struts2属性,该key对应的value就是一个Struts2属性值。该文件通常放在web应用的WEB-INF/classes路径下。将该文件放入web应用程序下的CLASSPATH路径下,Struts2框架就可以加载该文件。

 

3、Web.xml文件

准确的说,Web.xml不属于Struts2框架特有的文件。作为部署文件,Web.xml是所有java Web项目的核心文件。然而在这里之所以讲到该配置文件,是因为在使用struts2框架时,需要在Web.xml中配置一个前段控制器FilterDispatcher,用于对Struts2框架进行初始化和处理所有的请求。详细代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
 xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>*.action</url-pattern>
  </filter-mapping>
</web-app>

4、struts-plugin.xml文件

struts-plugin.xml文件是struts插件使用的配置文件,例如当结合struts和spring一起使用时就需要在Web.xml中引用该配置文件,这里不再详细说明。

 

5、struts-defaul.xml文件

struts-defaul.xml用来定义框架本身使用的action映射及result定义,是struts2框架默认加载的配置文件。它定义了struts2的一些核心bean和拦截器。struts-defaul包就在struts-defaul.xml文件中定义。该文件中还可以定义Struts2内置的结果类型、内置的拦截器以及不同拦截器组成的拦截器栈,这些拦截器栈可以直接使用。在struts-defaul.xml文件的最后还可以定义默认的拦截器引用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值