自定义mvc框架
什么是mvc
MVC全名:Model View Controller,其中Model(模型层)、View(视图层、Controller(控制层)
它是一种软件设计典范,用于业务逻辑处理、数据、界面显示分离
三层架构与mvc的区别
三层架构是一个经典的分层思想,将开发模式分为三层,每个人专注自己擅长模块即可。
MVC是一种设计模式,其目的是让html和业务逻辑分开
加减乘除案例
3.1先建一个包放中央控制器
package com.luojun.mvc.framework;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.luojun.mvc.action.AddAction;
import com.luojun.mvc.action.HelloAction;
/**
* 自定义mvc中的中央控制器
* @author 罗俊
*
*/
public class ActionServlet extends HttpServlet {
private Map<String, Action> map;
@Override
public void init() throws ServletException {
map=new HashMap<String, Action>();
//map.put("/helloAction", new HelloAction());
map.put("/addAction", new AddAction());
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1、获取请求路径
// /jsp_mvc/helloaction.action
String requestURI = req.getRequestURI();
System.out.println(requestURI);
//System.out.println(req.getRequestURL());
int start=requestURI.lastIndexOf("/");
int end=requestURI.lastIndexOf(".");
//2、截取请求路径中的/jsp_mvc/*.action 的/*部分
String actionName = requestURI.substring(start,end);
System.out.println(actionName);
//通过截取路径中的actionName找到对应的子控制器
Action action=map.get(actionName);//等于 new HelloAction()
//4、执行execute方法
action.execute(req, resp);
}
}
3.2在建一个控制器的父类 ,完成具体的业务逻辑
package com.luojun.mvc.framework;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 控制器的父类 完成具体的业务逻辑
* @author 罗俊
*
*/
public abstract class Action {
public abstract String execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException;
}
3.3配置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>jsp_mvc</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- 中央控制器 -->
<servlet>
<servlet-name>ActionServlet</servlet-name>
<servlet-class>com.luojun.mvc.framework.ActionServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ActionServlet</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
</web-app>
3.4在子控制器子类里实现加减乘除操作
package com.luojun.mvc.action;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.luojun.mvc.framework.Action;
public class AddAction extends Action {
@Override
public String execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String num1=req.getParameter("num1");
String num2=req.getParameter("num2");
int num3=0;
String fh=req.getParameter("fh");
if("+".equals(fh)) {
num3=Integer.parseInt(num1)+Integer.parseInt(num2);
}
if("-".equals(fh)) {
num3=Integer.parseInt(num1)-Integer.parseInt(num2);
}
if("*".equals(fh)) {
num3=Integer.parseInt(num1)*Integer.parseInt(num2);
}
if("/".equals(fh)) {
num3=Integer.parseInt(num1)/Integer.parseInt(num2);
}
req.setAttribute("num3", num3);
req.getRequestDispatcher("result.jsp").forward(req, resp);
return null;
}
}
运行结果