自定义MVC
什么是MVC?
MVC全名:Model View Controller,其中Model(模型层)、View(视图层)、Controller(控制层)
它是一种软件设计典范,用于业务逻辑处理、数据、界面显示分离。
常用模式:
model1:jsp+jdbc 这种就是把所有带码放在JSP界面上
model2:mvc 核心思想各司其职
M : 模型 entity、dao(调用数据层)
V:视图 JSP/HTML/freemarker ( 用户看到并与之交互的界面)
C : 控制器 Servlet/Action/Controller(接受用户输入并调用模型和视图去完成用户需求)
还有一点需要注意的是:1.不能跨层调用 2.只能出先由上而下的调用
当然也有特点的MVC模式可以简化我们很多代码 节省我们很多编码时间
MVC实现
接下来我们要实现一个MVC模式的运行 老样子我们先导入所需要的三个架包 当然这次用不到 下次会用到 还是先导入吧
要开始写MVC模式我们要先了解下自定义mvc框架工作原理图 浏览器通过配置*.action 发送一个请求到ActonServlet中央控制器 再拿到请求路径截取出 * 的内容再拿到 Action里面 调用execute方法 再到视图界面呈现
那么思路有了现在开始了
新建一个中央控制器ActionServlet 写上一个初始化方法init() 和调用doget 和dopost方法
还要写一个静态的map集合
private Map<String, Action> map;
然后配置再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>J2EE_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>
<!-- 配置ActionServlet 中央控制器-->
<servlet>
<servlet-name>ActionServlet</servlet-name>
<servlet-class>com.zhuchenxi.framework.ActionServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ActionServlet</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
</web-app>
配置好了我们开始写Acton 控制器的父类 用来完成具体的业务逻辑
package com.zhuchenxi.framework;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 控制器的父类 完成具体的业务逻辑
* @author 小曦
*
* http://www.javaxl.com
*/
public abstract class Action{
/**
* 所有子控制器继承了 都要写execute方法
* @param req
* @param resp
* @return
* @throws ServletException
* @throws IOException
*/
public abstract String execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException;
}
好了之后我们写一个HelloAction 并且继承Action
必须要继承父类Action才算是一个子控制器
package com.zhuchenxi.action;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.zhuchenxi.framework.Action;
/**
* 必须要继承父类Action才算是一个子控制器
* @author 小曦
*
* http://www.javaxl.com
*/
public class HelloAction extends Action {
@Override
public String execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("青依");
return null;
}
}
东西都有了之后我们需要再中央控制器里面的dopost方法获取到请求路径 输出一下是方面看
//获取请求路径
String uri = req.getRequestURI();
System.out.println(uri);
找到路径中最后一个/ 开始截取位置
int start = uri.lastIndexOf("/");
最后截取位置
int end = uri.lastIndexOf(".action");
通过截取路径中的actionName找到对应的子控制器 这里还用了里氏替换
Action action =map.get(actionName);
执行execute方法
action.execute(req, resp);
截取完成了 方法也调用了 这时候还要在init()初始化里面实例化一个map 并用put找到内容
/**
* 初始化
*/
public void init() throws ServletException {
map = new HashMap<String, Action>();
map.put("/helloAction", new HelloAction());
map.put("/addAction", new AddAction());
}
运行在浏览器跳转到
会在Eclipse里面输出 就算成功了
这里还要注意 因为是直接在ActionServlet里面运行的没有数据所以会报空指针 要在网页上面运行就不会了 还有 配置的是/helloAction是小写 不要再网页上写成大写了
加减乘除的Action
写一个addAction 里面获取到 add.jsp传过来的值
" +" " - " " * " " / " 还有两个值 再在里面进行判断 保存到setAttribute里面 转发到页面上就行
package com.zhuchenxi.action;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.zhuchenxi.framework.Action;
public class AddAction extends Action {
@Override
public String execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
int num3=0;
String a = req.getParameter("+");
String b = req.getParameter("-");
String c = req.getParameter("*");
String d = req.getParameter("/");
String num1 = req.getParameter("num1");
String num2 = req.getParameter("num2");
if("+".equals(a)) {
num3 =Integer.parseInt(num1) + Integer.parseInt(num2);
}
if("-".equals(b)) {
num3 =Integer.parseInt(num1) - Integer.parseInt(num2);
}
if("*".equals(c)) {
num3 =Integer.parseInt(num1) * Integer.parseInt(num2);
}
if("/".equals(d)) {
num3 =Integer.parseInt(num1) / Integer.parseInt(num2);
}
req.setAttribute("num3", num3);
req.getRequestDispatcher("add.jsp").forward(req, resp);
return null;
}
}
最后补上我们的add.jsp界面
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="addAction.action" method="post">
num1:<input name="num1" /><br/>
num2:<input name="num2" /><br/>
<input type="submit" value="+" name="+" />
<input type="submit" value="-" name="-" />
<input type="submit" value="*" name="*" />
<input type="submit" value="/" name="/" />
</form>
<h2>${num3 }</h2>
</body>
</html>
在页面上运行的效果
增加
减法
乘法
除法
没有报错的感觉真好 有什么问题请留言~~~~