学习目的是为了知道如何用Servlet实现 MVC,也是从原理上弄清楚SpringMVC。
一、工程目录
功能为用户输入 一个商品的信息,并提交,程序保存商品并展示已经保存的商品的信息。
所有的jsp文件都在WEB-INFO下,因此不能直接访问。
二、程序代码
- product
封闭了一个商品信息的javaBean .
package app02a.domain; import java.io.Serializable; public class Product implements Serializable{ private static final long serialVersionUID =1L; private String name ; private String description ; private float 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 float getPrice() { return price; } public void setPrice(float price) { this.price = price; } }
- ProducForm
注意,下面的price 为String 类型。
package app02a.from; /** * ProductForm与html中的from相对应,是后者在服务端的代表。 * @author lsj */ 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; } }
- 控制部分
package app02a.servlet; import java.io.IOException; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import app02a.domain.Product; import app02a.from.ProductForm; public class ControllerServlet extends HttpServlet{ @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { process(req, resp); } @Override protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { process(req, resp); } private void process(HttpServletRequest request, HttpServletResponse response){ String uri = request.getRequestURI() ; int lastIndex =uri.lastIndexOf("/"); String action = uri.substring(lastIndex+1) ; //execute an action if (action.equals("produc_input")){ //nothing to do }else if (action.equals("product_save")) { //create form ProductForm productForm = new ProductForm(); productForm.setName(request.getParameter("name")); productForm.setDescription(request.getParameter("description")); productForm.setPrice(request.getParameter("price")); //create model Product product = new Product() ; product.setName(productForm.getName()); product.setDescription(productForm.getDescription()); try { product.setPrice(Float.parseFloat(productForm.getPrice())); } catch (NumberFormatException e) { e.printStackTrace(); } //code to save product in DB //store model in a scope variable for the view //放入到了HttpServletRequest中,以便 对应的视图可以访问到 request.setAttribute("product", product); } //forward to a view String dispatchUrl = null; if (action.equals("product_input")){ dispatchUrl= "/WEB-INFO/jsp/ProductForm.jsp" ; }else if (action.equals("product_save")){ dispatchUrl= "/WEB-INFO/jsp/ProductDetails.jsp" ; } //RequestDispatcher: Defines an object that receives requests from the client and //sends them to any resource (such as a servlet, HTML file, or JSP file) on the server if (dispatchUrl!= null){ RequestDispatcher rd = request.getRequestDispatcher(dispatchUrl) ; try { rd.forward(request, response); } catch (ServletException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } }
时间: 2024-10-17 06:01:07