需求
操作流程:
- 进入商品查询列表页面(加载所有商品)
- 点击修改链接,进入商品修改页面,页面中加载显示的是要修改的商品信息(数据库查询得到),根据商品的ID查询商品信息
- 在商品的修改页面修改商品信息,修改后点击提交至数据库
开发mapper
mapper:
根据ID查询商品表信息
根据ID更新商品表信息
不需要开发,直接使用逆向工程生成的mapper代码
开发service
接口:
根据ID查询商品信息
修改商品信息
接口类:
/**
* 根据ID查询商品信息
* @param id 查询商品的ID
* @return 商品的扩展po类
* @throws Exception
*/
public ItemsCustom findItemsById(int id) throws Exception;
/**
* 修改商品信息
* @param id 修改商品的ID
* @param itemsCustom 修改商品的数据
* @throws Exception
*/
public void updateItems(int id,ItemsCustom itemsCustom) throws Exception;
实现类:
@Override
public ItemsCustom findItemsById(Integer id) throws Exception {
Items items = itemsMapper.selectByPrimaryKey(id);
//对于商品信息进行业务处理
//....
//最终返回ItemsCustom
ItemsCustom itemsCustom = new ItemsCustom();
//将items的内容拷贝到itemsCustom
BeanUtils.copyProperties(items,itemsCustom);
return itemsCustom;
}
@Override
public void updateItems(Integer id, ItemsCustom itemsCustom) throws Exception {
//添加业务校验,通常在service接口中对关键的参数进行校验
//验证ID是否为空,若为空则抛出异常
//更新商品信息,使用updateByPrimaryKeyWithBLOBs根据ID更新items表中的所有字段,包括text类型的字段
//使用updateByPrimaryKeyWithBLOBs根据ID更新items表中的所有字段要求必须传入ID,所以我们再次将Id set进去
itemsCustom.setId(id);
itemsMapper.updateByPrimaryKeyWithBLOBs(itemsCustom);
}
开发controller
方法:
商品信息修改页面的加载方法
商品页面修改信息的提交方法
@RequestMapping
特性:
1.
url映射
定义Controller方法所对应的url,进行处理器映射使用。
1.窄化请求映射
@Controller
//对url进行分类管理,可以在此处定义根路径,最终访问的url是 根路径+子路径 比如商品列表 /items/queryItems.action
@RequestMapping("/items")
public class itemsController {
2.限制http的请求方法
处于安全性考虑,限制请求为post方法
//商品修改加载原始信息
//@RequestMapping("/editItems")
//限制http请求方法为POST
@RequestMapping(value = "/editItems",method = {RequestMethod.POST})
再进行get请求,则会报错405:
controller方法的返回值类型
1、返回ModelAndView
需要在方法结束时定义ModelAndView,将model和view分别进行设置
如:
// 商品修改加载原始信息
// 限制http请求方法为POST和GET
@RequestMapping(value = "/editItems",method = {RequestMethod.POST,RequestMethod.GET})
public ModelAndView editItems() throws Exception{
//调用service 根据商品ID查询商品信息
ItemsCustom itemsCustom = itemsService.findItemsById(5);
//返回ModelAndView
ModelAndView modelAndView = new ModelAndView();
//将商品信息放到model中
modelAndView.addObject("itemsCustom",itemsCustom);
//商品修改页面
modelAndView.setViewName("items/editItems");
return modelAndView;
}
2、返回String
(1) 若Controller的方法返回的是String,表示返回逻辑视图名
真正的视图(JSP的路径) = 前缀+逻辑视图名+后缀
如:
@RequestMapping(value = "/editItems",method = {RequestMethod.POST,RequestMethod.GET})
public String editItems(Model model) throws Exception{
//调用service 根据商品ID查询商品信息
ItemsCustom itemsCustom = itemsService.findItemsById(5);
// //返回ModelAndView
// ModelAndView modelAndView = new ModelAndView();
// //将商品信息放到model中
// modelAndView.addObject("itemsCustom",itemsCustom);
// //商品修改页面
// modelAndView.setViewName("items/editItems");
//通过形参中的model将model的数据传到页面
//相当于modelandview的addObject
model.addAttribute("itemsCustom",itemsCustom);
return "items/editItems";
(2)redirect 重定向
商品修改提交后,重定向到商品查询列表。
redirect重定向特点:浏览器地址栏URl会变化,修改提交的request数据无法传到重定向的地址。因为重定向后会重新进行request(requset无法共享)
如:
//商品修改
@RequestMapping("/editItemsSubmit")
public String editItemsSubmit() throws Exception{
//调用service更新商品信息,页面需要将商品信息传到此方法
//重定向至商品查询列表页面
return "redirect:queryItems.action";
}
(3)forward 页面转发
forward页面转发特点:url地址栏不变,request可以共享
//商品修改
@RequestMapping("/editItemsSubmit")
public String editItemsSubmit() throws Exception{
//调用service更新商品信息,页面需要将商品信息传到此方法
//页面转发
return "forward:queryItems.action";
}
3、返回void
(2)redirect 重定向