1. model模型
這是一個具體的JavaBean類,該類提供了私有屬性的getter和setter屬性
Product.java
public class Product {
private String pid;
private String name;
private String img;
private double price;
public String getPid() {
return pid;
}
public void setPid(String pid) {
this.pid = pid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getImg() {
return img;
}
public void setImg(String img) {
this.img = img;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
此類的四個屬性分別是
- 商品的id
- 商品的名字
- 商品的價格
- 商品的圖片名
2.view視圖
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>Title</title>
</head>
<body>
<c:if test="${error != null}">
<c:forEach items="${error}" var="e">
<ul>
<font color="red">${e}</font>
</ul>
</c:forEach>
</c:if>
<form action="Detail" method="post">
pid:<input type="text" name="id"> <br>
name: <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>
該頁面是一個jsp頁面并且使用了jstl,所以我們必須要在pom.xml中添加對應(yīng)的依賴
jsp依賴:
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
<scope>provided</scope>
</dependency>
jstl依賴
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
product.jsp 用于展示商品的信息
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>商品詳情頁</title>
</head>
<body>
pid:${product.pid} <br>
name :${product.name} <br>
price:${product.price} <br>
img: ${product.img} <br>
</body>
</html>
3.controller 控制器
本實例通過對多個請求的控制來跳轉(zhuǎn)到不同的頁面。
首先定義一個負(fù)責(zé)解析請求地址來分發(fā)到不同控制器的Servlet
DispatcherServlet.java
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 ={"/Input","/Detail"} )
public class DispatcherServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String requestURI = request.getRequestURI();
String url = requestURI.substring(requestURI.lastIndexOf("/")+1);
System.out.println(url);
Controller controller = null;
if ("Input".equals(url)) {
controller = new ProductInputController();
} else if ("Detail".equals(url)) {
controller = new ProductDetailController();
}
String s = controller.handleRequest(request, response);
request.getRequestDispatcher(s).forward(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
}
我們可以得到用戶在地址欄里輸入的地址并且獲取到用戶想要跳轉(zhuǎn)到哪個頁面,由于本實例有兩個jsp頁面,所以我們可以通過接口來定義一個處理用戶輸入地址的請求代碼如下:
Controller.java
package com.qc.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public interface Controller {
String handleRequest(HttpServletRequest request, HttpServletResponse response);
}
ProductInputController.java
處理輸入商品信息請求的類
該類實現(xiàn)了Controller接口,重寫里面的handleRequest方法
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.jsp";
}
}
此類的作用就是返回一個index.jsp路徑到DispatcherServlet.java類中,并通過此類來轉(zhuǎn)發(fā)到index.jsp頁面
ProductDetailController.java
處理顯示商品信息請求的類
該類實現(xiàn)了Controller接口,重寫里面的handleRequest方法
import com.qc.bean.Product;
import com.qc.form.ProductForm;
import com.qc.validate.ProductValidate;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
public class ProductDetailController implements Controller {
@Override
public String handleRequest(HttpServletRequest request, HttpServletResponse response) {
//驗證傳過來的值是否合法
//首先獲取值
String name = request.getParameter("pname");
String p = request.getParameter("price");
String img = request.getParameter("img");
ProductForm form = new ProductForm();
double price = p == null? 0.0 :Double.parseDouble(p);
form.setName(name);
form.setPrice(price);
form.setImg(img);
ProductValidate pv = new ProductValidate();
List<String> error = pv.validate(form);
if (error.size() != 0) {
request.setAttribute("error", error);
return "WEB-INF/view/index.jsp";
}
String pid = request.getParameter("id");
Product product = new Product();
product.setPid(pid);
product.setName(name);
product.setPrice(price);
product.setImg(img);
request.setAttribute("product",product);
return "WEB-INF/view/product.jsp";
}
}
此類的目地是處理從頁面?zhèn)鬟^來的商品信息,當(dāng)傳過來的商品信息合規(guī)時就會返回展示商品信息的jsp頁面(product.jsp),如果不合規(guī)時就會跳轉(zhuǎn)到輸入商品信息的頁面(index.jsp)同時顯示不合理的信息內(nèi)容。
在處理之前我們需要創(chuàng)建一個ProductForm.java,主要?來做商品信息的校驗。
ProductForm.java
public class ProductForm {
private String name;
private double price;
private String img;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
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;
}
}
和JavaBean差不多,目地是把有可能出現(xiàn)不合理信息屬性的內(nèi)容放到一個java對象中
下面是處理不合理的商品信息的類
ProductValidate.java
import com.qc.form.ProductForm;
import java.util.ArrayList;
import java.util.List;
public class ProductValidate {
public List<String> validate(ProductForm pf) {
List<String> error = new ArrayList<>();
String name = pf.getName();
double price = pf.getPrice();
String img = pf.getImg();
if (name == null || name.length() == 0) {
error.add("product name must not be empty.");
}
if(img == null || img.length() == 0){
error.add("product image must not be empty.");
}
if(price < 0){
error.add("the price of product must be a positive number.");
}
return error;
}
}
此類通過定義一個List集合來存儲錯誤信息,這些錯誤信息通過判斷傳過來的商品信息內(nèi)容(商品名是否為null或者不存在,商品圖片名是否為null或者不存在,輸入的商品價格是否小于零)來存儲的,返回的是一個List集合到ProductDetailController.java類中,在該類中通過判斷返回的List集合里是否有值來跳轉(zhuǎn)到不同頁面。
- List集合有值:輸入的商品信息不合理,不能顯示在頁面中因此跳轉(zhuǎn)到index.jsp頁面并顯示錯誤信息
- List集合沒有值:說明輸入的商品信息正確,把正確的商品信息放到對象中并保存在request域然后跳轉(zhuǎn)到顯示商品信息的頁面product.jsp進(jìn)行展示。