1、Action接口

package com.togogo.webtoservice;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public interface Action { /**
* 默认的执行方法
* @param request 传入处理的请求对象
* @param response 传入处理的响应对象
* @return 返回一个跳转的路径
*/
public String execute(HttpServletRequest request,
HttpServletResponse response); }

2、BaseAction的父类

package com.togogo.webtoservice;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public abstract class BaseAction implements Action { @Override
public String execute(HttpServletRequest request,
HttpServletResponse response) {
String result = null;
String uri = request.getRequestURI();
StringBuilder sb = new StringBuilder(uri);
String path = sb.substring(uri.lastIndexOf("!") + 1,
uri.lastIndexOf("."));
// 已知,方法与uri的参数的字符串,一样的,那么有没有方法,可以通过方法名(字符串)直接调用方法?
// 实现通过方法名,直接调用方法,就类动态技术
try {
Method method = this.getClass().getDeclaredMethod(path,
HttpServletRequest.class, HttpServletResponse.class);
result = (String) method.invoke(this, request, response);
} catch (NoSuchMethodException e) { e.printStackTrace();
} catch (SecurityException e) { e.printStackTrace();
} catch (IllegalAccessException e) { e.printStackTrace();
} catch (IllegalArgumentException e) { e.printStackTrace();
} catch (InvocationTargetException e) { e.printStackTrace();
} return result;
} }

3、配置类

package com.togogo.webtoservice;

import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle; /**
* 路径与类名映身
*
* @author Administrator
*
*/
public class Config {
// private static Map<String, String> classNames = new HashMap<String,
// String>(); /**
* 通过key
*
* @param key
* @return
*/
public static String getClassName(String key, String config) {
ResourceBundle bundle = null;
if (config == null||"".equals(config)) {
bundle = ResourceBundle.getBundle("Action");
} else {
bundle = ResourceBundle.getBundle(config);
}
return bundle.getString(key);
} // static {
// classNames.put("/user", "com.togogo.action.UserAction");
// classNames.put("/admin", "com.togogo.action.AdminAction");
// } public static void main(String[] args) {
// 用于以类的形式来读取properties对象
ResourceBundle bundle = ResourceBundle
.getBundle("com.togogo.webtoservice.Action");
System.out.println(bundle.getString("user"));
} }

4、DispacherServlet 类

package com.togogo.webtoservice;

import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class DispacherServlet extends HttpServlet {
private static String config=null;
private static String enconding="UTF-8"; @Override
public void init(ServletConfig config) throws ServletException {
String initConfig=config.getInitParameter("config");
String initEnconding=config.getInitParameter("enconding");
if(initConfig!=null){
DispacherServlet.config=initConfig;
}
//通过web描述符文件web.xml配置
if(initEnconding!=null){
DispacherServlet.enconding=initEnconding;
} super.init(config);
} /**
* 接收任何请求,
*/
@Override
protected void service(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
try {
request.setCharacterEncoding(enconding);
response.setCharacterEncoding(enconding);
String uri = request.getRequestURI();
StringBuilder sb = new StringBuilder(uri);
String path = sb.substring(uri.lastIndexOf("/") + 1,
uri.lastIndexOf("!"));
System.out.println(path);
String className = Config.getClassName(path,config);
Action action = ActionFactory.create(className);
String result = action.execute(request, response); if (result.contains(":")) {
StringBuilder resultStr = new StringBuilder(result);
String rePath=resultStr.substring(result.indexOf(":")+1, result.length());
if(result.contains("redirect:")){
response.sendRedirect(rePath);
}else if(result.contains("forward:")){
request.getRequestDispatcher(rePath).forward(request, response);
} } else {
request.getRequestDispatcher(result).forward(request, response);
}
} catch (Exception e) {
e.printStackTrace();
} } }

5、Action工厂类

package com.togogo.webtoservice;
public class Factory { public static Action create(String className) {
Action action = null;
try {
action = (Action) Class.forName(className).newInstance();
} catch (ClassNotFoundException e) { e.printStackTrace();
} catch (InstantiationException e) { e.printStackTrace();
} catch (IllegalAccessException e) { e.printStackTrace();
}
return action;
}
}

6、测试TestAction类

package com.togogo.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import com.togogo.webtoservice.BaseAction; public class UserAction extends BaseAction { /**
* 用户登录
*
* @param request
* @param response
* @return
*/
public String login(HttpServletRequest request, HttpServletResponse response) {
System.out.println("我在登录"); System.out.println("我在------登录..");
return "main.jsp";
} /**
* 用户注册
*
* @param request
* @param response
* @return
*/
public String register(HttpServletRequest request,
HttpServletResponse response) {
System.out.println("我在注册");
return "forward:main.jsp";
} /**
* 用户注册
*
* @param request
* @param response
* @return
*/
public String undo(HttpServletRequest request,
HttpServletResponse response) {
System.out.println("我在撤消");
return "redirect:main.jsp";
} }

Action.properties映射文件

user=com.togogo.action.UserAction

web.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>web_mvc_webtoservice</display-name>
<!-- 这是一个核心控制器 -->
<servlet>
<servlet-name>dispacherServlet</servlet-name>
<servlet-class>com.togogo.webtoservice.DispacherServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>com.togogo.action.Action</param-value>
</init-param>
</servlet> <servlet-mapping>
<servlet-name>dispacherServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping> <welcome-file-list>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>

  

