Struts是一个面向请求驱动MVC实现的框架。
首先我们用最基本的Servlet写一个MVC的实现。
1.定义一个中央控制器(改控制器是用来获取请求URL,并根据URL的不同将请求分发到不同的Action上)
package com.yx.zzg.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.yx.zzg.action.Action;
import com.yx.zzg.action.impl.AddAction;
import com.yx.zzg.action.impl.QueryAction;
/**
* 中央控制器
*
* @author toshiba
*
*/
public class ActionServelt extends HttpServlet {
@Override
protected void service(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// 获取请求Url;
String urlPath = request.getRequestURI();
// 截取请求Url
urlPath = urlPath.substring(urlPath.indexOf("/", 1));
String forwardUrl = "";
// 判断Url并分发到不同的Action,并统一控制转向
if ("/user/adduser.do".equals(urlPath)) {
Action action = new AddAction();
forwardUrl = action.execute(request, response);
} else if ("/user/queryuser.do".equals(urlPath)) {
Action action = new QueryAction();
forwardUrl = action.execute(request, response);
}
request.getRequestDispatcher(forwardUrl).forward(request, response);
}
}
2.修改web.xml,拦截所有以.do结尾的请求
<servlet>
<servlet-name>actionServelt</servlet-name>
<servlet-class>com.yx.zzg.servlet.ActionServelt</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>actionServelt</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
3.定义一个接口(Action.java)
package com.yx.zzg.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public interface Action {
public String execute(HttpServletRequest request,HttpServletResponse response);
}
4.定义两个action,分别实现Action.java接口
package com.yx.zzg.action.impl;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.yx.zzg.action.Action;
public class AddAction implements Action {
public String execute(HttpServletRequest request,
HttpServletResponse response) {
String username=request.getParameter("username");
System.out.println("------------------addAction-------------"+username);
return "/success.jsp";
}
}
package com.yx.zzg.action.impl;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.yx.zzg.action.Action;
public class QueryAction implements Action {
public String execute(HttpServletRequest request,
HttpServletResponse response) {
String username=request.getParameter("username");
System.out.println("----------queryAction--------"+username);
return "/result.jsp";
}
}
这样处理如果业务增加,相应的中央控制器里面的if else语句就会越来越多,而且在程序里面且不方便以后维护。
所以我们就需要这样的一个配置文件去集中控制,这个配置文件需要指定请求url,被分发的action实现类,以及跳转页面等等。
如以下:
引用
<action-config>
<action path="/user/addaction" class="com.yx.zzg.action.impl.AddAction" success="/success.jsp"/>
<action path="/user/queryaction" class="com.yx.zzg.action.impl.QuerydAction" success="/result.jsp"/>
</action-config>
因此struts的实现的基本原理是struts的中央控制器获取请求url并读取struts-conf.xml文件,然后根据请求url和struts-conf.xml中配置的action的path进行比较,获取action的实现类并分发出去,action再调用我们的模型层(Model)。