struts2 web应用配置详解
1.首先来认识一下几个文件:
①web.xml :位于WEB-INF下。需自行创建。每一个web应用程序都应该有一个web.xml,它就像web应用程序的一本使用说明书,告诉服务器程序(如:tomcat) 该如何使用web 应用程序。
②struts-default.xml:位于struts2-core-2.XXX.jar的根目录下。定义了struts2默认配置。一般不要修改。包括result-type、拦截器和名为struts-default的package。通常每个包都应当继承struts-defauult包。struts2的众多核心功能都是由Interceptor(拦截器)实现的,Interceptor是struts2的基石,而在struts-default.xml中定义了这些Interceptor。可以这么说:只有继承了strut-default我们才能使用struts2的核心功能。
③struts-plugin.xml:位于struts2-XXX-pluin-2.X.XX.jar的根目录下。一般不要修改。用于配置插件使用的文件。可以用它来方便的加入一些特定的功能。它定义了一个package继承自struts-default,可以说struts-pluin.xml是struts-default.xml的补充。
④struts.xml:位于WEB-INF\classes目录下。主要用于配置页面的访问的路径。通常需要定义一个package,继承自struts-default,这样就拥有了struts-default的所有配置,从而很大程度的减轻了struts.xml的配置量。
⑤default.properties:位于struts2-core-2.XXX.jar中的org.
apache.struts2包中。这是一个属性文件,以key=value的形式存储配置信息,然后由struts2读取。它定义了struts2框架的默认配置。由struts2事先定义好了,一般不要修改。
⑥struts.properties:可有可无。用时需自行创建。可以用于配置struts,例如:struts.action.extension 属性用于指定访问action的后缀名,多个后缀名之间用英文逗号(,)隔开
struts.action.extension=do,,
这句话指定了action的后缀名为do或者为空,则我们想访问TestAction的url为http://localhost:8080/webapp/TestAction.do
或者http://localhost:8080/webapp/TestAction
所有配置信息都可以struts.xml编写,使用
<constant name=”key” value=”value”/>
<?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.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
<?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><package name="default" namespace="/" extends="struts-default"><default-action-ref name="index"></default-action-ref><action name="index" name="code"><default-action-ref name="index"></default-action-ref>
<global-results><result name="error">/error.jsp</result></global-results>
<exception-mapping result="error" exception="java.lang.Exception"/>
<global-results><result name="error">/error.jsp</result></global-results><global-exception-mappings><exception-mapping result="error" exception="java.lang.Exception"/></global-exception-mappings>
<action name="user_*" name="code"><default-interceptor-ref name="defaultStack"/>
<action name="index" name="code"><interceptors><interceptor name="timeInterceptor" class="com.jnmc.struts2.interceptor.TimeInterceptor"/></interceptors>