SpringMVC
MVC
m:model:模型,javabean
v:view:视图,html/jsp
c:controller:控制器:servlet
MyMVC模拟实现
⼀. 阶段⼀
- index.html⻚⾯
<!DOCTYPE html>
<html lang="en"> <head>
<meta charset="UTF-8">
<title>index</title>
</head> <body>
<h1>this is index page.</h1>
<form method="post" action="ProductServlet">
pid:<input type="text" name="pid" /><br />
pname:<input type="text" name="pname" /><br />
price:<input type="text" name="price" /><br />
img:<input type="text" name="img" /><br />
<input type="submit" value="submit" /><br />
</form>
</body>
</html>
- ProductServlet.java
package com.qfedu.controller;
import com.qfedu.bean.Product;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@javax.servlet.annotation.WebServlet(urlPatterns = "/ProductServlet")
public class ProductServlet extends HttpServlet {
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
//System.out.println(123);
String pid = request.getParameter("pid");
String pname = request.getParameter("pname");
String sprice = request.getParameter("price");
String img = request.getParameter("img");
double price = sprice == null ? 0.0 :
Double.parseDouble(sprice);
System.out.println("pid : " + pid);
System.out.println("price : " + sprice);
System.out.println("pname : " + pname);
System.out.println("img : " + img);
Product p = new Product();
p.setPid(pid);
p.setPname(pname);
p.setImg(img);
p.setPrice(price);
request.setAttribute("p", p);
request.getRequestDispatcher("product.jsp").forward(request,response);
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
- Product.jsp
可以实现⻚⾯的跳转以及数据的展示
问题:⻚⾯都在webapp下,安全性不⾼
<%--
Created by IntelliJ IDEA.
User: james
Date: 2020/3/2
Time: 2:52 PM
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>product</title>
</head>
<body>
<h1>this is product detail page.</h1>
<%--
ognl: 对象导航语言
user.addr.province;
--%>
<h3>pid : ${p.pid}</h3>
<h3>pname : ${p.pname}</h3>
<h3>price : ${p.price}</h3>
<h3>img : ${p.img}</h3>
</body>
</html>
⼆.阶段⼆
为了提⾼程序的安全性,将所有的⻚⾯都放⼊webapp/WEB-INF/view⽬录下,index.html⻚⾯⽆法
直接访问,需要专⻔建⽴⼀个Sevlet来访问该⻚⾯,实现内容请求转发
新建⼀个Servlet来实现内部转发 ProductInputServlet.java
package com.qfedu.controller;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet(urlPatterns = "/ProductInputServlet")
public class ProductInputServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.getRequestDispatcher("/WEB-INF/view/index.html").forward(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
webapp下的WEB-INF/view/index.html⻚⾯可以直接访问,更新ProductServlet.java
package com.qfedu.controller;
import com.qfedu.bean.Product;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@javax.servlet.annotation.WebServlet(urlPatterns = "/ProductServlet")
public class ProductServlet extends HttpServlet {
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
//System.out.println(123);
String pid = request.getParameter("pid");
String pname = request.getParameter("pname");
String sprice = request.getParameter("price");
String img = request.getParameter("img");
double price = sprice == null ? 0.0 :
Double.parseDouble(sprice);
三. 阶段三,模拟Spring MVC的具体实现
- Controller.java
package com.qfedu.mvc.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 自定义接口,实现管理多个请求,在模拟Spring MVC中的控制器
*/
public interface Controller {
String handleRequest(HttpServletRequest request, HttpServletResponse response);
}
2.ProductInputController.java
package com.qfedu.mvc.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ProductInputController implements Controller {
@Override
public String handleRequest(HttpServletRequest request, HttpServletResponse response) {
return "/WEB-INF/view/index.html";
}
}
3.ProductDetailController.java
package com.qfedu.mvc.controller;
import com.qfedu.bean.Product;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ProductDetailController implements Controller {
@Override
public String handleRequest(HttpServletRequest request, HttpServletResponse response) {
String pid = request.getParameter("pid");
String pname = request.getParameter("pname");
String sprice = request.getParameter("price");
String img = request.getParameter("img");
double price = sprice == null ? 0.0 : Double.parseDouble(sprice);
System.out.println("pid : " + pid);
System.out.println("price : " + sprice);
System.out.println("pname : " + pname);
System.out.println("img : " + img);
Product p = new Product();
p.setPid(pid);
p.setPname(pname);
p.setImg(img);
p.setPrice(price);
request.setAttribute("p", p);
return "/WEB-INF/view/product.jsp";
}
}
4.DispatcherServlet.java负责将多个请求分发给各自不同的控制器
package com.qfedu.mvc.controller;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet(urlPatterns = {"/ProductInput", "/ProductDetail"})
public class DispatcherServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String requestURI = request.getRequestURI();
//System.out.println(requestURI);
String action = requestURI.substring(requestURI.lastIndexOf("/") + 1);
//System.out.println(action);
Controller controller = null;
if("ProductInput".equalsIgnoreCase(action)){
controller = new ProductInputController();
}else if("ProductDetail".equalsIgnoreCase(action)){
controller = new ProductDetailController();
}
String url = controller.handleRequest(request, response);
request.getRequestDispatcher(url).forward(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
结论:多个请求可以交给同一个Servlet(DispatcherServlet),然后各自不同的请求会交给各自的控制器。所有请求都叫个DispatcerServet,该角色相当于一个前端控制器,可以大大的提高客户的以及服务器端的工作效率
四.阶段四
给阶段三的功能之上添加一个校验功能
新增一个ProductForm.java,主要用来做Product校验,struts1有专门的formbean对象用来校验表单数据,在某些框架中将formbean与bean对象合二为一了
package com.qfedu.form;
public class ProductForm {
private String pname;
private double price;
private String img;
public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getImg() {
return img;
}
public void setImg(String img) {
this.img = img;
}
}
ProductValidate.java,用来完成对于formbean进行校验,如果校验不通过,则将错误信息存储起来
package com.qfedu.validate;
import com.qfedu.form.ProductForm;
import java.util.ArrayList;
import java.util.List;
public class ProductValidate {
/**
* 校验给定的ProductForm对象
* @param pf 要校验的对象
* @return 表单校验不成功,则将错误信息存储
*/
public List<String> validate(ProductForm pf){
List<String> errors = null;
String pname = pf.getPname();
String img = pf.getImg();
double price = pf.getPrice();
errors = new ArrayList<>();
if(pname == null || pname.length() == 0){
errors.add("product name must not be empty.");
}
if(img == null || img.length() == 0){
errors.add("product image must not be empty.");
}
if(price < 0){
errors.add("the price of product must be a positive number.");
}
return errors;
}
}
更新ProductDetailController.java
package com.qfedu.mvc.controller;
import com.qfedu.ProductValidate;
import com.qfedu.bean.Product;
import com.qfedu.form.ProductForm;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
public class ProductDetailController implements Controller {
@Override
public String handleRequest(HttpServletRequest request, HttpServletResponse response) {
String pid = request.getParameter("pid");
String pname = request.getParameter("pname");
String sprice = request.getParameter("price");
String img = request.getParameter("img");
double price = sprice == null ? 0.0 : Double.parseDouble(sprice);
System.out.println("pid : " + pid);
System.out.println("price : " + sprice);
System.out.println("pname : " + pname);
System.out.println("img : " + img);
ProductForm pf = new ProductForm();
pf.setImg(img);
pf.setPname(pname);
pf.setPrice(price);
ProductValidate pv = new ProductValidate();
List<String> errors = pv.validate(pf);
try {
if(errors != null && !errors.isEmpty()){
request.setAttribute("errors", errors);
return "/WEB-INF/view/index.jsp";
}else{
Product p = new Product();
p.setPid(pid);
p.setPname(pf.getPname());
p.setImg(pf.getImg());
p.setPrice(pf.getPrice());
request.setAttribute("p", p);
}
} catch (Exception e) {
e.printStackTrace();
}
return "/WEB-INF/view/product.jsp";
}
}
index.jsp文件
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>product</title>
<!-- 引入 Bootstrap -->
<link href="https://cdn.bootcss.com/twitter-bootstrap/3.3.7/css/bootstrap-theme.css" rel="stylesheet">
<link href="https://cdn.bootcss.com/twitter-bootstrap/3.3.7/css/bootstrap.css" rel="stylesheet">
</head>
<body>
<h1>this is product inner index jsp page</h1>
<c:if test="${errors!=null}">
<ul>
<c:forEach items="${errors}" var="e">
<li><font color="red">${e}</font> </li>
</c:forEach>
</ul>
</c:if>
<!--<form method="post" action="/ProductServlet">-->
<form role="form" method="post" action="/ProductDetail">
<div class="form-group">
pid:<input type="text" class="form-control" name="pid" placeholder="请输入编号"><br>
pname:<input type="text" class="form-control" name="pname" placeholder="请输入用户名"><br>
price:<input type="text" class="form-control" name="price" placeholder="请输入价格"><br>
img:<input type="text" class="form-control" name="img" placeholder="请输入上传格式正确的图片"><br>
<input type="submit" value="submit"><br>
</div>
</form>
<script src="https://code.jquery.com/jquery.js"></script>
<!-- 包括所有已编译的插件 -->
<script src="https://cdn.bootcss.com/twitter-bootstrap/3.3.7/js/bootstrap.js"></script>
</body>
</html>
五.转发与重定向
转发(服务端行为)
形式:
request.getRequestDispatcher().forward(request,response)
转发在服务器端发挥作用,通过forward()方法提交信息在多个页面之间进行传递。
1.地址栏不会改变
2.转发只能转发到当前Web应用内的资源
3.在转发过程中,可以将数据保存到request域对象中去
4.转发只有一次请求
5.转发是服务器端行为
转发过程
1.客户端浏览器发送http
2.web浏览器接受请求
3.调用内部的一个方法在容器内部完成请求处理和转发动作
需注意的是:转发的路径必须是同一个web容器下的url。在客户端浏览器路径栏显示的仍然是第一次访问的路径。转发行为是浏览器只做了一次访问请求。
重定向(客户端行为)
形式:response.sendRedirect("");
重定向地址栏会改变
重定向可以跳转到当前web应用,甚至是外部域名网站
不能在重定向的过程中,将数据保存到request域对象中。
重定向过程
1.客户端发送http请求,
2.web服务器接收后,发送302状态吗相应以及新的location给客户端浏览器
3.客户端浏览器发现是302响应,则自动发送一个http请求,请求url为重定向的地址,响应的服务器根据此请求寻找资源并发送给客户。