04_springmvc注解开发
一.注解开发
需求:1、进入商品查询列表页面。
2、点击修改,进入商品修改页面,页面中显示了要修改的商品(从数据库查询),要修改的商品从数据库查询,根据商品id(主键)查询商品信息。
3、在商品修改页面,修改商品信息,修改后,点击提交。
1.mapper开发
这里已经有逆向工程生成对应的mapper.xml和mapper.java
2.service开发
功能:商品查询、根据id查询商品信息、根据id修改商品信息、根据id删除商品信息、
package com.ssm.service;
import com.ssm.po.ItemsCustom;
import com.ssm.po.ItemsQueryVo;
import java.util.List;
/**
* Description:ItemsService
* User: jiatp
* Date:2019/9/7 0007 下午 5:22
*/
public interface ItemsService {
//商品查询
public List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo)throws Exception;
//根据id查询商品信息
public ItemsCustom findItemsById(int id)throws Exception;
//根据id修改商品信息
public void updateItems(Integer id,ItemsCustom itemsCustom)throws Exception;
//根据id删除商品信息
public void deleteItems(Integer[] ids)throws Exception;
}
3.开发controller
功能:修改页面的展示、修改页面的提交、
package com.ssm.controller;
import com.ssm.exception.CustomException;
import com.ssm.po.ItemsCustom;
import com.ssm.po.ItemsQueryVo;
import com.ssm.service.ItemsService;
import com.ssm.validation.ValidGroup1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import javax.annotation.Resource;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.*;
/**
* Description:
* User: jiatp
* Date:2019/9/7 0007 下午 8:40
*/
@Controller
@RequestMapping("/items")
public class ItemsController {
@Autowired
private ItemsService itemsService;
//商品信息查询
@RequestMapping("/queryItems")
public ModelAndView queryItems(HttpServletRequest request, ItemsQueryVo itemsQueryVo) throws Exception{
String id = request.getParameter("id");
System.out.println(id);
//调用service查找数据库,查询商品列表,这里使用静态模拟
List<ItemsCustom> itemsList = itemsService.findItemsList(itemsQueryVo);
//返回ModelAndView
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("itemsList",itemsList);//相当于request.setAtttributes
modelAndView.setViewName("items/itemsList");//指定返回的视图
return modelAndView;
}
/**
* @Author jiatp
* @Date 2019/9/8 0008 下午 12:21
* @MethodName
* @MethodParameter
* @MethodReturnType
* @declare 商品修改页展示
*/
// @RequestMapping(value = "/editItems",method = {RequestMethod.GET,RequestMethod.POST})
// public ModelAndView editItems() throws Exception{
// //调用servie查询 根据id
// ItemsCustom itemsCustom = itemsService.findItemsById(1);
//
// //定义ModelAndView
// ModelAndView modelAndView = new ModelAndView();
// //将商品信息放在modelandView
// modelAndView.addObject("itemsCustom",itemsCustom);
// //页面展示
// modelAndView.setViewName("items/editItems");
// return modelAndView;
// }
// controller方法的返回值 这里演示String
/*
@RequestParam 指定request传入参数和形参进行绑定
required = true 指定参数是否需要传入
defaultvalue 可以设置默认值如果id参数没有传入,将默认值和形参进行绑定
*/
@RequestMapping(value = "/editItems",method = {RequestMethod.GET,RequestMethod.POST})
public String editItems(Model model, @RequestParam(value = "id",required = true) Integer item_id) throws Exception{
//调用servie查询 根据id
ItemsCustom itemsCustom = itemsService.findItemsById(item_id);
model.addAttribute("itemsCustom",itemsCustom);
return "items/editItems";
}
//商品信息页的修改提交、
// 在校验pojo之前添加 @Validated,校验之后添加BindingResult接受校验出错的参数
// 这两个配对出现 顺序是固定的
@RequestMapping("/editItemsSubmit")
public String editItemsSubmit(Model model, HttpServletRequest request, Integer id,ItemsCustom itemsCustom) throws Exception{
//其它操作
//调用servie更新页面信息
itemsService.updateItems(id,itemsCustom);
//重定向 return "forward:queryItems.action"
return "success";
}
}
注解@RequestMapping
功能:
- url映射:定义controller方法对应的url,进行处理器映射使用。
- .窄化请求映射:对url进行分类管理,可以这里定义根路径,最终访问的路径是:根路径+子路径
3..限制http请求方法:出于安全性考虑,对http的链接进行方法限制。如果限制请求为post方法,进行get请求,报错:
可以这样定义:
controller方法的返回值
1.返回ModelAndView:需要方法结束时,定义ModelAndView,将model和view分别进行设置。
2.返回string:如果controller方法返回string,
redirect重定向:redirect重定向特点:浏览器地址栏中的url会变化。修改提交的request数据无法传到重定向的地址。因为重定向后重新进行request(request无法共享)
forward页面转发:通过forward进行页面转发,浏览器地址栏url不变,request可以共享。
3.返回void
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串");
测试........
04_springmvc注解开发的更多相关文章
- SpringMVC注解开发初步
一.(补充)视图解析器---XmlViewResolver 作用:分离配置信息. 在视图解析器---BeanNameViewResolver的基础之上进行扩充,新建一个myView.xml分离信息 在 ...
- SpringMVC的注解开发入门
1.Spring MVC框架简介 支持REST风格的URL 添加更多注解,可完全注解驱动 引入HTTP输入输出转换器(HttpMessageConverter) 和数据转换.格式化.验证框架无缝集成 ...
- Struts2框架之-注解开发
Struts2主要解决了从JSP到Action上的流程管理,如何进行Uri和action类中每个方法的绑定这是重点,在这里先简单看一下配置文件中的简单配置: <span style=" ...
- Java自定义注解开发
一.背景 最近在自己搞一个项目时,遇到可需要开发自定义注解的需求,对于没有怎么关注这些java新特性的来说,比较尴尬,索性就拿出一些时间,来进行研究下自定义注解开发的步骤以及使用方式.今天在这里记下, ...
- Annotation(四)——Struts2注解开发
Hibernate和Spring框架的开发前边总结了,这次看一下流行的MVC流程框架Struts2的注解开发吧.Struts2主要解决了从JSP到Action上的流程管理,如何进行Uri和action ...
- Annotation(三)——Spring注解开发
Spring框架的核心功能IoC(Inversion of Control),也就是通过Spring容器进行对象的管理,以及对象之间组合关系的映射.通常情况下我们会在xml配置文件中进行action, ...
- Annotation(一)——注解开发介绍
<p>在编程中,一直强调的一点就是注释的编写,注释的规范等等.尤其是越是核心,程序越复杂,逻辑越多的清空下,注释的编写对我们以后的阅读代码,维护软件起着至关重要的作用.一款软件有着好的注释 ...
- spring注解开发中常用注解以及简单配置
一.spring注解开发中常用注解以及简单配置 1.为什么要用注解开发:spring的核心是Ioc容器和Aop,对于传统的Ioc编程来说我们需要在spring的配置文件中邪大量的bean来向sprin ...
- 使用Java注解开发自动生成SQL
使用注解开发的好处就是减少配置文件的使用.在实际过程中,随着项目越来越复杂,功能越来越多,会产生非常多的配置文件.但是,当配置文件过多,实际维护过程中产生的问题就不容易定位,这样就会徒劳的增加工作量. ...
随机推荐
- spark 变量使用 broadcast、accumulator
broadcast 官方文档描述: Broadcast a read-only variable to the cluster, returning a [[org.apache.spark.broa ...
- 读取数据库的数据并转换成List<>
一.在有帮助类DbHelperSQL的时候 1.下为其中返回SqlDataReader的方法 /// <summary> /// 执行查询语句,返回SqlDataReader ( 注意:调 ...
- 判断MDI窗体的子窗体是否存在
//***************************************************************************//函 数名: CreateForm//返 回 ...
- BZOJ 1089 (SCOI 2003) 严格n元树
Description 如果一棵树的所有非叶节点都恰好有n个儿子,那么我们称它为严格n元树.如果该树中最底层的节点深度为d (根的深度为0),那么我们称它为一棵深度为d的严格n元树.例如,深度为2的严 ...
- CentOS 7.2部署NTP服务器实现时间同步
CentOS 7.2部署NTP服务器实现时间同步 [日期:2017-12-18] 来源:Linux社区 作者:梁明远 [字体:大 中 小] 1. 前言 对于容器编排系统,前段时间主要研究kube ...
- 为什么程序员都不喜欢使用switch,而是大量的 if……else if ?
作者:熊爸爸 原文:http://3g.163.com/tech/article/E02RDE6C0511SDDL.html 请用5秒钟的时间查看下面的代码是否存在bug. OK,熟练的程序猿应该已经 ...
- Django杂篇(1)
目录 Django杂篇(1) bulk_create Pagination 创建多对多表关系的常用方法 form校验组件的应用 渲染页面 展示错误信息 校验数据 常用字段 Django杂篇(1) 这里 ...
- 解决Eclipse建立Maven Web项目后找不到src/main/java资源文件夹的办法
问题如题,明细见下图: 解决方法: 在项目上右键选择properties,然后点击java build path,在Librarys下,编辑JRE System Library,选择workspace ...
- pycharm IDE在导入自定义模块时提示有错,但实际没错
在建立python项目时,有时为了区分资源和代码,如在项目文件夹下新建img和src两个文件夹,这时导入自定义模块会提示错误,结果没错但感觉别扭.如: 这是因为pycharm提示功能是从根目录上去寻找 ...
- 网页qq在线交谈
网页中如何启用QQ交谈 1. 登录QQ, 打开网址:http://shang.qq.com/v3/widget.html 启用QQ通讯组件. 2. 选择组件样式,设置提示语,例如: 3. 刷新页面,C ...