java web之路 controller参数绑定从前端页面获得数据
目录
jsp页面,controller,requestmapping
@RequestMapping("/queryitems")
限制请求的方法method={RequestMethod.POST,RequestMethod.GET}
jsp页面,controller,requestmapping
springmvc将jsp写在WebRoot/WEB-INF下,不能直接访问。需要通过控制器来决定那个页面可以被访问,所以能看到那个页面是由contrller来决定。
产生一个请求到contrller,contrller跟据请求的参数决定返回视图。(controller中注解requestmapping表示接收这个请求,通过return表示转到那个jsp页面上。)
前端页面将一组数据传到controller可以通过定义一个<from action="${pageContext.request.contextPath }/items/editItemsSubmit.action">,这样,页面中的属性将会被controller接收到。springmvc将url和controller方法映射
前端页面
在springmvc中,前端页面中属性的name要与pojo中的属性值同名,这样在controller进行参数绑定时才可以自动成功绑定到。
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>查询商品列表</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/items/queryitems.action" method="post">
查询条件:
<table width="100%" border=1>
<tr>
<td><input type="submit" value="查询" /></td>
</tr>
</table>
商品列表:
<table width="100%" border=1>
<tr>
<td>商品名称</td>
<td>商品价格</td>
<td>生产日期</td>
<td>商品描述</td>
<td>操作</td>
</tr>
<c:forEach items="${itemslist }" var="item">
<tr>
<td>${item.name }</td>
<td>${item.price }</td>
<td><fmt:formatDate value="${item.createtime}"
pattern="yyyy-MM-dd HH:mm:ss" /></td>
<td>${item.detail }</td>
<td><a
href="${pageContext.request.contextPath }/items/editItems.action?id=${item.id}">修改</a></td>
</tr>
</c:forEach>
</table>
</form>
</body>
</html>
action="${pageContext.request.contextPath }/items/queryitems.action,
controller
controller的queryitems的写法:
@Autowired
ItemsService itemsService;
@RequestMapping("/queryitems")
public ModelAndView queryItems() throws Exception {
List<ItemsCustom> itemslist = itemsService.findItemList(null);
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("itemslist", itemslist);
modelAndView.setViewName("items/itemslist");
return modelAndView;
}
@RequestMapping("/queryitems")
注解表示接收queryitems这个请求并处理
controller除了可以返回ModelAndView还可以返回String,如果返回的是String,则返回的是url。
如果不使用ModelAndView返回,那么将通过形参绑定的方式返回数据
@RequestMapping("/queryitems")
public String queryItems(Model model) throws Exception {
List<ItemsCustom> itemslist = itemsService.findItemList(null);
// ModelAndView modelAndView = new ModelAndView();
// modelAndView.addObject("itemslist", itemslist);
// modelAndView.setViewName("items/itemslist");
model.addAttribute(itemslist);
return "items/itemslist";
}
限制请求的方法method={RequestMethod.POST,RequestMethod.GET}
@RequestMapping(value="/editItems",method= {RequestMethod.GET,RequestMethod.POST})
public String editItems(HttpServletRequest request, @RequestParam(value="id") Integer item_id,Model model) throws Exception{
//获得商品信息
ItemsCustom itemsCustom = itemsService.findItemsById(item_id);
// ModelAndView modeAndView = new ModelAndView();
//
// modeAndView.addObject("itemsCustom",itemsCustom);
// modeAndView.setViewName("items/editItems");
model.addAttribute(itemsCustom);
return "items/editItems";
}
参数自动绑定,前端页面的name要与对象属性名一致,才能自动绑定。
如果在controller中想使用不一样的名字,可以使用
@RequestParam(value="id") Integer item_id
@RequestParam(value="id") 表示前端传过来的名称为id,在controller中使用的名称为item_id
@RequestMapping(value="editItemsSubmit",method=RequestMethod.POST)
public String editItemsSubmit(HttpServletRequest request,HttpServletResponse response ,String createtime, Integer id, ItemsCustom itemsCustom) throws Exception {
// SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//
// itemsCustom.setCreatetime(sdf.parse(createtime));
itemsService.updateItems(id, itemsCustom);
return "success";
}
package cn.itcast.ssm.controller;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.support.HttpRequestHandlerServlet;
import org.springframework.web.servlet.ModelAndView;
import cn.itcast.ssm.po.ItemsCustom;
import cn.itcast.ssm.po.ItemsQueryVo;
import cn.itcast.ssm.service.ItemsService;
@Controller
//为了对url进行分类管理 ,可以在这里定义根路径,最终访问url是根路径+子路径
//比如:商品列表:/items/queryItems.action
@RequestMapping("/items")
public class ItemsController {
@Autowired
ItemsService itemsService;
@RequestMapping("/queryitems")
public ModelAndView queryItems() throws Exception {
List<ItemsCustom> itemslist = itemsService.findItemList(null);
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("itemslist", itemslist);
modelAndView.setViewName("items/itemslist");
return modelAndView;
}
// //根据id显示商品信息
// @RequestMapping(value="/editItems",method= {RequestMethod.GET,RequestMethod.POST})
// public ModelAndView editItems(HttpServletRequest request, Integer id) throws Exception{
//
// //获得商品信息
// ItemsCustom itemsCustom = itemsService.findItemsById(id);
//
// ModelAndView modeAndView = new ModelAndView();
//
// modeAndView.addObject("itemsCustom",itemsCustom);
// modeAndView.setViewName("items/editItems");
//
// return modeAndView;
//
// }
//根据id显示商品信息
@RequestMapping(value="/editItems",method= {RequestMethod.GET,RequestMethod.POST})
public String editItems(HttpServletRequest request, @RequestParam(value="id") Integer item_id,Model model) throws Exception{
//获得商品信息
ItemsCustom itemsCustom = itemsService.findItemsById(item_id);
// ModelAndView modeAndView = new ModelAndView();
//
// modeAndView.addObject("itemsCustom",itemsCustom);
// modeAndView.setViewName("items/editItems");
model.addAttribute(itemsCustom);
return "items/editItems";
}
// 提交修改商品信息
@RequestMapping(value="editItemsSubmit",method=RequestMethod.POST)
public String editItemsSubmit(HttpServletRequest request,HttpServletResponse response ,String createtime, Integer id, ItemsCustom itemsCustom) throws Exception {
// SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//
// itemsCustom.setCreatetime(sdf.parse(createtime));
itemsService.updateItems(id, itemsCustom);
return "success";
}
}
包装类型的pojo参数类型绑定
有时候简单类开的参数绑定并不能满足要求,需要绑定pojo类型。比如我们在中传入的是包装类型,
contrlloer:
@RequestMapping("/queryitems")
public ModelAndView queryItems(ItemsQueryVo itemsQueryVo) throws Exception {
List<ItemsCustom> itemslist = itemsService.findItemList(itemsQueryVo);
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("itemslist", itemslist);
modelAndView.setViewName("items/itemslist");
return modelAndView;
}
public interface ItemsService {
public List<ItemsCustom> findItemList(ItemsQueryVo itemsQueryVo) throws Exception;
//根据id显示商品信息
public ItemsCustom findItemsById(Integer id) throws Exception;
//根据id修改商品信息,使用integer类型可以判断是id是否为空
public void updateItems(Integer id, ItemsCustom itemsCustom) throws Exception;
}
因为前端页面的类型名称要跟contrlloer里形参的名称保持一致,才能自动绑定,所以前端页面应该使用ItemsQueryVo类的属性
public class ItemsQueryVo {
//商品信息
private Items items;
//为了系统 可扩展性,对原始生成的po进行扩展
private ItemsCustom itemsCustom;
public Items getItems() {
return items;
}
public void setItems(Items items) {
this.items = items;
}
public ItemsCustom getItemsCustom() {
return itemsCustom;
}
public void setItemsCustom(ItemsCustom itemsCustom) {
this.itemsCustom = itemsCustom;
}
}
<form
action="${pageContext.request.contextPath }/items/queryitems.action"
method="post">
查询条件:
<table width="100%" border=1>
<tr>
<td><input type="text" name="itemsCustom.name"></td>
<td><input type="submit" value="查询" /></td>
</tr>
</table>
<td><input type="text" name="itemsCustom.name"></td>完成包装类型的参数绑定
数组类型的参数绑定
例如在提量删除的时候,需要传入多个参数,此时会使用到数组类型的参数绑定方法。即便使用这种类型的绑定方法,也是遵守前端页面属性名称(name)与pojo类型的名称相同,才会自动绑定。
contrlloer方法
//批量删除
@RequestMapping("/deleteItems")
public String delItemsSubmit(Integer[] items_id) throws Exception {
// 调用删除server
System.out.println("deleteItems");
return "success";
}
前端页面,重要的是这里的name,name要和contrlloer里的形参保持一致
<td><input type="checkbox" name="items_id" value="${item.id}"></input></td>
前端页面全局:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>查询商品列表</title>
<script type="text/javascript">
function deleteItems() {
document.itemsForm.action = "${pageContext.request.contextPath }/items/deleteItems.action";
document.itemsForm.submit();
}
function queryitems() {
document.itemsForm.action = "${pageContext.request.contextPath }/items/queryitems.action";
document.itemsForm.submit();
}
</script>
</head>
<body>
<form name="itemsForm" method="post">
查询条件:
<table width="100%" border=1>
<tr>
<td>商品查询<input type="text" name="itemsCustom.name"><input
type="button" value="查询" onClick="queryitems()" /></td>
<td><input type="button" value="批量删除" onClick="deleteItems()"></td>
</tr>
</table>
商品列表:
<table width="100%" border=1>
<tr>
<td>选择</td>
<td>商品名称</td>
<td>商品价格</td>
<td>生产日期</td>
<td>商品描述</td>
<td>操作</td>
</tr>
<c:forEach items="${itemslist}" var="item">
<tr>
<td><input type="checkbox" name="items_id" value="${item.id}"></input></td>
<td>${item.name }</td>
<td>${item.price }</td>
<td><fmt:formatDate value="${item.createtime}"
pattern="yyyy-MM-dd HH:mm:ss" /></td>
<td>${item.detail }</td>
<td><a
href="${pageContext.request.contextPath }/items/editItems.action?id=${item.id}">修改</a></td>
</tr>
</c:forEach>
</table>
</form>
</body>
</html>
。。。。。