1.Spring mvc
MVC
m:model:模型,javabean
v:view:视图,html/jsp
c:controller:控制器:servlet
为了提高程序的安全性,将所有的页面都放入webapp/WEB-INF/view目录下,index.html页面无法直接访问,需要专门建立一个Sevlet来访问该页面,实现内容请求转发
index.html页面
<html lang="en">
<head>
<meta charset="UTF-8">
<title>index</title>
</head>
<body>
<h1>this is index.html</h1>
<form action="/ProductDetail" method="post">
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">
</form>
</body>
</html>
prodoct.jsp
<%--
Created by IntelliJ IDEA.
User: 91yuwenle
Date: 2020/3/2
Time: 20:36
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.jsp</h1>
<h3>pid:${pt.pid}</h3>
<h3>pname:${pt.pname}</h3>
<h3>price:${pt.price}</h3>
<h3>img:${pt.img}</h3>
</body>
</html>
1.自定义一个Contorller接口 Controller.java
package com.jinFang.mvc.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public interface Controller {
String handleRequest(HttpServletRequest request, HttpServletResponse response);
}
2.不同实现类分别实现Contorller接口
1.ProductInputController.java
package com.jinFang.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";
}
}
2.ProductDetailController.java
package com.jinFang.mvc.controller;
import com.jinFang.bean.Product;
import com.jinFang.form.ProductForm;
import com.jinFang.validate.ProductValidate;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
public class ProductDetailController implements Controller {
@Override
public String handleRequest(HttpServletRequest request, HttpServletResponse response) {
String spid=request.getParameter("pid");
String pname=request.getParameter("pname");
String sprice=request.getParameter("price");
String img=request.getParameter("img");
int pid=spid==null?0:Integer.parseInt(spid);
Double price=sprice==null?0.0:Double.parseDouble(sprice);
ProductForm pf=new ProductForm();
pf.setPname(pname);
pf.setPrice(price);
pf.setImg(img);
ProductValidate pv = new ProductValidate();
List<String> errors=pv.validate(pf);
if (errors!=null && !errors.isEmpty()){
request.setAttribute("errors", errors);
return "/WEB-INF/view/index.jsp";
}else {
Product pt=new Product();
pt.setPid(pid);
pt.setPname(pname);
pt.setPrice(price);
pt.setImg(img);
request.setAttribute("pt",pt);
return "/WEB-INF/view/product.jsp";
}
}
}
3.DispatcherServlet.java 负责将多个请求分发给各自不同的控制器
package com.jinFang.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 urlPattern = request.getRequestURI();
String action = urlPattern.substring(urlPattern.lastIndexOf("/")+1);
Controller coll=null;
if ("ProductInput".equalsIgnoreCase(action)){
coll=new ProductInputController();
}else if ("ProductDetail".equalsIgnoreCase(action)){
coll=new ProductDetailController();
}
String url=coll.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.jinFang.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;
}
@Override
public String toString() {
return "ProductForm{" +
"pname='" + pname + '\'' +
", price=" + price +
", img='" + img + '\'' +
'}';
}
}
2.ProductValidate.java,用来完成对于formbean进行校验,如果校验不通过,则将错误信息存储起来
package com.jinFang.validate;
import com.jinFang.form.ProductForm;
import java.util.ArrayList;
import java.util.List;
public class ProductValidate {
public List <String> validate(ProductForm productForm){
List<String> errors=null;
String pname = productForm.getPname();
Double price = productForm.getPrice();
String img = productForm.getImg();
errors=new ArrayList<>();
if (pname==null||pname.length()==0){
errors.add("product name must not be empty.");
}
if (price==null||price<0){
errors.add("product price must not be error.");
}
if (img==null||img.length()==0){
errors.add("product img must not be empty.");
}
return errors;
}
}
更新过程已在ProductDetailController.java