Struts在xml里配置很简单,例如:
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<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>
配置完这个之后,默认的struts回去classpath里找struts.xml所以你必须在classpath中配置一个struts.xml。
当然这个位置文件可以修改位置或名字,修改位置和命名的方法如下:
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
<init-param>
<param-name>filterConfig</param-name>
<param-value>classpath:xx/xx/xx.xml</param-value><!--此处配置struts.xml的位置和命名-->
</init-param>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
例如有一个user.xml里面放了所有的对用户的操作的action。有老师的xml,有课程的xml,这些如果都放到struts.xml中,那么struts.xml就显得很笨重。
所以我们用<include>标签来引入其他模块的action。
例如在struts.xml中通常会配置基本的属性之后引入其他的xml。
<!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" />
<include file="struts/hello/hello.xml"/>
</struts>
这是hello.xml的配置
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="hello" extends="struts-default" namespace="/hello">
<action name="hello" class="com.zhang.controller.HelloWorldController" >
<result>/hello.jsp</result>
</action>
</package>
</struts>