SpringMVC学习笔记(八)——商品修改功能开发
2018-04-02 13:54
316 查看
一、操作流程
1、进入商品查询列表页面。2、点击修改,进入商品修改页面,页面中显示了要修改的商品(从数据库查询),要修改的商品从数据库查询,根据商品id(主键)查询商品信息。
3、在商品修改页面,修改商品信息,修改后,点击提交。
二、开发mapper
mapper:
根据id查询商品信息
根据id更新Items表的数据
不用开发了,使用逆向工程生成的代码。
三、开发service
service接口
package com.jiayifan.service; import com.jiayifan.ssm.po.ItemsQueryVo; import java.util.List; import com.jiayifan.ssm.po.*; /** * 商品管理的Service * @author 贾一帆 * */ public interface ItemsService { //商品的查询列表 public List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo) throws Exception; //根据id查询商品信息 public ItemsCustom findItemsById(Integer id) throws Exception; //修改商品信息 /** * * @param id 要修改商品的id * @param itemsCustom * @throws Exception */ public void updateItems(Integer id,ItemsCustom itemsCustom) throws Exception; }
service接口实现类
package com.jiayifan.ssm.service.impl; import java.util.List; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import com.jiayifan.service.ItemsService; import com.jiayifan.ssm.mapper.ItemsMapper; import com.jiayifan.ssm.mapper.ItemsMapperCustom; import com.jiayifan.ssm.po.Items; import com.jiayifan.ssm.po.ItemsCustom; import com.jiayifan.ssm.po.ItemsQueryVo; /** * 商品管理 * @author 贾一帆 * */ public class ItemsServiceImpl implements ItemsService { @Autowired private ItemsMapperCustom itemsMapperCustom; @Autowired private ItemsMapper itemsMapper; @Override public List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo) throws Exception { //通过ItemsMapperCustom查询数据库 return itemsMapperCustom.findItemsList(itemsQueryVo); } @Override public ItemsCustom findItemsById(Integer id) throws Exception { Items items = itemsMapper.selectByPrimaryKey(id); //中间对商品信息进行业务处理 //业务处理代码 //返回扩展类 ItemsCustom itemsCustom = new ItemsCustom(); //将items的内容拷贝到itemsCustom中 BeanUtils.copyProperties(items, itemsCustom); return itemsCustom; } @Override public void updateItems(Integer id, ItemsCustom itemsCustom) throws Exception { //添加业务校验,通常在service接口对关键参数进行校验 //校验id是否为空,如果为空我们就抛出异常 //更新商品信息,使用该方法可以根据id修改items表中的所有信息,包括大文本字段 //调用updateByPrimaryKeyWithBLOBs方法必须在参数对象中传入id itemsCustom.setId(id); itemsMapper.updateByPrimaryKeyWithBLOBs(itemsCustom); } }
四、开发controller
package com.jiayifan.ssm.controller; import java.util.ArrayList; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import com.jiayifan.service.ItemsService; import com.jiayifan.ssm.po.Items; import com.jiayifan.ssm.po.ItemsCustom; /** * 商品的Controller * @author 贾一帆 * */ @Controller public class ItemsController { //商品查询列表 //@RequestMapping("/queryItems")实现的是对queryItems方法和url进行映射,一个方法对应一个url //建议url和方法名一样 @Autowired private ItemsService itemsService; @RequestMapping("/queryItems") public ModelAndView queryItems() throws Exception { //调用service查找数据库,查询商品列表,这里先使用静态的数据模拟 List<ItemsCustom> itemsList = itemsService.findItemsList(null); //想List中填充静态数据 //返回ModelAndView ModelAndView modelAndView = new ModelAndView(); //相当于request的setAttribute方法 modelAndView.addObject("itemsList",itemsList); //指定视图 modelAndView.setViewName("items/itemsList"); return modelAndView; } //商品信息修改页面显示 @RequestMapping("/editItems") public ModelAndView editItems() throws Exception { //调用sercice查询商品信息 ItemsCustom itemsCustom = itemsService.findItemsById(1); //返回ModelAndView ModelAndView modelAndView = new ModelAndView(); //将商品信息放到Model modelAndView.addObject("itemsCustom",itemsCustom); //商品修改页面 modelAndView.setViewName("items/editItems"); return modelAndView; de39 } //商品信息修改提交 @RequestMapping("/editItemsSubmit") public ModelAndView editItemsSubmit() throws Exception { //调用service返回商品信息,页面需要将商品信息传到此方法 //此方法暂停 ModelAndView modelAndView = new ModelAndView(); //先返回一个成功页面 modelAndView.setViewName("success"); return modelAndView; } }
五、部署调试
六、@RequestMapping
用法
1、通过RequestMapping注解可以定义不同的处理器映射规则。
@RequestMapping(value="/item")或
@RequestMapping("/item")
value的值是数组,可以将多个
url映射到同一个方法
2、窄化请求映射
在
class上添加
@RequestMapping(url)指定通用请求前缀, 限制此类下的所有方法请求
url必须以请求前缀开头,通过此方法对
url进行分类管理。
//为了对我们的url进行分类管理,可以在这里定义一个根路径,最终访问url是根路径+子路径 //比如商品列表:/items/queryItems.action @RequestMapping("/items") public class ItemsController {
3、请求方法限定
(1)限定
GET方法
@RequestMapping(method = RequestMethod.GET)
如果通过
Post访问则报错:
HTTP Status 405 - Request method 'POST' not supported
例如:
@RequestMapping(value="/editItem",method=RequestMethod.GET)
(2)限定
POST方法
@RequestMapping(method = RequestMethod.POST)
如果通过Post访问则报错:
HTTP Status 405 - Request method 'GET' not supported
(3)
GET和
POST都可以
@RequestMapping(method={RequestMethod.GET,RequestMethod.POST})
七、controller
方法返回值
1、返回ModelAndView
controller方法中定义
ModelAndView对象并返回,对象中可添加
model数据、指定
view。
2、返回
void
在
controller方法形参上可以定义
request和
response,使用
request或
response指定响应结果:
(1)使用
request转向页面,如下:
request.getRequestDispatcher("页面路径").forward(request, response);
(2)也可以通过
response页面重定向:
response.sendRedirect("url")
(3)也可以通过
response指定响应结果,例如响应
json数据如下:
response.setCharacterEncoding("utf-8"); response.setContentType("application/json;charset=utf-8"); response.getWriter().write("json串");
3、返回字符串
(1)表示返回逻辑视图名。
真正视图(jsp路径)=前缀+逻辑视图名+后缀
@RequestMapping(value="/editItems",method={RequestMethod.POST,RequestMethod.GET}) public String editItems(Model model) throws Exception { //调用sercice查询商品信息 ItemsCustom itemsCustom = itemsService.findItemsById(1); //通过参数中的model将数据传到页面 model.addAttribute("itemsCustom",itemsCustom); return "items/editItems"; }
(2)
redirect重定向
商品修改提交后,重定向到商品查询列表。
redirect重定向特点:浏览器地址栏中的
url会变化。修改提交的
request数据无法传到重定向的地址。因为重定向后重新进行
request(
request无法共享)
@RequestMapping("/editItemsSubmit") public String editItemsSubmit() throws Exception { //重定向 return "redirect:queryItems.action"; }
(3)
forward页面转发
通过
forward进行页面转发,浏览器地址栏
url不变,
request可以共享。
@RequestMapping("/editItemsSubmit") public String editItemsSubmit() throws Exception { //重定向 return "forward:queryItems.action"; }
相关文章推荐
- springmvc学习笔记(10)-springmvc注解开发之商品修改功能
- springmvc整合mybatis之商品修改的功能开发
- springmvc学习笔记(10)-springmvc注解开发之商品修改功能
- springmvc注解开发-商品修改功能分析
- springmvc(六)商品修改功能开发
- 【SpringMVC整合MyBatis】商品修改功能分析
- springmvc学习笔记(10)-springmvc注解开发之商品改动功能
- ASP.NET动态网站开发培训-25.论文管理系统(五、添加后台新增及修改功能)
- 虚拟币交易平台源码修改 二次开发 btchanges 增加充值3级奖励分红功能
- 【SpringMVC注解开发】validation校验-商品修改校验 ---SpringMVC学习笔记(六)
- 第十一章:【UCHome二次开发】功能修改
- Android开发之TextView中间设置横线,适用于电商项目,商品原价之类的功能。
- C项目开发—通讯录(三) 删除、修改、搜索功能、源码发放
- Android开发实现仿京东商品搜索选项卡弹窗功能
- C++开发人脸性别识别教程(18)——辅助功能之文件名批量修改、方法验证
- Android开发之TextView中间设置横线,适用于电商项目,商品原价之类的功能。
- iOS开发的一些小技术:让UIImage有缩放功能、控制log的输出、xcode修改文件注释、随机数的使用、在UIImageView 中旋转图像、在Quartz中如何设置旋转点、创建.plist文件并存储
- ecshop用AJAX开发商品订单异步评价功能
- 【商城开发三】Android 仿淘宝商品详情页下拉足迹修改版
- MVC5 网站开发之八 栏目功能 添加、修改和删除