博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
我的Spring MVC第一个应用
阅读量:4701 次
发布时间:2019-06-09

本文共 4455 字,大约阅读时间需要 14 分钟。

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,包含视图,且没有模型,所有直接转发	 */	@Override	public ModelAndView handleRequest(HttpServletRequest req,HttpServletResponse resp) throws Exception {		logger.info("进入handleRequest成功");		return new ModelAndView("/WEB-INF/jsp/ProductForm.jsp");	}}

  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);		@Override	public 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("/WEB-INF/jsp/ProductDetails.jsp", "product",product);	}}

  ProductDetails.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%>
详情

产品已保存

详细列表:
名称: ${product.name}
简介: ${product.description}
价格: ¥${product.price}

  ProductForm.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%>
添加
添加:

  springmvc-servlet.xml

  web.xml

springmvc
org.springframework.web.servlet.DispatcherServlet
1
springmvc
*.action

  

转载于:https://www.cnblogs.com/ceet/p/6594515.html

你可能感兴趣的文章
Elasticsearch TermQuery 详解
查看>>
一个困扰了我N久的bug , android.enableAapt2=false 无效
查看>>
查看客户端的IP地址,机器名,MAC地址,登陆名等信息
查看>>
移动端经常遇到的小bug
查看>>
网络&热恋NSURLConnection代理及GET¥POST请求
查看>>
SshTerminal
查看>>
MySQL常用函数
查看>>
安装cocoapods
查看>>
Ubuntu安装搜狗拼音教程
查看>>
Happy Number
查看>>
Sqlserver 系统视图简单说明
查看>>
【摘录】PHP异步调用实现方式
查看>>
php缓存机制
查看>>
bzoj2049 线段树 + 可撤销并查集
查看>>
sql语句---存在即更新,否则insert
查看>>
cookie机制、session机制
查看>>
BZOJ 3787: Gty的文艺妹子序列
查看>>
Comet OJ - Contest #5 简要题解
查看>>
CF1093G Multidimensional Queries
查看>>
移动端提升页面速度与网站性能
查看>>