一.开发环境
eclipse+tomcat+struts-2.2.3
eclipse 下载地址:http://www.eclipse.org/downloads/ tomcat下载地址:http://tomcat.apache.org/download-70.cgi struts下载地址:http://struts.apache.org/download.cgi#struts23162二.新建web项目,并向web项目中添加依赖jar包
三.配置web.xm,向web.xml配置Struts2过滤器
struts2 org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter struts2 /* index.jsp
FilterDispather作为Struts2框架的核心控制器,负责拦截用户的所有请求,当用户请求到达时,该过滤器会过滤用户请求,这样将不同的请求以业务类型划分,并将请求继续传给不同的业务控制器Action,Action调用不同的业务模型对请求进行处理,并将处理结果返回给视图。
四.向web项目中新建Action业务控制器
package Action;
import com.opensymphony.xwork2.ActionSupport;
public class indexAction extends ActionSupport {
public String index()
{
return SUCCESS;
}
}
五.配置Struts.xml,配置Action业务控制器到Struts.xml中
/hello.jsp
为什么要配置struts.xml?因为Struts2除了是一个MVC框架之外,还是 一个小巧的轻量级容器,负责管理核心控制器FilterDispather和业务控制器Action.可以说是struts容器将核心控制器拦截的用户请求转发给业务控制器,而不是核心控制器直接将请求转发给业务控制器,这样做解除了核心控制器FilterDispather与业务控制器Action之间的耦合,核心控制器FilterDispather只负责拦截用户请求、业务控制器只负责处理业务请求,核心控制器与业务控制器通过struts容器通信,而它们两者完全不知都彼此的存在。
六.添加jsp视图
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
My JSP 'index.jsp' starting page