首先是配置web,xml文件
<servlet>
<servlet-name>ControllerServlet</servlet-name>
<servlet-class>controller.ControllerServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ControllerServlet</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
ControllerServlet 用到servlet的doGet,doPost和init方法
这里将doGet,doPost的处理过程统一交给doAction来处理
init将实现ControllerMapping的构造
代码如下:
package controller;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.xml.sax.SAXException;
import exception.NotFindMappingException;
import action.BaseAction;
import action.LoginAction;
/**
* @author lingxiao
*/
public class ControllerServlet extends HttpServlet {
private static String defForward="SYSTEMERROR";
private String actionName;
private HttpSession session;
private ControllerMapping mapping;
/**
* 请求统一交给doAction处理
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
try {
this.doAction(request, response);
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
/**
* 请求统一交给doAction处理
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
try {
this.doAction(request, response);
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
/**
* 构建ControllerMapping
*/
public void init() throws ServletException {
super.init();
Log log = LogFactory.getLog(getClass());
ControllerMappingFactory factory = new ControllerMappingFactory(this.getServletContext().getRealPath("/") + "//WEB-INF//controllerMapping.xml",new ControllerMapping());
//根据controllerMapping.xml构建ControllerMapping
try {
log.debug("构建ControllerMapping......");
mapping = (ControllerMapping) factory.buildingMapping();
log.debug("构建ControllerMapping成功。");
} catch (ParserConfigurationException e) {
log.error("构建ControllerMapping失败。"+e.getMessage());
e.printStackTrace();
} catch (SAXException e) {
log.error("构建ControllerMapping失败。"+e.getMessage());
e.printStackTrace();
} catch (IOException e) {
log.error("构建ControllerMapping失败。"+e.getMessage());
e.printStackTrace();
}
// Put your code here
}
/**
* ControllerData为一个控制器,将会根据其属性controllerBean来加载一个类并创建对象
*/
private String doBaseAction(HttpServletRequest request, HttpServletResponse response,ControllerData data) throws ClassNotFoundException{
Log log = LogFactory.getLog(getClass());
try {
Object obj = Class.forName(data.getControllerBean()).newInstance();
log.debug("根据URL:"+actionName+"获得bean对象:"+obj.toString());
ReflectFactory factory = new ReflectFactory(request,obj);
log.debug("正在注入表单属性......");
BaseAction action = null;
try {
action = (BaseAction)factory.building();
} catch (IllegalArgumentException e) {
log.error("注入表单属性失败:"+e.getMessage());
e.printStackTrace();
} catch (SecurityException e) {
log.error("注入表单属性失败:"+e.getMessage());
e.printStackTrace();
} catch (NoSuchMethodException e) {
log.error("注入表单属性失败:"+e.getMessage());
e.printStackTrace();
} catch (InvocationTargetException e) {
log.error("注入表单属性失败:"+e.getMessage());
e.printStackTrace();
}
log.debug("正在注入表单属性完毕。");
//使用反射机制将表单数据写入对象的属性中。
return action.doOperate(request,response,session);
//直接调用doOperate方法
} catch (InstantiationException e) {
log.error("根据URL:"+actionName+"获得bean对象失败:"+e.getMessage());
e.printStackTrace();
} catch (IllegalAccessException e) {
log.error("根据URL:"+actionName+"获得bean对象失败:"+e.getMessage());
e.printStackTrace();
}
return this.defForward;
}
/**
* 将请求对应ControllerMapping转发给相应的bean
* 通过bean的返回值根据ControllerMapping Forward的值转向下一个地址
*/
public void doAction(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException, ClassNotFoundException, ParserConfigurationException, SAXException {
Log log = LogFactory.getLog(getClass());
//session比较常用,所以在这里直接获得session吧session传递给bean
session = request.getSession(true);
//解析URL,将getServletPath()前边"/"的路径,和后面的.action去掉
String[] path = request.getServletPath().split("/");
String action = request.getServletPath();
if(path.length>0)
action = path[path.length-1];
actionName = action.split("//.")[0];
//通过actionName来对应bean
ControllerData data = null;
try {
data = (ControllerData)mapping.getControllerData(actionName);
//通过actionName找到对应的ControllerData数据
} catch (NotFindMappingException e) {
log.error(e.exceptionInfo());
e.printStackTrace();
}
String forwardName = this.doBaseAction(request, response, data);
//通过ControllerData加载类,生成对象,返回forward值
if(forwardName.equals(this.defForward))
response.sendRedirect("/errorPage/systemError.jsp");
//缺省的forward值,返回一个系统错误页面
String rstring = new String();
try {
rstring = data.getForwardValue(forwardName);
//根据配置文件返回地址
} catch (NotFindMappingException e) {
log.error(e.exceptionInfo());
e.printStackTrace();
}
request.getRequestDispatcher(rstring).forward(request, response);
}
}
分享到:
相关推荐
mvc构架.net版集成spring,hibernate ,前端用ext.net,功能丰富,界面华丽,运行速度快。
简单的外文翻译,有关_Spring的web_MVC_构架模式
使用php开发自己的MVC模式构架.pdf ),新手可以看看,认识下什么是mvc框架,对框架有个底层的认识,也能试着自己开发一个
PHP实例开发源码—简易通讯录php MVC构架.zip PHP实例开发源码—简易通讯录php MVC构架.zip PHP实例开发源码—简易通讯录php MVC构架.zip
MVC,Smarty
采用MVC(模型-视图-控制器)构架的一个简单程序
对于 extjs 来说,大客户端程序一直很难写,当你为大客户端程序添加更多的功能和项目的时候,项目的体积往往迅速增长。...织和维持 ,所以,extjs4 配备了一个新的应用程序体系结构,它能结构化你的代码,那就是 extjs4 MVC。
对于 extjs 来说,大客户端程序一直很难写,当你为大客户端程序添加更多的功能和项目的时候,项目的体积往往迅速增长。...织和维持 ,所以,extjs4 配备了一个新的应用程序体系结构,它能结构化你的代码,那就是 extjs4 MVC。
外国佬的.net MVC构架底层外国佬的.net MVC构架底层外国佬的.net MVC构架底层
外文翻译_Spring的web MVC 构架模式.docx外文翻译_Spring的web MVC 构架模式.docx外文翻译_Spring的web MVC 构架模式.docx外文翻译_Spring的web MVC 构架模式.docx外文翻译_Spring的web MVC 构架模式.docx外文翻译_...
关于Spring的web MVC 构架模式Spring的web MVC 构架模式
jsp订单作品+课设mvc构架 用于jsp课程设计下载。 独自开发,运行良好。
JAVA MVC构架 电子留言簿
外文翻译Spring地web MVC 构架模式.pdf外文翻译Spring地web MVC 构架模式.pdf
简易MVC构架的通讯录PHP源码,是PHP程序员学习MVC的简单范例。 主要功能:增 删 查 改 最方便的是拥有快速的排序功能。可以方便自主选择是调用数据库,还是使用客户端JS排序。
简单的外文翻译,有关_Spring的web_MVC_构架模式
外文翻译_Spring的web MVC 构架模式.pdf外文翻译_Spring的web MVC 构架模式.pdf
简易MVC构架的通讯录PHP源码,是PHP程序员学习MVC的简单范例。 主要功能:增 删 查 改 最方便的是拥有快速的排序功能。可以方便自主选择是调用数据库,还是使用客户端JS排序。
毕设 外文文献翻译--Spring的web MVC构架模式.docx毕设 外文文献翻译--Spring的web MVC构架模式.docx
基于PHP的简易通讯录php MVC构架.zip