测试页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a></a>
<form action="${pageContext.request.contextPath }/user!register.do" method="post">
<!-- <input name="clasName" value="com.togogo.action.UserAction"> -->
<input type="submit"> </form>
</body>
</html>

源码下载:

http://files.cnblogs.com/files/zhuyuejiu/web_mvc_webtoserive.zip

一个简单的MVC框架的实现的更多相关文章

  1. 自己动手写一个简单的MVC框架(第一版)

    一.MVC概念回顾 路由(Route).控制器(Controller).行为(Action).模型(Model).视图(View) 用一句简单地话来描述以上关键点: 路由(Route)就相当于一个公司 ...

  2. AsMVC:一个简单的MVC框架的Java实现

    当初看了<从零开始写一个Java Web框架>,也跟着写了一遍,但当时学艺不精,真正进脑子里的并不是很多,作者将依赖注入框架和MVC框架写在一起也给我造成了不小的困扰.最近刚好看了一遍sp ...

  3. 自己动手写一个简单的MVC框架(第二版)

    一.ASP.NET MVC核心机制回顾 在ASP.NET MVC中,最核心的当属“路由系统”,而路由系统的核心则源于一个强大的System.Web.Routing.dll组件. 在这个System.W ...

  4. 一个简单的MVC框架的实现-基于注解的实现

    1.@Action注解声明 package com.togogo.webtoservice.annotations; import java.lang.annotation.Documented; i ...

  5. PHP之简单实现MVC框架

    PHP之简单实现MVC框架   1.概述 MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,一种软件设计典范,用一种 ...

  6. [.NET] 一步步打造一个简单的 MVC 网站 - BooksStore(一)

    一步步打造一个简单的 MVC 网站 - BooksStore(一) 本系列的 GitHub地址:https://github.com/liqingwen2015/Wen.BooksStore 简介 主 ...

  7. [.NET] 一步步打造一个简单的 MVC 电商网站 - BooksStore(二)

    一步步打造一个简单的 MVC 电商网站 - BooksStore(二) 本系列的 GitHub地址:https://github.com/liqingwen2015/Wen.BooksStore 前: ...

  8. 自己实现的一个简单的EF框架(反射实现)

    我实现了一个简单的EF框架,主要用于操纵数据库.实现了对数据库的基本操纵--CRUD 这是项目结构 这是一个 core 下的 DLL 写了一个数据库工厂,用于执行sql语句.调用sql语句工厂 写了一 ...

  9. [.NET] 一步步打造一个简单的 MVC 电商网站 - BooksStore(一)

    一步步打造一个简单的 MVC 电商网站 - BooksStore(一) 本系列的 GitHub地址:https://github.com/liqingwen2015/Wen.BooksStore &l ...

随机推荐

  1. Maximum repetition substring (poj3693 后缀数组求重复次数最多的连续重复子串)

    Maximum repetition substring Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6328   Acc ...

  2. swiper拖拽之后不自动滑动问题

    //swiper轮播图 var mySwiper = new Swiper('.swiper-container',{ initialSlide :0, autoplay : 3000, direct ...

  3. JDFS:一款分布式文件管理系统,第五篇(整体架构描述)

    一 前言 截止到目前为止,虽然并不完美,但是JDFS已经初步具备了完整的分布式文件管理功能了,包括:文件的冗余存储.文件元信息的查询.文件的下载.文件的删除等.本文将对JDFS做一个总体的介绍,主要是 ...

  4. wireshark数据包分析实战 第二章

    1,监听网络线路:即嗅探器的位置确定. 2,混杂模式:将网卡设置成混杂模式,网卡可以接受经过网卡的所有数据报,包括目的地址不是本网卡的数据报.这些数据都会发送给cpu处理,这样,wireshark就能 ...

  5. LCT学习笔记

    最近自学了一下LCT(Link-Cut-Tree),参考了Saramanda及Yang_Zhe等众多大神的论文博客,对LCT有了一个初步的认识,LCT是一种动态树,可以处理动态问题的算法.对于树分治中 ...

  6. DataRow和DataRowView的区别

    可以将DataView同数据库的视图类比,不过有点不同,数据库的视图可以跨表建立视图,DataView则只能对某一个DataTable建立视图. DataView一般通过DataTable.Defau ...

  7. Masonry框架源码深度解析

    Masonry是iOS在控件布局中经常使用的一个轻量级框架,Masonry让NSLayoutConstraint使用起来更为简洁.Masonry简化了NSLayoutConstraint的使用方式,让 ...

  8. Rythm.js 使用教程详解

    转载自 http://blog.csdn.net/qq_26536483/article/details/78261515 简介 rythm.js是一款让页面元素跳动起来的插件,并且带音乐,共7种用法 ...

  9. Android 屏幕相关概念(1)

    1.  术语和概念 术语 说明 备注  Screen size(屏幕尺寸)  指的是手机实际的物理尺寸,比如常用的2.8英寸,3.2英寸,3.5英寸,3.7英寸  摩托罗拉milestone手机是3. ...

  10. Guava快速入门

    Guava快速入门 Java诞生于1995年,在这20年的时间里Java已经成为世界上最流行的编程语言之一.虽然Java语言时常经历各种各样的吐槽,但它仍然是一门在不断发展.变化的语言--除了语言本身 ...