基于Java注解的MVC例子

一直以来对Spring MVC的巧妙设计所折服,花了几头时间研究一下,自己做了一个简单的demo,分享一下。

首先定义两个annotation,第一个annotation:Controller,表示这是个控制类,第二个annotation表示他是接受servlet的forward的方法。他们值得combine表示该handler的访问路径。

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;


@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Contoller {

	String value() default "";
}

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;


@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RequestMapping {


	String value() default "";
}

和Spring MVC一样,定义了一个DispatchServlet。


import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 分发Servlet
 * @author lhq
 *
 */
public class DispatcherServlet extends HttpServlet {
	
	Map<String,Method> methodMap = new HashMap<String,Method>();
	Map<String,Object> handlerMap = new HashMap<String,Object>();
	
	
	
	@Override
	public void init() throws ServletException {
		loadHandler();
	}
	
	private void loadMethods(Class<?> clazz,String name){
		Method [] methods = clazz.getMethods();
		for(Method method:methods){
			RequestMapping path = method.getAnnotation(RequestMapping.class);
			if(null!=path){
				methodMap.put(name+"/"+path.value(), method);
			}
		}
	}
	
	private void loadHandler()  {
		try {
			Properties properties = new Properties();
			InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("action.properties");
			properties.load(is);
			String[] handlerArray = properties.getProperty("actionClass").split(",");
			for(String handlerName:handlerArray){
				Class<?> handerClass = Class.forName(handlerName);
				Contoller contoller = (Contoller) handerClass.getAnnotation(Contoller.class);
				handlerMap.put(contoller.value(), handerClass.newInstance());
				loadMethods(handerClass, contoller.value());
			}
			System.out.println();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
	}
	
	

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		process(req,resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		process(req,resp);
	}
	
	protected void process(HttpServletRequest request, HttpServletResponse response)
		throws ServletException, IOException {
		String servletPath = request.getServletPath();
		servletPath = servletPath.substring(1);
		String [] actionAndMethod=servletPath.split("/");
		String actionName = actionAndMethod[0];
		Method method = methodMap.get(servletPath);
		Object action = handlerMap.get(actionName);
		try {
			method.invoke(action, request,response);
		} catch (Exception e) {
			e.printStackTrace();
		} 
	}
	
	
}


配DispatchServlet,和普通的Servlet项目不同,这里讲*.do的请求全部转发的该类,而不是每一个Servlet全都需要在web.xml配置,个人感觉在web.xml

配置是一件非常麻烦的事情。

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <display-name></display-name>	
  	<servlet>
		<servlet-name>DispatcherServlet</servlet-name>
		<servlet-class>DispatcherServlet</servlet-class>
	</servlet>

	<servlet-mapping>
		<servlet-name>DispatcherServlet</servlet-name>
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>
</web-app>


开始写控制类。

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Contoller("article")
public class ArticleContoller {

	@RequestMapping("add.do")//通过article/add.do便可访问该类
	public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException,
			IOException {
		System.out.println("-----article.add()----");
	}
	
	@RequestMapping("del.do")
	public void del(HttpServletRequest request, HttpServletResponse response) throws ServletException,
			IOException {
		System.out.println("-----article.del()----");
	}
}

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Contoller("user")
public class UserContoller {

	@RequestMapping("add.do")
	public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException,
			IOException {
		System.out.println("-----user.add()----");
	}
}


在Spring中是配置一个扫描路径,Spring会扫描该路径下的所有标记了@Controller的类加入到容器中,扫描某个包下有哪些类实现起来比较麻烦(个人感觉Java这点设计的不好),就通过配置的方式来确定Controller吧

actionClass=ArticleContoller,UserContoller

在启动tomcat,在地址栏输入:http://localhost:8080/jsp/article/del.do

便打印出:-----article.del()----

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值