springmvc学习笔记(10)-springmvc注解开发之商品改动功能

springmvc学习笔记(10)-springmvc注解开发之商品改动功能

标签: springmvc


  • springmvc学习笔记10-springmvc注解开发之商品改动功能

    • 需求
    • 开发mapper
    • 开发service
    • 开发controller
    • RequestMapping
    • controller方法的返回值

本文以商品改动为例,记录springmvc的注解开发。包含mapper,service,controller,@RequestMapping,controller方法的返回值等

需求

操作流程:

  • 1.进入商品查询列表页面
  • 2.点击改动,进入商品改动页面,页面中显示了要改动的商品。要改动的商品从数据库查询,依据商品id(主键)查询商品信息
  • 3.在商品改动页面,改动商品信息,改动后,点击提交

开发mapper

mapper:

  • 依据id查询商品信息
  • 依据id更新Items表的数据

不用开发了,使用逆向project生成的代码。

开发service

com.iot.learnssm.firstssm.service.ItemsService中加入两个接口

  //依据id查询商品信息/**** <p>Title: findItemsById</p>* <p>Description: </p>* @param id 查询商品的id* @return* @throws Exception*/ItemsCustom findItemsById(Integer id) throws Exception;//改动商品信息/**** <p>Title: updateItems</p>* <p>Description: </p>* @param id 改动商品的id* @param itemsCustom 改动的商品信息* @throws Exception*/void updateItems(Integer id,ItemsCustom itemsCustom) throws Exception;

com.iot.learnssm.firstssm.service.impl.ItemsServiceImpl中实现接口,添加itemsMapper属性

@Autowired
private ItemsMapper itemsMapper;public ItemsCustom findItemsById(Integer id) throws Exception {Items items = itemsMapper.selectByPrimaryKey(id);//中间对商品信息进行业务处理//....//返回ItemsCustomItemsCustom itemsCustom = new ItemsCustom();//将items的属性值复制到itemsCustomBeanUtils.copyProperties(items, itemsCustom);return itemsCustom;
}public void updateItems(Integer id, ItemsCustom itemsCustom) throws Exception {//加入业务校验,通常在service接口对关键參数进行校验//校验 id是否为空,假设为空抛出异常//更新商品信息使用updateByPrimaryKeyWithBLOBs依据id更新items表中全部字段。包含 大文本类型字段//updateByPrimaryKeyWithBLOBs要求必须转入iditemsCustom.setId(id);itemsMapper.updateByPrimaryKeyWithBLOBs(itemsCustom);
}

开发controller

方法:

  • 商品信息改动页面显示
  • 商品信息改动提交
//使用@Controller来标识它是一个控制器
@Controller
//为了对url进行分类管理 ,能够在这里定义根路径,终于訪问url是根路径+子路径
//比方:商品列表:/items/queryItems.action
//@RequestMapping("/items")
public class ItemsController {@Autowiredprivate ItemsService itemsService;//商品查询列表@RequestMapping("/queryItems")//实现 对queryItems方法和url进行映射。一个方法相应一个url//一般建议将url和方法写成一样public ModelAndView queryItems() throws Exception{//调用service查找数据库,查询商品列表List<ItemsCustom> itemsList = itemsService.findItemsList(null);//返回ModelAndViewModelAndView modelAndView = new ModelAndView();//相当于request的setAttribute方法,在jsp页面中通过itemsList取数据modelAndView.addObject("itemsList",itemsList);//指定视图//下边的路径,假设在视图解析器中配置jsp的路径前缀和后缀,改动为items/itemsList//modelAndView.setViewName("/WEB-INF/jsp/items/itemsList.jsp");//下边的路径配置就能够不在程序中指定jsp路径的前缀和后缀modelAndView.setViewName("items/itemsList");return modelAndView;}//商品信息改动页面显示@RequestMapping("/editItems")//限制http请求方法,能够post和get//@RequestMapping(value="/editItems",method={RequestMethod.POST, RequestMethod.GET})public ModelAndView editItems()throws Exception {//调用service依据商品id查询商品信息ItemsCustom itemsCustom = itemsService.findItemsById(1);// 返回ModelAndViewModelAndView modelAndView = new ModelAndView();//将商品信息放到modelmodelAndView.addObject("itemsCustom", itemsCustom);//商品改动页面modelAndView.setViewName("items/editItems");return modelAndView;}//商品信息改动提交@RequestMapping("/editItemsSubmit")public ModelAndView editItemsSubmit(HttpServletRequest request, Integer id, ItemsCustom itemsCustom)throws Exception {//调用service更新商品信息,页面须要将商品信息传到此方法itemsService.updateItems(id, itemsCustom);//返回ModelAndViewModelAndView modelAndView = new ModelAndView();//返回一个成功页面modelAndView.setViewName("success");return modelAndView;}}

@RequestMapping

