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,你可以创建三个包,在适当的包装和存储相关的动作。包装标签具有以下属性:
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%不会碰这些设置。基本的配置文件包含以下主要内容:
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忽略。
- Struts2的配置文件详解
- Struts2的配置文件详解
- Struts2的配置文件详解
- Struts2的配置文件详解
- Struts2的properties配置文件详解
- Struts2的properties配置文件详解
- Struts2的properties配置文件详解
- 详解Struts2的properties配置文件
- Struts2的properties配置文件详解
- Struts2的properties配置文件详解
- Struts2的properties配置文件详解
- Struts2的properties配置文件详解
- Struts2的properties配置文件详解
- Struts2的配置文件标签详解
- struts2的struts.properties配置文件详解
- struts2的struts.properties配置文件详解
- struts2的struts.properties配置文件详解
- struts2的struts.properties配置文件详解
- 谷歌下解决Pop遮罩层无法遮挡滚动条下问题
- UF超滤膜:UF超滤膜的性能及功效
- 协议森林11 涅槃 (TCP重新发送)
- Hibernate 查询缓存
- 协议森林12 天下为公 (TCP堵塞控制)
- Struts2的配置文件详解
- 协议森林13 9527 (DNS协议)
- addr2line探秘
- 协议森林15 先生,要点单吗? (HTTP协议概览)
- Web 软件测试 Checklist 应用系列 第 2 部分: 导航和链接
- linux文件的三个主要的修改时间
- BF算法和KMP算法
- dsfsdfdsfds
- 协议森林14 逆袭 (CIDR与NAT)