我的Spring MVC第一個應用 (最終版),springmvc
項目結構圖:
代碼如下:
Product
package com.mstf.bean;import java.io.Serializable;/** * Product類,封裝了一些資訊,包含三個屬性 * @author wangzheng * */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;}}
ProductForm
package com.mstf.bean.form;/** * ProductForm是表單類 * 作用:當資料校正失敗時,用於儲存和展示使用者在原始表單的輸入 * @author wangzheng * */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;}}
InputProductController
package com.mstf.controller;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.springframework.web.servlet.ModelAndView;import org.springframework.web.servlet.mvc.Controller;public class InputProductController implements Controller {private static final Log logger=LogFactory.getLog(InputProductController.class);/** * 返回一個ModelAndView,包含視圖,且沒有模型,所有直接轉寄 */@Overridepublic ModelAndView handleRequest(HttpServletRequest req,HttpServletResponse resp) throws Exception {logger.info("進入handleRequest成功");return new ModelAndView("ProductForm");}}
SaveProductController
package com.mstf.controller;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.springframework.web.servlet.ModelAndView;import org.springframework.web.servlet.mvc.Controller;import com.mstf.bean.Product;import com.mstf.bean.form.ProductForm;public class SaveProductController implements Controller {private static final Log logger=LogFactory.getLog(InputProductController.class);@Overridepublic ModelAndView handleRequest(HttpServletRequest req,HttpServletResponse resp) throws Exception {req.setCharacterEncoding("UTF-8"); //轉碼 resp.setCharacterEncoding("UTF-8");logger.info("進入SaveProductController成功");// 構建一個ProductForm表單對象 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 (Exception e) { e.printStackTrace(); } return new ModelAndView("ProductDetails", "product",product);}}
springmvc-config.xml
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean name="/product_input.action" class="com.mstf.controller.InputProductController"/> <bean name="/product_save.action" class="com.mstf.controller.SaveProductController"/> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean></beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <init-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/config/springmvc-config.xml</param-value></init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping></web-app>
ProductDetails.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>詳情</title><style type="text/css">@IMPORT url("css/main.css");</style></head><body><div id="global"> <h4>產品已儲存</h4> <p> <h5>詳細列表:</h5> 名稱: ${product.name}<br> 簡介: ${product.description}<br> 價格: ¥${product.price} </p></div></body></html>
ProductForm.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>添加</title><style type="text/css">@IMPORT url("css/main.css");</style></head><body><div id="global"><form action="product_save.action" method="post"><fieldset><legend>添加:</legend><p> <label for="name">名稱: </label><input type="text" id="name" name="name" tabindex="1"> </p> <p> <label for="description">簡介: </label><input type="text" id="description" name="description" tabindex="2"></p> <p> <label for="price">價格: </label><input type="text" id="price" name="price" tabindex="3"></p><p id="buttons"><input id="reset" type="reset" tabindex="4"><input id="submit" type="submit" tabindex="5" value="添加"></p></fieldset></form></div></body></html>