场景描述:
用户提交产品名称和价格到Spring MVC Spring MVC接受数据并显示
1.实体类Product.java
1 package pojo; 2 3 public class Product { 4 private int id; 5 private String name; 6 private float price; 7 8 public int getId() { 9 return id; 10 } 11 12 public void setId(int id) { 13 this.id = id; 14 } 15 16 public String getName() { 17 return name; 18 } 19 20 public void setName(String name) { 21 this.name = name; 22 } 23 24 public float getPrice() { 25 return price; 26 } 27 28 public void setPrice(float price) { 29 this.price = price; 30 } 31 }
2.增加商品的页面addProduct.jsp
在web目录下 (不是在WEB-INF下)增加商品的页面addProduct.jsp
1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 8 <html> 9 <head> 10 <base href="<%=basePath%>"> 11 12 <title>My JSP 'addProduct.jsp' starting page</title> 13 14 <meta http-equiv="pragma" content="no-cache"> 15 <meta http-equiv="cache-control" content="no-cache"> 16 <meta http-equiv="expires" content="0"> 17 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 18 <meta http-equiv="description" content="This is my page"> 19 <!-- 20 <link rel="stylesheet" type="text/css" href="styles.css"> 21 --> 22 23 </head> 24 25 <body> 26 <h1 align="center">增加商品</h1> 27 <form action="addProduct"> 28 产品名称:<input type="text" name="name" vlaue=""><br> 29 产品价格:<input type="text" name="price" vlaue=""><br> 30 <input type="submit" value="增加商品"> 31 </form> 32 </body> 33 </html>
3.控制器类ProductController
控制器ProductController,准备一个add方法映射/addProduct路径
为add方法准备一个Product 参数,用于接收注入
最后跳转到showProduct页面显示用户提交的数据
注: addProduct.jsp 提交的name和price会自动注入到参数 product里
注: 参数product会默认被当做值加入到ModelAndView 中,相当于:
mav.addObject("product",product);
1 package pojo; 2 3 import org.springframework.stereotype.Controller; 4 import org.springframework.web.bind.annotation.RequestMapping; 5 import org.springframework.web.servlet.ModelAndView; 6 7 @Controller 8 public class ProductController { 9 10 @RequestMapping("/addProduct") 11 public ModelAndView add(Product product) throws Exception { 12 ModelAndView mav = new ModelAndView("showProduct"); 13 return mav; 14 } 15 16 }
4.显示商品的界面showProduct.jsp
在WEB-INF/page 目录下创建 showProduct.jsp
用 EL 表达式显示用户提交的名称和价格
1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 8 <html> 9 <head> 10 <base href="<%=basePath%>"> 11 12 <title>My JSP 'showProduct.jsp' starting page</title> 13 14 <meta http-equiv="pragma" content="no-cache"> 15 <meta http-equiv="cache-control" content="no-cache"> 16 <meta http-equiv="expires" content="0"> 17 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 18 <meta http-equiv="description" content="This is my page"> 19 <!-- 20 <link rel="stylesheet" type="text/css" href="styles.css"> 21 --> 22 23 </head> 24 25 <body> 26 <h1 align="center">商品信息</h1> 27 产品名称:${product.name}<br> 28 产品价格:${product.price} 29 </body> 30 </html>
5.测试
目录结构: