springmvc学习笔记(10)-springmvc注解开发之商品改动功能
springmvc学习笔记(10)-springmvc注解开发之商品改动功能
标签: springmvc
本文以商品改动为例,记录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);
//中间对商品信息进行业务处理
//....
//返回ItemsCustom
ItemsCustom itemsCustom = new ItemsCustom();
//将items的属性值复制到itemsCustom
BeanUtils.copyProperties(items, itemsCustom);
return itemsCustom;
}
public void updateItems(Integer id, ItemsCustom itemsCustom) throws Exception {
//加入业务校验,通常在service接口对关键參数进行校验
//校验 id是否为空,假设为空抛出异常
//更新商品信息使用updateByPrimaryKeyWithBLOBs依据id更新items表中全部字段。包含 大文本类型字段
//updateByPrimaryKeyWithBLOBs要求必须转入id
itemsCustom.setId(id);
itemsMapper.updateByPrimaryKeyWithBLOBs(itemsCustom);
}
开发controller
方法:
- 商品信息改动页面显示
- 商品信息改动提交
//使用@Controller来标识它是一个控制器
@Controller
//为了对url进行分类管理 ,能够在这里定义根路径,终于訪问url是根路径+子路径
//比方:商品列表:/items/queryItems.action
//@RequestMapping("/items")
public class ItemsController {
@Autowired
private ItemsService itemsService;
//商品查询列表
@RequestMapping("/queryItems")
//实现 对queryItems方法和url进行映射。一个方法相应一个url
//一般建议将url和方法写成一样
public ModelAndView queryItems() throws Exception{
//调用service查找数据库,查询商品列表
List<ItemsCustom> itemsList = itemsService.findItemsList(null);
//返回ModelAndView
ModelAndView 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);
// 返回ModelAndView
ModelAndView modelAndView = new ModelAndView();
//将商品信息放到model
modelAndView.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);
//返回ModelAndView
ModelAndView 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串");
springmvc学习笔记(10)-springmvc注解开发之商品改动功能的更多相关文章
- springmvc学习笔记(13)-springmvc注解开发之集合类型參数绑定
springmvc学习笔记(13)-springmvc注解开发之集合类型參数绑定 标签: springmvc springmvc学习笔记13-springmvc注解开发之集合类型參数绑定 数组绑定 需 ...
- springmvc学习笔记(12)-springmvc注解开发之包装类型參数绑定
springmvc学习笔记(12)-springmvc注解开发之包装类型參数绑定 标签: springmvc springmvc学习笔记12-springmvc注解开发之包装类型參数绑定 需求 实现方 ...
- SpringMVC:学习笔记(10)——整合Ckeditor且实现图片上传
SpringMVC:学习笔记(10)——整合Ckeditor且实现图片上传 配置CKEDITOR 精简文件 解压之后可以看到ckeditor/lang下面有很多语言的js,如果不需要那么多种语言的,可 ...
- [SpringMVC]SpringMVC学习笔记一: springmvc原理及实例解析.
前言:今天来回顾下SpringMVC的开发原理, 使用图文并茂的方式 来解析其中的内幕, 我相信懂了其中的运行机制后, 对于面试中SpringMVC大家都可以说so easy了. 一, 图示法 第二张 ...
- SpringMVC学习笔记一:采用注解式搭建简单springMVC环境
搭建的环境使用的是maven项目 项目目录树: 搭建环境使用的jar包,pom.xml文件 <project xmlns="http://maven.apache.org/POM/4. ...
- springMVC学习笔记(一)-----springMVC原理
一.什么是springmvc springMVC是spring框架的一个模块,springMVC和spring无需通过中间整合层进行开发. springMVC是一个基于mvc的web框架. Sprin ...
- springMVC学习笔记--初识springMVC
前一段时间由于项目的需要,接触了springMVC(这里主要是讲3.1版,以下内容也是围绕这个版本展开),发觉其MVC模式真的很强大,也简单易用,完全是基于注解实现其优雅的路径配置的.想想以前接手的项 ...
- SpringMVC学习笔记七:SpringMVC的数据验证
SpringMVC支持JSR(Java Specification Requests, Java规范提案)303-Bean Validation数据验证规范,该规范的实现者很多,其中较常用的是 Hib ...
- SpringMVC学习笔记:SpringMVC框架的执行流程
一.MVC设计模式 二.SpringMVC框架介绍 三.SpringMVC环境搭建 四.SpringMVC框架的请求处理流程及体系结构
随机推荐
- 实例化flask的参数及对app的配置
Flask 是一个非常灵活且短小精干的web框架 , 那么灵活性从什么地方体现呢? 有一个神奇的东西叫 Flask配置 , 这个东西怎么用呢? 它能给我们带来怎么样的方便呢? 首先展示一下: from ...
- 关于vector.size()和string.length() 的返回类型 size_type
今天写循环的时候碰到一个问题,发现:string.length()返回的类型是size_type.它是unsigned 类型.string::size_type它在不同的机器上,长度是可以不同的,并非 ...
- Spring Boot (23) Lettuce Redis
Spring Boot除了支持常见的ORM框架外,更是对常用的中间件提供了非常好的封装,随着SpringBoot2.x的到来,支持的组件也越来越丰富,也越来越成熟,其中对Redis的支持不仅仅是它丰富 ...
- Laravel5.1学习笔记17 数据库3 数据迁移
介绍 建立迁移文件 迁移文件结构 执行迁移 回滚迁移 填写迁移文件 创建表 重命名/ 删除表 创建字段 修改字段 删除字段 建立索引 删除索引 外键约束 #介绍 Migrations are lik ...
- 常用animation动画
/*编辑动画名*/ animation-name: myDemo; /*动画持续时间*/ animation-duration: 6s; /*动画方向*/ /*reverse 反向*/ /*alter ...
- CxImage实现9PNG
CxImage* ScaleImageBy9PNG(CxImage *pRawImage, int nDstWidth,int nDstHeight) { if(NULL == pRawImage) ...
- mysql_数据查询_连接查询
连接查询 1.连接(join) 也称θ连接,从两个关系的笛卡尔积中选择属性间满足一定条件的元组. 等值连接:θ为“=”的连接运算称为等值连接.从关系R和S的广义笛卡尔积中选取A.B属性值相等的元组. ...
- tab切换案例
做个简单的tab切换效果,分别于jquery和js操作 (1)jQuery操作 先看下效果: <!DOCTYPE html> <html lang="en"> ...
- 小白年薪26万,为什么Python岗位薪资越来越高?
人工智能和大数据概念的兴起,带动了Python的快速增长——Python语言逻辑简洁.入门简单.生态丰富,几乎成为几个新兴领域的不二选择.而除了这两个领域,Python还有更多的适用领域:爬虫.web ...
- nodejs 文件操作模块 fs
const fs=require("fs"); //文件操作 //创建目录 ./ 代表当前目录 ../ 代表上级目录fs.mkdir('./test',function(err){ ...