java 页面绑定数据_java web之路 controller参数绑定从前端页面获得数据
目录
jsp页面,controller,requestmapping
前端页面
controller
@RequestMapping("/queryitems")
限制请求的方法method={RequestMethod.POST,RequestMethod.GET}
包装类型的pojo参数类型绑定
数组类型的参数绑定
jsp页面,controller,requestmapping
springmvc将jsp写在WebRoot/WEB-INF下,不能直接访问。需要通过控制器来决定那个页面可以被访问,所以能看到那个页面是由contrller来决定。
产生一个请求到contrller,contrller跟据请求的参数决定返回视图。(controller中注解requestmapping表示接收这个请求,通过return表示转到那个jsp页面上。)
前端页面将一组数据传到controller可以通过定义一个,这样,页面中的属性将会被controller接收到。springmvc将url和controller方法映射
前端页面
在springmvc中,前端页面中属性的name要与pojo中的属性值同名,这样在controller进行参数绑定时才可以自动成功绑定到。
查询商品列表
查询条件:
商品列表:
商品名称
商品价格
生产日期
商品描述
操作
${item.name }
${item.price }
${item.detail }
action="${pageContext.request.contextPath }/items/queryitems.action,
controller
controller的queryitems的写法:
@Autowired
ItemsService itemsService;
@RequestMapping("/queryitems")
public ModelAndView queryItems() throws Exception {
Listitemslist = 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 {
Listitemslist = 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 {
Listitemslist = 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 {
Listitemslist = itemsService.findItemList(itemsQueryVo);
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("itemslist", itemslist);
modelAndView.setViewName("items/itemslist");
return modelAndView;
}
public interface ItemsService {
public ListfindItemList(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;
}
}
查询条件:
完成包装类型的参数绑定
数组类型的参数绑定
例如在提量删除的时候,需要传入多个参数,此时会使用到数组类型的参数绑定方法。即便使用这种类型的绑定方法,也是遵守前端页面属性名称(name)与pojo类型的名称相同,才会自动绑定。
contrlloer方法
//批量删除
@RequestMapping("/deleteItems")
public String delItemsSubmit(Integer[] items_id) throws Exception {
//调用删除server
System.out.println("deleteItems");
return "success";
}
前端页面,重要的是这里的name,name要和contrlloer里的形参保持一致
前端页面全局:
查询商品列表
查询条件:
商品查询
商品列表:
选择
商品名称
商品价格
生产日期
商品描述
操作
${item.name }
${item.price }
${item.detail }
。。。。。