SpringBoot中新增和修改表单如何传参实践 - liviamarre/myRoad GitHub Wiki
我要添加一个Product对象,先把一个新的Product实例传到前台:
@RequestMapping("/new")
public String newArea(ModelMap map) {
map.addAttribute("product", new Product());
return "new_product";
}
前端页面这样写:
<body>
<div><h2>添加新产品</h2><a href="/">返回首页</a></div>
<div><a href="/product/list">返回产品列表</a></div>
<br/>
<form name="product" action="/product/add">
<div><span>名称:</span> <input type="text" name="name"/></div>
<div><span>规格:</span> <input type="text" name="specification"/></div>
<div><span>产地:</span> <input type="text" name="productingArea"/></div>
<div><span>品牌:</span> <input type="text" name="brand"/></div>
<div><span>描述:</span> <textarea cols="50" rows="6" name="description" style="margin:3px 0 3px 0"></textarea></div>
<div><span>默认价格:</span><input type="text" name="defaultPrice" onkeypress="return event.keyCode>=48&&event.keyCode<=57" ng-pattern="/[^a-zA-Z]/"/></div>
<br/>
<div><span><input type="submit" value="提交" /> <input type="reset" value="重置" /></span></div>
</form>
提交的代码:
@RequestMapping("/add")
public String addProduct(@ModelAttribute("form") Product product, ModelMap map) {
// 调用jdbcTemplate对象中的方法实现操作
String sql = "insert into t_product(name,specification,producing_area,brand,default_price,description) values(?,?,?,?,?,?)";
jdbcTemplate.update(sql, product.getName(), product.getSpecification(), product.getProductingArea(), product.getBrand(), product.getDefaultPrice(), product.getDescription());
return "redirect:/product/list";
}
从后台获取当前对象值:
@RequestMapping("/update")
public String update(@RequestParam("productId") Integer productId, ModelMap map) throws SQLException {
Product product = this.queryProduct(productId);
map.addAttribute("product", product);
return "update_product";
}
前台显示:
<body>
<div><h2>修改产品</h2></div>
<div><a href="/">返回首页</a><a style="margin-left:20px" href="/product/list">返回产品列表</a></div>
<br/>
<form name="product" action="/product/updateSubmit?productId=${product.id}" method="post">
<input type="hidden" name="id" value="${product.id}"/>
<div><span>名称:</span> <input type="text" name="name" value="${product.name}"/></div>
<div><span>规格:</span> <input type="text" name="specification" value="${product.specification}"/></div>
<div><span>产地:</span> <input type="text" name="productingArea" value="${product.productingArea}"/></div>
<div><span>品牌:</span> <input type="text" name="brand" value="${product.brand}"/></div>
<div><span>描述:</span> <textarea cols="50" rows="6" name="description" style="margin:3px 0 3px 0">${product.description}</textarea></div>
<div><span>默认价格:</span><input type="text" name="defaultPrice" value="${product.defaultPriceStr}" onkeypress="return event.keyCode>=48&&event.keyCode<=57" ng-pattern="/[^a-zA-Z]/" /></div>
<br/>
<div><span><input type="submit" value="提交" style="width:100px;height:32px;" /></span></div>
</form>
</body>
修改完以后提交:
@RequestMapping(value= "/updateSubmit", method=RequestMethod.POST)
public String updateProduct(@ModelAttribute Product form, @RequestParam("productId") Integer productId,ModelMap map) {
// 调用jdbcTemplate对象中的方法实现操作
String sql = "update t_product set name=?,specification=?,producing_area=?,brand=?,default_price=?,description=? where id ="
+ productId;
jdbcTemplate.update(sql, form.getName(), form.getSpecification(), form.getProductingArea(),
form.getBrand(), form.getDefaultPrice(), form.getDescription());
return "redirect:/product/list";
}
@RequestMapping("/update")
public String update(@RequestParam("productId") Integer productId, ModelMap map) throws SQLException {
Product product = this.queryProduct(productId);
map.addAttribute("product", product);
return "update_product";
}
前台请求路径:
http://localhost:8000/product/productId = 1