  • url映射

定义controller方法相应的url,进行处理器映射使用。

  • 窄化请求映射
//使用@Controller来标识它是一个控制器
@Controller
//为了对url进行分类管理 。能够在这里定义根路径,终于訪问url是根路径+子路径
//比方:商品列表:/items/queryItems.action
@RequestMapping("/items")
public class ItemsController {
  • 限制http请求方法

出于安全性考虑。对http的链接进行方法限制。

//商品信息改动页面显示//@RequestMapping("/editItems")//限制http请求方法。能够post和get@RequestMapping(value="/editItems",method={RequestMethod.POST, RequestMethod.GET})public ModelAndView editItems()throws Exception {

假设限制请求为post方法。进行get请求,即将上面代码的注解改为@RequestMapping(value="/editItems",method={RequestMethod.POST})

报错,状态码405:

controller方法的返回值

  • 返回ModelAndView

须要方法结束时,定义ModelAndView,将model和view分别进行设置。

  • 返回string

假设controller方法返回string

1.表示返回逻辑视图名。

真正视图(jsp路径)=前缀+逻辑视图名+后缀

@RequestMapping(value="/editItems",method={RequestMethod.POST,RequestMethod.GET})
//@RequestParam里边指定request传入參数名称和形參进行绑定。

//通过required属性指定參数是否必须要传入 //通过defaultValue能够设置默认值。假设id參数没有传入。将默认值和形參绑定。 //public String editItems(Model model, @RequestParam(value="id",required=true) Integer items_id)throws Exception { public String editItems(Model model)throws Exception { //调用service依据商品id查询商品信息 ItemsCustom itemsCustom = itemsService.findItemsById(1); //通过形參中的model将model数据传到页面 //相当于modelAndView.addObject方法 model.addAttribute("itemsCustom", itemsCustom); return "items/editItems"; }

2.redirect重定向

商品改动提交后,重定向到商品查询列表。

redirect重定向特点:浏览器地址栏中的url会变化。改动提交的request数据无法传到重定向的地址。由于重定向后又一次进行request(request无法共享)

//重定向到商品查询列表
//return "redirect:queryItems.action";

3.forward页面转发

通过forward进行页面转发,浏览器地址栏url不变,request能够共享。

//页面转发
return "forward:queryItems.action";
  • 返回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串");

作者@brianway很多其它文章:个人站点 | CSDN | oschina

posted on 2017-07-03 14:15 mthoutai 阅读(...) 评论(...) 编辑 收藏

转载于:https://www.cnblogs.com/mthoutai/p/7110903.html

springmvc学习笔记(10)-springmvc注解开发之商品改动功能相关推荐

  1. SpringMVC:学习笔记(10)——整合Ckeditor且实现图片上传

    SpringMVC:学习笔记(10)--整合Ckeditor且实现图片上传 配置CKEDITOR 精简文件 解压之后可以看到ckeditor/lang下面有很多语言的js,如果不需要那么多种语言的,可 ...

  2. SpringMVC学习03之使用注解开发SpringMVC

    复习 Spring MVC的特点: 轻量级,简单易学 高效 , 基于请求响应的MVC框架 与Spring兼容性好,无缝结合 约定优于配置 功能强大:RESTful.数据验证.格式化.本地化.主题等 简 ...

