java web之路 controller引數繫結從前端頁面獲得資料
jsp頁面,controller,requestmapping
controller中註解requestmapping表示接收這個請求,通過reture表示轉到那個jsp頁面上。
前端頁面將一組資料傳到controller可以通過定義一個<from action="${pageContext.request.contextPath }/items/editItemsSubmit.action">,這樣,頁面中的屬性將會被controller接收到。springmvc將url和controller方法對映
springmvc將jsp寫在WebRoot/WEB-INF下,不能直接訪問。
前端頁面:
在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的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";
}
}