一个仿Struts的MVC框架

这个框架包括四个类ActionServlet,ActionConfig,AcationMapping,Actionforword和一个接口Action。其web.xml的配置如下

<?xml version="1.0" encoding="UTF-8" ?>
- <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
- <servlet>
  <description>This is the description of my J2EE component</description>
  <display-name>This is the display name of my J2EE component</display-name>
  <servlet-name>ActionServlet</servlet-name>
  <servlet-class>com.controller.ActionServlet</servlet-class>
- <init-param>
  <param-name>config</param-name>
  <param-value>mvc-config.xml</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
  </servlet>
- <servlet-mapping>
  <servlet-name>ActionServlet</servlet-name>
  <url-pattern>*.do</url-pattern>
  </servlet-mapping>
  </web-app>


ActionConfig类如下:

public class ActionConfig {
 Object action;
 String method="execute";
 public Object getAction() {
  return action;
 }
 public void setAction(Object action) {
  this.action = action;
 }
 public String getMethod() {
  return method;
 }
 public void setMethod(String method) {
  this.method = method;
 }
 public ActionConfig()
 {
  super();
 }
 public ActionConfig(Object action)
 {
  super();
  this.action = action;
 }
 public ActionConfig(Object action, String mehtod)
 {
  this(action);
  this.method = mehtod;
 }
}

ActionMapping类如下:

import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
public class ActionMapping {
 static Hashtable<String, ActionConfig> actionMapping = new Hashtable<String, ActionConfig>();
 public static void putAction(String path, ActionConfig action) {
  actionMapping.put(path, action);
 }
 public static ActionConfig getAction(String path) {
  return actionMapping.get(path);
 }
 public static void loadMapping(String filepath) {
  SAXBuilder sax = new SAXBuilder();
  try {
   Document doc = sax.build(filepath);
   Element root = doc.getRootElement();
   List list = root.getChildren();
   for (Object object : list) {
    Element e = (Element) object;
    String path = e.getAttributeValue("path");
    String clazz = e.getAttributeValue("class");
    String method = e.getAttributeValue("method");
    Object obj = Class.forName(clazz).newInstance();
    ActionConfig ac = new ActionConfig();
    if (method != null && method.length() != 0) {
     ac.setMethod(method);
    } else {
     method = "execute";
    }
    ActionMapping.putAction(path, ac);
    System.out.println(clazz+"的"+method+"方法正在初始化......");
   }
  } catch (Exception e) {
   // TODO: handle exception
  }
 }
 private static String getAttributeValue(String string) {
  // TODO Auto-generated method stub
  return null;
 }
}

ActionForword类如下:

public class ActionForward {
 String url;
 boolean isDirect=false;
 public boolean isDirect() {
  return isDirect;
 }
 public void setDirect(boolean isDirect) {
  this.isDirect = isDirect;
 }
 public String getUrl() {
  return url;
 }
 public void setUrl(String url) {
  this.url = url;
 }
 public ActionForward()
 {
  super();
 }
 public ActionForward(String url)
 {
  super();
  this.url=url;
 }
 public ActionForward(String url,boolean isDirect)
 {
  this(url);
  this.isDirect=isDirect;
 }
}


ActionServlet类如下:

import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.controller.ActionForward;
public class ActionServlet extends HttpServlet {
 public ActionServlet() {
  super();
 }
 public void destroy() {
  super.destroy();
 }
 
 public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
   
 }
 public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
 }
 private void doProcessor(HttpServletRequest request, HttpServletResponse response)
 throws ServletException, IOException {
  try {
   String path =request.getServletPath();
   if (path.indexOf(".")!=-1) {
    path=path.substring(0,path.indexOf(".")); 
   }
   ActionConfig action = ActionMapping.getAction(path);
   if (action==null) {
    response.sendError(404,"action没有映射");
    return;
   }
   Object targer= action.getAction();
   Class targerclass = targer.getClass();
   String methodName = action.getMethod();
   Class[] argsType=new Class[]{HttpServletRequest.class,HttpServletResponse.class};
   Method method = targerclass.getMethod(methodName, argsType); 
   Object result=method.invoke(targer, new Object[]{request,response});
   ActionForward af=(ActionForward) result;
   if (af!=null && af.getUrl()!=null && af.getUrl().length()!=0)
   {
    if (af.isDirect())
    {
     response.sendRedirect(request.getContextPath()+af.getUrl());
    }
    else
    {
     request.getRequestDispatcher(af.getUrl()).forward(request, response);
    }
   }
  } catch (Exception e) {
   // TODO: handle exception
  }
 }
 public void init() throws ServletException {
  // Put your code here
 }
}

Action接口的代码如下:

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.controller.ActionForward;
public interface Action {
 public ActionForward execute(HttpServletRequest request,HttpServletResponse response);
}

MVC-Config.xml的配置如下:

<mvc>
   <action path="" class="" method=""/>
</mvc>




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值