目录
我的MyEclipse版本:
1.在web项目的lib文件夹中引入Struts-jar包
在Apache官网下载Struts:
分别下载jar包和源代码,便于在myeclipse中,阅读。
在Struts-2.5.18.all/struts-2.5.18/lib下是所有的jar文件(非常之多,以下是部分截图):
只需在web-INF/lib下,导入核心的13个包即可,其他有需要再加:
并添加他们到编译路径中。
2.在web.xml中,引入Struts的核心功能
配置过滤器(加入如下信息:)
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
web.xml内容变为:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>First_Struts</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
3.开发Action类
在src下新建一个类,模拟Action开发
package a_first_struts;
import com.opensymphony.xwork2.ActionSupport;
public class HelloAction extends ActionSupport{
public String execute() throws Exception{
System.out.println("访问到HelloAction,开始处理请求");
System.out.println("可以调用service处理");
return "yes";
}
}
4.配置Action类
在src下新建xml文档:struts.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<package name="hello" extends="struts-default">
<action name="first_struts" class="a_first_struts.HelloAction" method="execute">
<result name="yes">/index.jsp</result>
</action>
</package>
</struts>
5.启动tomcat服务器,打开浏览器验证:
同时,可以看到,控制台的输出:
至此,struts开发环境搭建完成。