Struts2的配置文件详解

来源:互联网 发布:网络大学本科算统招吗 编辑:程序博客网 时间:2024/05/12 19:47

web.xml, struts.xml, struts-config.xml 和 struts.properties,这些事struts常用的配置文件,其中web.xml,struts.xml是比较重要的,必须的配置文件。

  web.xml文件:

在web.xml配置文件是一个的J2EE配置文件,决定如何处理的HTTP请求的servlet容器的元素。它不是严格意义上的Struts2的配置文件,但它是一个必须配置的文件,Struts2才能正常工作。

正如前面所讨论的,此文件提供任何Web应用程序的入口点。Struts2的应用程序的入口点,将是一个部署描述符(web.xml)中定义的过滤器。因此,我们将FilterDispatcher类放在web.xml中定义的入口。需要创建文件夹的WebContent/ WEB-INF下的web.xml文件。

这是第一个需要产生的配置文件,如果你开始没有产生它(例如Eclipse或者Maven2)模板或工具的帮助下。以下是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"    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"   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>Struts 2</display-name>   <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>/*</url-pattern>   </filter-mapping></web-app>

需要注意的是,我们的Struts2的过滤器映射到/*,但不到/*.action,而不是意味着所有的URL将被过滤器解析。

  struts.xml文件:

struts.xml文件中包含的配置信息,将会修改用来开发。这个文件可以被用来覆盖默认设置的应用程序,例如struts.devMode=false和其他设置中定义的属性文件。这个文件可以创建文件夹WEB-INF/classes下。

让我们一起来看看我们在struts.xml文件中创建Hello World的例子,就像我们在前面的解释。 

<?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.devMode" value="true" />   <package name="helloworld" extends="struts-default">           <action name="hello"             class="com.tutorialspoint.struts2.HelloWorldAction"             method="execute">            <result name="success">/HelloWorld.jsp</result>      </action>      <-- more actions can be listed here -by www.yiibai.com/struts2 -->   </package>   <-- more packages can be listed here --></struts>

首先要注意的是DOCTYPE。所有的Struts配置文件中需要有正确的DOCTYPE,在我们的小例子所示。 <struts>是根标记的元素,我们声明使用不同的包<package>标签。其中,<package>允许分离和模块化的配置。这是非常有用的,当你有一个大的项目,项目被划分成不同的模块。 

如果你的项目有三个域 - business_applicaiton,customer_application和staff_application,你可以创建三个包,在适当的包装和存储相关的动作。包装标签具有以下属性:

AttributeDescriptionname (required)The unique identifier for the packageextendsWhich package does this package extend from? By default, we use struts-default as the base package.abstractIf marked true, the package is not available for end user consumption.namesapceUnique namespace for the actions

constant常量标签name和value属性将被用来覆盖default.properties中定义的属性,就像刚才设置struts.devModeproperty。设置struts.devMode属性,让我们看到了更多的调试信息在日志文件中。

我们对每一个URL标签定义action标记,我们想访问定义的一个类的execute()方法,访问时,我们将访问相应的URL。

result确定什么被返回到浏览器的一个action后执行。从操作返回的字符串应该是一个result的名称。结果如上配置的每次action,或作为一个“global”的结果。result标签有可选的名称和类型的属性。默认名称值是“success”。

随着时间的推移,struts.xml文件可以做大,打破它包的模块化是一种方式,但支柱提供了另一种模块化的struts.xml文件。你可以将档案分割成多个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>     <include file="my-struts1.xml"/>     <include file="my-struts2.xml"/></struts>
我们还没有涉及到的其他配置文件是struts-default.xml中。这个文件包含了Struts的标准配置设置,你的项目的99.99%不会碰这些设置。

  struts-config.xml 文件:

struts-config.xml配置文件是一个在Web客户端组件的视图和模型之间的联系,但你的项目的99.99%不会碰这些设置。基本的配置文件包含以下主要内容:

SNInterceptor & Description1struts-config
This is the root node of the configuration file.2form-beans
This is where you map your ActionForm subclass to a name. You use this name as an alias for your ActionForm throughout the rest of the struts-config.xml file, and even on your JSP pages.3global forwards
本节映射在你的web应用的名称。您可以使用这个名称,是指实际的页面。这就避免了硬编码在您的网页的URL。4action-mappings
This is where you declare form handlers and they are also known as action mappings.5controller
This section configures Struts internals and rarely used in practical situations.6plug-in
This section tells Struts where to find your properties files, which contain prompts and error messages

下面是示例struts-config.xml文件:

<?xml version="1.0" encoding="ISO-8859-1" ?><!DOCTYPE struts-config PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 1.0//EN""http://jakarta.apache.org/struts/dtds/struts-config_1_0.dtd"><struts-config>   <!-- ========== Form Bean Definitions ============ -->   <form-beans>      <form-bean name="login" type="test.struts.LoginForm" />   </form-beans>   <!-- ========== Global Forward Definitions ========= -->   <global-forwards>   </global-forwards>   <!-- ========== Action Mapping Definitions ======== -->   <action-mappings>      <action         path="/login"         type="test.struts.LoginAction" >         <forward name="valid" path="/jsp/MainMenu.jsp" />         <forward name="invalid" path="/jsp/LoginView.jsp" />      </action>   </action-mappings>   <!-- ========== Controller Definitions ======== -->   <controller       contentType="text/html;charset=UTF-8"      debug="3"      maxFileSize="1.618M"      locale="true"      nocache="true"/></struts-config>

struts-config.xml文件的更多详细信息,请查看Struts文档。

struts.properties 文件

此配置文件提供了一种机制来更改默认行为的框架。其实在struts.properties配置文件中包含的所有的属性也可以被配置在web.xml中使用的init-param,以及在struts.xml中的配置文件中使用恒定的标签。但如果你喜欢的东西保持独立和多支柱的具体,那么你可以创建此文件在folderWEB-INF/classes目录下。

在这个文件中配置的值将覆盖默认值配置在default.properties这是包含在Struts2的核心xyzjar的分布。还有几个你可能会考虑改变使用struts.properties文件的属性:

### When set to true, Struts will act much more friendly for developersstruts.devMode = true### Enables reloading of internationalization filesstruts.i18n.reload = true### Enables reloading of XML configuration filesstruts.configuration.xml.reload = true### Sets the port that the server is run onstruts.url.http.port = 8080

任何与井号(#)开头的行会被假定为注释,将被Struts2忽略。 


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 小孩听力残疾幼儿园不收怎么办 幼儿园不收外省小孩怎么办 孩子刚上幼儿园哭闹老师怎么办 孕妇糖耐指标高怎么办 貂蝉经常没蓝怎么办 儿童声导抗c型怎么办 酱牛肉不烂怎么办拯救 卤牛肉一切就碎怎么办 牛肉煮的太烂了怎么办 牛肉卤的太烂怎么办 牛肉炖得太软怎么办 牛肉炖的很硬怎么办 牛肉做的有点硬怎么办 牛肉做老了还能怎么办 牛肉粒 炒老了怎么办 牛肉炖出来很硬怎么办 煮的牛肉太硬怎么办 在产蛋鸡体重偏轻怎么办 产蛋鸡不上高峰怎么办 蛋鸡天热下蛋少怎么办 成年鸡嗉子鼓涨怎么办 黄牛拼失败了钱怎么办 磁力泥粘衣服上怎么办 鼻涕泥弄衣服上怎么办 水晶橡皮泥弄在衣服上怎么办 橡皮泥弄到衣服上怎么办 橡皮泥弄在衣服上怎么办 磁力泥弄衣服上怎么办 苹果7p按键坏了怎么办 苹果手机内存满了怎么办 来微信消息手机上面没提示怎么办 苹果手机储存空间满了怎么办 苹果六储存满了怎么办视频 微信听筒没声音怎么办 苹果手机有电自动关机怎么办 白玉蜗牛生蛋了怎么办 农民工工资拖欠怎么办没有合同 公司拖欠员工工资没签合同怎么办 东京去大阪乘大巴行李怎么办? 在外手机没电了怎么办 被公司起诉我该怎么办