用DOM4J读XML,实现自定义MVC框架(二)

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;
  }
 }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值