  3. [SpringMVC]SpringMVC学习笔记一: springmvc原理及实例解析.

    前言: 今天来回顾下SpringMVC的开发原理, 使用图文并茂的方式 来解析其中的内幕, 我相信懂了其中的运行机制后, 对于面试中SpringMVC大家都可以说so easy了. 一, 图示法 第二 ...

  4. SpringMVC学习笔记:springMVC中相关细节

    SpringMVC中相关细节 1.什么是MVC? MVC是一种软件架构思想,将软件按照模型.视图.控制器来划分. ①M:模型层:指工程中的javaBean,作用是处理数据.javaBean分为两类:一 ...

  5. SpringMVC学习笔记七:SpringMVC的数据验证

    SpringMVC支持JSR(Java Specification Requests, Java规范提案)303-Bean Validation数据验证规范,该规范的实现者很多,其中较常用的是 Hib ...

  6. java mvc框架代码_JAVA技术学习笔记:SpringMVC框架(内附入门程序开发代码)

    原标题:JAVA技术学习笔记:SpringMVC框架(内附入门程序开发代码) JavaEE体系结构包括四层,从上到下分别是应用层.Web层.业务层.持久层.Struts和SpringMVC是Web层的 ...

  7. SpringMVC学习笔记(二)常用注解

    SpringMVC学习笔记(二)常用注解 1.RequestParam 作用: 把请求中指定名称的参数给控制器中的形参赋值. 属性: value:请求参数中的名称. required:请求参数中是否必 ...

  8. 【学习笔记】SpringMVC—@RequestMapping注解

    [学习笔记]SpringMVC-@RequestMapping注解

  9. SpringMVC学习笔记

    文章目录 SpringMVC学习笔记 Spring MVC 什么是 MVC 设计模式? Spring MVC 的核心组件 Spring MVC 的工作流程 如何使用? Spring MVC 注解 Sp ...

最新文章

  1. matplotlib直方图_你真的了解matplotlib吗?---直方图(上)
  2. 代码夹带是洪水猛兽吗?
  3. 最小错误率贝叶斯决策
  4. NYOJ-42 一笔画问题
  5. Linux文件系统和文本编辑器
  6. window10安装python2.7_Windows10-python2.7安
  7. 将Windows MyEclipse的web项目移植到Debian下
  8. 打印1-400以内 能同时被5和9 整数的数将这些数放入一个列表中,再输出这个列表
  9. sentinel接入网关应用_阿里sentinel配合gateway 网关限流
  10. 谈谈应届生应聘的一点看法
  11. oracle怎么定义参数函数返回值,Oracle自定义函数记录
  12. Wolfram|Alpha搜索引擎
  13. PageHelper.startPage 分页的坑
  14. 计算机网络ip地址划分范围,ip地址分类及范围划分有哪些
  15. 关于浏览器被hao123劫持
  16. 归因分析笔记13 特征重要度正确性的验证
  17. 前列腺增生症的治疗行业调研报告 - 市场现状分析与发展前景预测
  18. 浅谈用友NC产品单点登录机制
  19. 融跃教育登陆湖南卫视!揭秘融跃是个什么样机构!CFA/FRM/ACCA
  20. (更新中)论文中 如何插入 参考文件索引

热门文章

  1. IIS 用户验证及授权
  2. Dubbo 序列化协议 5 连问,你接得住不?
  3. 一千个不用 Null 的理由,你还用?
  4. 记一次悲惨的 Excel 导出事件
  5. mysql 实时聚合分析_mysql滑动聚合/年初至今聚合原理与用法实例分析
  6. 结合泛函极值_第2章泛函的极值.doc
  7. 大型监控网络系统如何规划ip地址?
  8. 深圳出台数据中心PUE新政,或将开启千亿级节能市场
  9. python dbscan 如何确定eps参数_如何选择eps和minPts(DBSCAN算法的两个参数)以获得有效结果?...
  10. json yeid_【分享】自动格式化输出JSON的小插件分享给大家