基于Servlet Controller的Model2
示例
这个应用程序可以用来输入产品信息;在如图的表单中输入信息:
http://localhost:8080/base-webapp/product_input
并提交,然后,应用程序给用户发送一个确认页面,并显示所保存产品的详细信息。
http://localhost:8080/base-webapp/product_save
这个应用程序能够执行下面这两个action:
- 显示Add Product表单,URI中包含字符串“product_input”
- 保存产品,并返回确认页面,URI中包含字符串“product_save”
这个应用包含一下组件:
- Product类时模型对象,包含产品信息
- ProductForm类,封装用于输入产品的HTML表单域
- ControllerServlet类,是这个Model2应用的Controller
- Action类,用于保存产品信息
- View的两个JSP页面:ProductForm.jsp和ProductDetails.jsp
源码:
product bean 类:
package com.gof.test.bean;
import java.io.Serializable;
public class Product implements Serializable {
/**
*
*/
private static final long serialVersionUID = -2088880133830936693L;
private String nameString;
private String description;
private float price;
public String getName(){
return nameString;
}
public void setName(String name){
this.nameString = name;
}
public String getDescription(){
return description;
}
public void setDescription(String description){
this.description = description;
}
public float getPrice(){
return price;
}
public void setPrice(float price){
this.price = price;
}
}
product form 类:
package com.gof.test.bean;
public class ProductForm {
private String nameString;
private String description;
private String price;
public String getName(){
return nameString;
}
public void setName(String name){
this.nameString = 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;
}
}
ControllerServlet类:
package com.gof.test.servlet;
import java.io.IOException;
import java.net.URL;
import javax.print.DocFlavor.STRING;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.gof.test.action.SaveProductAction;
import com.gof.test.bean.Product;
import com.gof.test.bean.ProductForm;
public class ControllerServlet extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException{
process(req, resp);
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException{
process(req, resp);
}
private void process(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException{
String uri = req.getRequestURI();
int lastIndex = uri.lastIndexOf("/");
String action = uri.substring(lastIndex + 1);
String dispatchUrlString = null;
if (action.equals("product_input")){
dispatchUrlString = "/jsp/controller/ProductForm.jsp";
}else
if (action.equals("product_save")){
ProductForm productForm = new ProductForm();
productForm.setName(req.getParameter("name"));
productForm.setDescription(req.getParameter("description"));
productForm.setPrice(req.getParameter("price"));
Product product = new Product();
product.setName(productForm.getName());
product.setDescription(productForm.getDescription());
try{
product.setPrice(Float.parseFloat(productForm.getPrice()));
}catch (NumberFormatException e){
e.printStackTrace();
}
SaveProductAction saveProductAction = new SaveProductAction();
saveProductAction.save(product);
req.setAttribute("product", product);
dispatchUrlString = "/jsp/controller/ProductDetails.jsp";
}
if (dispatchUrlString != null){
RequestDispatcher rDispatcher = req.getRequestDispatcher(dispatchUrlString);
rDispatcher.forward(req, resp);
}
}
}
在web.xml中注册这个servlet:
<!-- Servlet Controller -->
<servlet>
<servlet-name>controller</servlet-name>
<servlet-class>com.gof.test.servlet.ControllerServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>controller</servlet-name>
<url-pattern>/product_input</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>controller</servlet-name>
<url-pattern>/product_save</url-pattern>
</servlet-mapping>
save action类:
package com.gof.test.action;
import com.gof.test.bean.Product;
public class SaveProductAction {
public void save(Product product){
// insert product into the database
}
}
View - ProductForm.jsp :
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Add Product Form</title>
</head>
<body>
<div>
<h3>Add a product</h3>
<form method="post" action="product_save">
<table>
<tr>
<td>Product Name:</td>
<td><input type="text" name="name"/></td>
</tr>
<tr>
<td>Description:</td>
<td><input type="text" name="description"/></td>
</tr>
<tr>
<td>Price:</td>
<td><input type="text" name="price"/></td>
</tr>
<tr>
<td><input type="reset" /></td>
<td><input type="submit" value="Add Product"/></td>
</tr>
</table>
</form>
</div>
</body>
</html>
View - ProductDeails.jsp:
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Save Product</title>
</head>
<body>
<div>
<h4>The product has been saved.</h4>
<p>
<h5>Details:</h5>
Product Name: ${product.name}<br/>
Description: ${product.description}<br/>
Price:${product.price}<br/>
</p>
</div>
</body>
</html>