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
<%--
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下,安全性不高
二. 阶段二
为了提高程序的安全性,将所有的页面都放入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);
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);
request.getRequestDispatcher("/WEB-INF/view/product.jsp").forward(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
三. 阶段三,模拟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);
}
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";
}
}
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";
}
}
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";
}
}
自定义的MVC框架也就有了校验功能