1. 创建web项目,添加jar包
很多人不知道怎么添加struts2的jar包,其实不需要记,用解压软件打开struts-2.3.24.1\apps下的事例程序,直接复制WEB-INF下的lib文件夹中的所有jar包就可以了。
2. 创建action
public class HelloAction {
public String execute() {
return "ok";
}
}
3. 在struts.xml中配置action的访问路径
(1)在src的根目录下新建struts2的核心配置文件struts.xml(名字和位置都不能变)
(2)复制案例中的dtd约束语句到struts.xml中,
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
(3)具体配置action的访问路径
<struts>
<package name="hellodemo" extends="struts-default" namespace="/">
<!-- name 访问名称 -->
<action name="hello" class="com.zzc.struts2.action.HelloAction">
<!-- 配置方法的返回值界面 name="ok" 正好和action中的return "ok" 相对应-->
<result name="ok">/WEB-INF/jsp/hello.jsp</result>
</action>
</package>
</struts>
4. 配置struts2的过滤器(在web,xml中配置,在web.xml中加入以下的代码)
<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>
5. 在浏览器可以正常访问(......./hello.action),然后可以根据需求编写其他action。