Struts2使用需要用到两个配置文件
struts.xml : Struts2自己的配置文件,用来配置Action
web.xml : 用来加载Struts2框架
1.struts.xml
struts.xml是Struts2默认的文件名,一般不会更改。
真正的项目中不会只用struts.xml一个配置文件,因为一个配置文件会造成配置文件过于臃肿, 会按照模块进行拆分。
struts.xml
|-struts-user.xml
|-...
以下是struts.xml和struts-user.xml的详细配置及解释
struts.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>
<!-- 开发者模式:自动重新加载。 每次修改XML配置时,不用重启服务器。默认:false-->
<constant name="struts.devMode" value="true" />
<!-- URL中的动作扩展名: http://localhost:8080/StrutsDemo/User/login.html
不需要扩展名时,value="" :http://localhost:8080/StrutsDemo/User/login
-->
<constant name="struts.action.extension" value="html" />
<!-- 根据模块将struts.xml文件也分成多个, 分放在不同的包下 -->
<include file="config/struts-user.xml"></include>
</struts>
struts-user.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>
<!-- name:包名
namespace:命名空间 (一般情况 包名与命名空间名字一致, 命名空间首字母大写)
http://localhost:8080/StrutsDemo/User/login
extends:继承
<parkage name="defalut" namespace="/" extends="struts-defalut"></parkage>
-->
<parkage name="user" namespace="User" extends=struts-default"">
<!-- methord:指定需要执行的方法名, 默认执行execute()方法 -->
<action name="login" class="com.main.java.action.user.LoginAction" methord="">
<!-- 方法返回字符串:return SUCCESS; -->
<result name="SUCCESS">welcom.jsp</result>
<result name="Error">error.jsp</result>
<!-- type:redirect重定向,默认转发方式 -->
<result name="INDEX" type="redirect">index/indexAction</result>
</action>
</parkage>
</struts>
2.web.xml
所有的MVC框架都需要web应用加载一个核心控制器。
对于struts2而言,需要加载FilterDispatch,只要加载了FileterDispatch,FileterDispatch就会自动加载Struts2框架
所以只需要在web.xml中配置FileterDispatch就可以了
具体配置详解如下:
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app 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" version="2.4">
<filter>
<!-- 配置Struts2核心Filter的名字 -->
<filter-name>struts2</filter-name>
<!-- 配置Struts2核心Filter的实现类 -->
<!-- FilterDispatch:在Struts2.1.3中已经被废弃 -->
<!-- Struts版本 >= 2.1.3 建议使用org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFileter-->
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
<!-- 配置Struts2框架默认加载的Action包结构,可以没有。 -->
<init-param>
<param-name>actionPackages</param-name>
<param-value>org.apache.struts2.showcase.person</param-value>
</init-param>
<!-- 配置Struts2框架的配置提供者类,可以没有 -->
<init-param>
<param-name>configProviders</param-name>
<param-value>lee.MyConfigurationProvider</param-value>
</init-param>
</filter>
<!-- 配置Filter拦截的URL -->
<filter-mapping>
<!-- 配置Struts2的核心FilterDispatcher拦截所有用户请求 -->
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
为Filter配置初始化参数时,其中有3个初始化参数有特殊意义
- config:使用逗号隔开的字符串,每个字符串都是一个XML配置文件的位置。Struts2框架将自动加载该属性指定的系列配置文件。
- actionPackages:使用逗号隔开的字符串,每个字符串都是一个包空间,Struts2框架将扫描这些包空间下的Action。
- configProviders:配置自己的ConfigurationProvider类
还可以在此处配置Struts2常量
<init-param>
<param-name>常量名</param-name>
<param-value>常量值</param-value>
</init-param>