使用流程:
1、引入Struts 2的库文件:
将“common-logging-1.0.4.jar”、“freemaker-2.3.8.jar”、“struts2-core-2.0.11.1.jar”、“xwork-2.0.4.jar”和“ognl-2.6.11.jar”类库复制到Web项目的WEB-INF/lib目录下。
2、编辑Web项目中的“web.xml”配置文件,在配置文件中增加Struts 2核心Filter的配置:
web.xml配置文件<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" 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_3_0.xsd"> <display-name></display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- 定义核心Filter --> <filter> <!-- 定义核心Filter的名称 --> <filter-name>struts2</filter-name> <!-- 定义核心Filter的实现类 --> <filter-class> org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter </filter-class> </filter> <filter-mapping> <!-- 定义核心Filter的名称 --> <filter-name>struts2</filter-name> <!-- 使用该核心Filter来接受所有的Web请求 --> <url-pattern>/*</url-pattern> </filter-mapping></web-app>
3、新建控制器类Action
4、配置Action,修改struts.xml,其中包括指定Action的实现类以及Action处理结果与视图资源文件之间的映射:
struts.xml配置文件<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <!-- Action必须放在指定的包命名空间中 --> <package name="structs2" extends="struts-default"> <!-- 定义login的Action,其实现类为com.chaos.structs2.action.LoginAction --> <action name="aaa" class="com.chaos.structs2.action.LoginAction"> <!-- 定义处理结果与视图资源之间的关系 --> <result name="success">/login_success.jsp</result> <result name="error">/login_error.jsp</result> </action> </package> </struts>