目录
一.什么是MVC
1.1.三层架构和MVC的区别
二.自定义MVC工作原理图
三.自定义mvc实现
3.1 创建web工程
3.2 中央处理器
3.3 Action接口定义
3.4 实现子控制器
3.5 完善中央控制器
3.5.1 请求分发功能
3.5.2 使用配置文件配置action
3.5.3 请求参数处理
1. 定义接口
在中央处理器中加入请求参数的处理能力
2.处理请求参数中的null及空字符串转换问题
为了更方面的处理请求参数中的null及空字符串,转换为数值型数据的问题,加入一个监听器,在应用启动时注册转换器。
3.6 完善Action
1.构建抽象类
2.自定义的Action子控制器示例:
发送请求,示例http://localhost:8080/mvc/studentAction.actionmethodName=addStudent&sid=100&age=23&addr=aabbcc
四.其他公用组件的集成
五.打jar包
MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,一种软件设计典范。用一种业务逻辑、数据、界面显示分离的方法,将业务逻辑聚集到一个部件里面,在改进和个性化定制界面及用户交互的同时,不需要重新编写业务逻辑。
MVC结构
Model:是应用程序中用于处理应用程序数据逻辑的部分,通常模型对象负责在数据库中存取数据。
View:是应用程序中处理数据显示的部分,通常视图是依据模型数据创建的。
Controller:是应用程序中处理用户交互的部分,通常控制器负责从视图读取数据,控制用户输入,并向模型发送数据。
MVC是一个框架模式,它强制性的使应用程序的输入、处理和输出分开。使用MVC应用程序被分成三个核心部件:模型、视图、控制器。它们各自处理自己的任务。最典型的MVC就是JSP + servlet + javabean的模式
Model:常用javabean去实现,通过各种类来对数据库的数据进行获取,并封装在对象当中。
View:常用JSP来实现,通过可直接观察的JSP页面来展示我们从数据库中获取的数据。
Controller:常用servlet来实现,通过servlet来获取经过javabean包装过的对象(已存入数据库中的数据),然后再发送数据传输到JSP界面。
1)不能跨层调用; 2)只能由上往下进行调用:View -> Controller -> Model
三层架构是基于业务逻辑来分的,而MVC是基于页面来分的;
三层是种软件架构,通过接口实现编程,MVC模式是一种复合设计模式,一种解决方案;
三层架构模式是体系结构模式,MVC是设计模式;
三层架构模式又可归于部署模式,MVC可归于表示模式。
核心组件说明:
创建一个web工程,需要加入必要的依赖。
通过servlet来实现一个中央控制器,负责所有请求的接收。(后续中央控制器在再将请求转发给各个子控制器,此处可以先把请求接进来,转发功能后面再加)
/**
* 中央控制器,负责接收所有的请求并分别给控制器具体处理
* @author Administrator
*/
@WebServlet("*.action")
public class ActionDispatchServlet extends HttpServlet {
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) {
doPost(request, response);
}
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) {
System.out.println("dopost ..... ");
}
}
Action接口定义了每个子控制器需要遵循的行为,使得所有的子控制器都有一个同一的抽象类型,所以我们可以在中央控制器中使用Action接口类型来引用所有的子控制器。这样就为用户扩展自定义的子控制器提供了条件
/**
* 每个子控制器必须实现该接口,负责处理中央处理器分配的请求
* @author Administrator
*/
public interface Action {
/**
* 处理请求
* @param request 请求
* @param response 响应
* @return String 返回转发或重定向的jsp页面名称
*/
String exeute(HttpServletRequest request, HttpServletResponse response);
}
为方便调试,实现两个子控制器
public class BookAction implements Action {
@Override
public String exeute(HttpServletRequest request, HttpServletResponse response) {
return "bookList";
}
}
public class StudentAction implements Action {
@Override
public String exeute(HttpServletRequest request, HttpServletResponse response) {
// TODO Auto-generated method stub
return "students";
}
}
为了便于理解,我们可以分步骤的,循序渐进的完善中央控制器:
为了在中央控制器中完成请求的分发,需要在中央控制器中维护所有子控制器的实例,并且能够依据请求路径,将请求转发给与其关联的子控制器。
/**
* 中央控制器,负责接收所有的请求并分别给控制器具体处理
* @author Administrator
*/
@WebServlet("*.action")
public class ActionDispatchServlet extends HttpServlet {
//用于保存path与action子控制器的映射
public static Map actionMap = new HashMap<>();
static {
actionMap.put("/studentAction", new StudentAction());
actionMap.put("/bookAction", new BookAction());
}
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) {
doPost(request, response);
}
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) {
String servletPath = request.getServletPath();
String path = servletPath.split("\\.")[0];
Action action = actionMap.get(path);
String rpath = action.exeute(request, response);
System.out.println(rpath);
}
}
在上面的示例中,在中央控制器中直接创建action子控制器,如果新增一个子控制器需要在中央控制器中添加,这样并不实用。 为了增加灵活性,可以将action转移到配置文件中配置,中央控制器通过配置来初始化action子控制器。
1 此时需要将config.xml文件的解析和建模项目的功能集成进来。
(ConfigModel,ActionModel,ForwardModel,ConfigModelFactory)
2 在项目的src目录下加入如下配置文件(config.xml)
]>
完善中央处理器,通过配置文件来获取子控制器配置
@WebServlet("*.action")
public class ActionDispatchServlet extends HttpServlet {
//用于保存path与action子控制器的映射
//public static Map actionMap = new HashMap<>();
private static ConfigModel configModel;
static {
//actionMap.put("/students", new StudentAction());
//actionMap.put("/books", new BookAction());
configModel = ConfigModelFactory.getConfigModel();
}
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
doPost(request, response);
}
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
String servletPath = request.getServletPath();
String path = servletPath.split("\\.")[0];
Action action = getActionByPath(path);
String name = action.exeute(request, response);
ForwardModel forwardModel = getForwardModel(path, name);
if (forwardModel.isRedirect()) {
response.sendRedirect(request.getContextPath() + "/" + forwardModel.getPath());
} else {
request.getRequestDispatcher(forwardModel.getPath()).forward(request, response);
}
}
//通过请求路径获取对应的action实例
private Action getActionByPath(final String path) {
ActionModel action = configModel.find(path);
try {
Class> clazz = Class.forName(action.getType());
return (Action)clazz.newInstance();
} catch (Exception e) {
throw new RuntimeException("创建Action实例异常"+e.getMessage(), e);
}
}
public ForwardModel getForwardModel(String path, String name) {
return configModel.find(path).find(name);
}
}
注: 本例的实现中Action子控制器是多例模式的,及每个请求对应一个Action实例
/**
* 对于需要处理请求参数的Action可以通过实现该接口获取请求参数的
* 处理能力,中央控制器将会使用该接口来获取Model对象,并统一处理
* 参数
* @author Administrator
*/
public interface ModelDrive {
Object getModel();
}
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
......
Action action = getActionByPath(path);
//处理请求参数
if(action instanceof ModelDrive) {
Object model = ((ModelDrive) action).getModel();
if(model != null) {
try {
BeanUtils.populate(model, request.getParameterMap());
} catch (Exception e) {
throw new RuntimeException("在中央处理器中处理请求参数时发生异常", e);
}
}
}
String name = action.exeute(request, response);
......
}
/**
* ServletContextListener接口为Servlet API中的接口,用于监听ServletContext对象的生命周期。
* 当Servlet 容器启动或终止Web 应用时,会触发ServletContextEvent事件,该事件由
* ServletContextListener来处理。
* @author Administrator
*/
@WebListener
public class BeanUtilsListener implements ServletContextListener {
/**
* 当Servlet 容器终止Web 应用时调用该方法。在调用该方法之前,
* 容器会先销毁所有的Servlet和Filter 过滤器。
*/
@Override
public void contextDestroyed(ServletContextEvent arg0) {
// TODO Auto-generated method stub
}
/**
* 当Servlet 容器启动Web应用时调用该方法。
* 在调用完该方法之后,容器再对Filter 初始化,
* 并且对那些在Web应用启动时就需要被初始化的Servlet进行初始化。
*/
@Override
public void contextInitialized(ServletContextEvent arg0) {
ConvertUtils.register(new IntegerConverter(null), Integer.class);
ConvertUtils.register(new FloatConverter(null), Float.class);
ConvertUtils.register(new DoubleConverter(null), Double.class);
ConvertUtils.register(new LongConverter(null), Long.class);
ConvertUtils.register(new BigDecimalConverter(null), BigDecimal.class);
}
}
每个Action只能有一个execute方法,如果处理一个模块的增删改查则需要单独编写多个Action,这样会比较麻烦。如果在一个Action实例中可以处理多个请求方法,则框架会更加灵活。
public abstract class AbstractDispatchAction implements Action {
@Override
public String exeute(HttpServletRequest request, HttpServletResponse response) {
String methodName = request.getParameter("methodName");
Class extends AbstractDispatchAction> clazz = this.getClass();
try {
Method method = clazz.getDeclaredMethod(
methodName,
HttpServletRequest.class,
HttpServletResponse.class);
return (String)method.invoke(this, request,response);
} catch (Exception e) {
throw new RuntimeException("在调用Action中的["+methodName+"]方法是异常", e);
}
}
}
public class StudentAction extends AbstractDispatchAction implements ModelDrive {
private Student student = new Student();
@Override
public Object getModel() {
return student;
}
/*@Override
public String exeute(HttpServletRequest request, HttpServletResponse response) {
System.out.println("StudentAction = " + student);
return "students";
}*/
public String getStudents(HttpServletRequest request, HttpServletResponse response) {
System.out.println("getStudents");
System.out.println("StudentAction = " + student);
return "students";
}
public String addStudent(HttpServletRequest request, HttpServletResponse response) {
System.out.println("addStudent");
System.out.println("add student = " + student);
return "students";
}
}
注意:在请求中需要添加一个methodName的固定参数,该参数指定了需要调用的Action中的方法的名称。
可以将通用分页,字符编码过滤器等组件一集成到mvc框架中便于复用。
将自定义mvc框架打成jar包,以便于在其他项目中使用。
项目 --(右击)-->Export