Servlet – MVC



Servlet – MVC

文章目录
1 项目结构图
2 ControllerServlet.java
3 SaveProductAction.java
4 ProductForm.java
5 Product.java
6 ProductForm.jsp
7 ProductDetails.jsp
1 项目结构图
2 ControllerServlet.java
package com.zxy97.spring.controller;

import com.zxy97.spring.form.ProductForm;
import com.zxy97.spring.model.Product;
import com.zxy97.spring.action.SaveProductAction;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import javax.servlet.RequestDispatcher;
import javax.servlet.annotation.WebServlet;

/**
* @description MVC
* @date 2018-10-02
* @author zhaoxuyang
*/
@WebServlet(name = “ControllerServlet”, urlPatterns = {“/input-product”, “/save-product”})
public class ControllerServlet extends HttpServlet {

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String uri = request.getRequestURI();
int lastIndex = uri.lastIndexOf(“/”);
String action = uri.substring(lastIndex + 1);
String dispatchUrl = null;
if (null != action) {
switch (action) {

case “input-product”:
dispatchUrl = “/WEB-INF/jsp/ProductForm.jsp”;
break;

case “save-product”:
ProductForm productForm = new ProductForm();
productForm.setName(request.getParameter(“name”));
productForm.setDescription(request.getParameter(“description”));
productForm.setPrice(request.getParameter(“price”));

Product product = new Product();
product.setName(productForm.getName());
product.setDescription(productForm.getDescription());
try {
product.setPrice(new BigDecimal(productForm.getPrice()));
} catch (Exception e) {
dispatchUrl = “/WEB-INF/jsp/ProductDetails.jsp”;
break;
}

SaveProductAction saveProductAction = new SaveProductAction();
saveProductAction.save(product);
request.setAttribute(“product”, product);
dispatchUrl = “/WEB-INF/jsp/ProductForm.jsp”;
break;

case “”:
dispatchUrl = “/WEB-INF/jsp/ProductForm.jsp”;
break;
}
}

if (dispatchUrl != null) {
RequestDispatcher rd = request.getRequestDispatcher(dispatchUrl);
rd.forward(request, response);
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

@Override
public String getServletInfo() {
return “Short description”;
}

}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
3 SaveProductAction.java
package com.zxy97.spring.action;

import com.zxy97.spring.model.Product;

public class SaveProductAction {
public void save(Product product){
System.out.println(“将product保存到数据库中:\n” + product.toString());//product保存到数据库中
}
}

1
2
3
4
5
6
7
8
9
10
1
2
3
4
5
6
7
8
9
10
4 ProductForm.java
package com.zxy97.spring.form;

/**
* @描述 与用户交互的模型
* @作用 存储用户输入,反馈信息给用户
*/
public class ProductForm {
private String name;
private String description;
private String price;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
5 Product.java
package com.zxy97.spring.model;

import java.io.Serializable;
import java.math.BigDecimal;

/**
* @描述 与系统交互的模型
*/
public class Product implements Serializable{
private String name;
private String description;
private BigDecimal price;

@Override
public String toString() {
return “Product{” + “name=” + name + “, description=” + description + “, price=” + price + ‘}’;
}
public String getName() {
return name;
}
public String getDescription() {
return description;
}
public BigDecimal getPrice() {
return price;
}
public void setName(String name) {
this.name = name;
}
public void setDescription(String description) {
this.description = description;
}
public void setPrice(BigDecimal price) {
this.price = price;
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
6 ProductForm.jsp
<%–
Document : ProductForm
Created on : 2018-10-2, 16:09:09
Author : zhaoxuyang
–%>

<%@page contentType=”text/html” pageEncoding=”UTF-8″%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″>
<title></title>
</head>
<body>
<form method=”post” action=”save-product”>
<h4>添加</h4>
<label>
<span>名称:</span>
<input type=”text” name=”name” placeholder=”填写名称” />
</label>
<label>
<span>描述:</span>
<input type=”text” name=”description” placeholder=”填写描述” />
</label>
<label>
<span>单价:</span>
<input type=”number” name=”price” placeholder=”填写单价” step=”any” />
</label>
<label>
<span>&nbsp;</span>
<input type=”submit” value=”提交” />
</label>
</form>
</body>
</html>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
7 ProductDetails.jsp
<%–
Document : ProductDetails
Created on : 2018-10-2, 16:08:48
Author : zhaoxuyang
–%>

<%@page contentType=”text/html” pageEncoding=”UTF-8″%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″>
<title>保存</title>
</head>
<body>
<div>
<h4>已保存</h4>
<p>
<h5>信息:</h5>
名称:${product.name}
描述:${product.description}
价格:${product.price}
</p>
</div>
</body>
</html>

———————
作者:zxy97
来源:CSDN
原文:https://blog.csdn.net/zhaoxuyang1997/article/details/82927176
版权声明:本文为博主原创文章,转载请附上博文链接!