发表时间:2009-06-19
最后修改:2009-09-08
package com.ray.mvc.control;
import java.io.IOException;
import java.lang.reflect.GenericDeclaration;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.ray.biz.ink.Action;
import com.ray.mvc.pojo.ActionModel;
import com.ray.mvc.xmlreader.DOM4JReadXml;
import com.ray.xmldao.dao.DBConnection;
/**
* 功能:拦截所有页面的url,作为中央控制器,再根据拦截的url进行调用相应的接口与实现类,
* 根据xml的指定路径转发到不同的页面。
* @author Ray
* @createdate:2009年6月13日
* @version:1.0.0
* questinoList :
* (1)
* //实现 Action(接口) action = new 接口实现类()
* // action.method();
*
* */
public class ControlServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//截取所有页面的url
String currUrl = request.getRequestURI();
System.out.println("currUrl="+currUrl);
//截取所有页面的路径
String path=currUrl.substring(currUrl.indexOf("/", 1));
path = path.substring(0,path.indexOf("."));
System.out.println("path="+path);
DBConnection conn = new DBConnection();
//页面转向路径
String forwardUrl="";
//实例化ActionModel对象
ActionModel model = new ActionModel();
model = FindActoinModel(path);
if(model!=null&&model.getFail()!=null&&model.getSuccess()!=null){
try {
Class cls = Class.forName(model.getInkRealize());
Object obj = cls.newInstance();
Action action = (Action) obj;
boolean isExecute= action.execute (request, response);
if(isExecute){
forwardUrl = model.getSuccess();
}else{
forwardUrl = model.getFail();
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
forwardUrl = "/fail.jsp";
}
//统一转向
request.getRequestDispatcher(forwardUrl).forward(request,response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
/**
* 取得当前的ActionModel对象
* @param path
* @return ActionModel
* */
private ActionModel FindActoinModel(String path){
// 实例化ActionModel对象
ActionModel model = new ActionModel();
int flag = 0;
// 创建DOM4JReadXml 实例
DOM4JReadXml dom= new DOM4JReadXml();
List list = dom.getAttributList();
for(int i = 0; i < list.size(); i++){
model = (ActionModel)list.get(i);
//System.out.println(model.getInk());
if(model.getInput().equals(path)){
flag = 1;
break;
}
}
if(flag==1){
return model;
}else{
return null;
}
}
}