一、基本步骤
1.创建web工程
2.导如struts2相关jar包(基础八个)
commons-fileupload-x.y.z.jar
commons-io-x.y.z.jar
commons-lang-x.y.jar
commons-logging-x.y.z.jar
commons-logging-api-x.y.jar
freemarker-x.y.z.jar
javassist-x.y.z.GA
ognl-x.y.z.jar
struts2-core-x.y.z.jar
xwork-core.x.y.z.jar
3.在工程的src下创建Action类,需要继承ActionSupport类。
package com.ppo.action;
import com.opensymphony.xwork2.ActionSupport;
public class FirstAction extends ActionSupport {
@Override
public String execute() throws Exception {
//调用该Action,默认执行execute(方法)
//返回的字符串有SUCCESS,ERROR,INPUT,NONE,LOGIN,或者自定义字符串
return SUCCESS;
}
@Override
public void validate() {
// 完成校验功能,在execute之前执行
super.validate();
}
}
4.配置文件web.xml(文件路径:项目/WebRoot/WEB-INF/web.xml)和struts.xml(文件路径:项目/src/struts.xml)
4.1在web.xml中配置struts2
<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>
4.2在struts.xml中配置action
<?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>
<!-- package标签的namespace和action标签的name是访问路径 -->
<!-- 访问FirstAction:http://localhost:8080/项目名/action/firstaction -->
<package name="default" namespace="/action" extends="struts-default">
<action name="firstaction" method="" class="com.ppo.action.FirstAction">
<!-- result的name值等于action返回的字符串常量 -->
<result name="">/index.jsp</result>
</action>
</package>
</struts>
5、发布运行web项目
(示例环境:myeclipse10+tomcat7.0+jdk1.7)