一.注解开发

需求: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

功能:

  1. url映射:定义controller方法对应的url,进行处理器映射使用。
  2. .窄化请求映射:对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注解开发的更多相关文章

  1. SpringMVC注解开发初步

    一.(补充)视图解析器---XmlViewResolver 作用:分离配置信息. 在视图解析器---BeanNameViewResolver的基础之上进行扩充,新建一个myView.xml分离信息 在 ...

  2. SpringMVC的注解开发入门

    1.Spring MVC框架简介 支持REST风格的URL 添加更多注解,可完全注解驱动 引入HTTP输入输出转换器(HttpMessageConverter) 和数据转换.格式化.验证框架无缝集成 ...

  3. Struts2框架之-注解开发

    Struts2主要解决了从JSP到Action上的流程管理,如何进行Uri和action类中每个方法的绑定这是重点,在这里先简单看一下配置文件中的简单配置: <span style=" ...

  4. Java自定义注解开发

    一.背景 最近在自己搞一个项目时,遇到可需要开发自定义注解的需求,对于没有怎么关注这些java新特性的来说,比较尴尬,索性就拿出一些时间,来进行研究下自定义注解开发的步骤以及使用方式.今天在这里记下, ...

  5. Annotation(四)——Struts2注解开发

    Hibernate和Spring框架的开发前边总结了,这次看一下流行的MVC流程框架Struts2的注解开发吧.Struts2主要解决了从JSP到Action上的流程管理,如何进行Uri和action ...

  6. Annotation(三)——Spring注解开发

    Spring框架的核心功能IoC(Inversion of Control),也就是通过Spring容器进行对象的管理,以及对象之间组合关系的映射.通常情况下我们会在xml配置文件中进行action, ...

  7. Annotation(一)——注解开发介绍

    <p>在编程中,一直强调的一点就是注释的编写,注释的规范等等.尤其是越是核心,程序越复杂,逻辑越多的清空下,注释的编写对我们以后的阅读代码,维护软件起着至关重要的作用.一款软件有着好的注释 ...

  8. spring注解开发中常用注解以及简单配置

    一.spring注解开发中常用注解以及简单配置 1.为什么要用注解开发:spring的核心是Ioc容器和Aop,对于传统的Ioc编程来说我们需要在spring的配置文件中邪大量的bean来向sprin ...

  9. 使用Java注解开发自动生成SQL

    使用注解开发的好处就是减少配置文件的使用.在实际过程中,随着项目越来越复杂,功能越来越多,会产生非常多的配置文件.但是,当配置文件过多,实际维护过程中产生的问题就不容易定位,这样就会徒劳的增加工作量. ...

随机推荐

  1. MySQL 07章_子查询

    子查询就是查询中还可以嵌套其他的查询,通常是内层查询的结果作为外层查询的条件来使用 执行循序,自内向外依次执行 一.内层查询返回“单列单行”的结果 -- 1.查询宋江的出生日期 SELECT TIME ...

  2. arm-linux-ar 和 arm-linux-ranlib 的使用

    静态库是在编译时需要的库. 1. 建立一个静态库 [arm@localhost gcc]#arm­linux­ar ­r libhello.a h1.o h2.o 2. 为静态库建立索引 [arm@l ...

  3. jmeter遇到的问题:java.net.ConnectException: Connection refused: connect

    1.使用jmeter执行脚本,在察看结果树的的响应数据中看到的错误提示如下: java.net.ConnectException: Connection refused: connect at jav ...

  4. thinkphp 规则路由

    规则路由是一种比较容易理解的路由定义方式,采用ThinkPHP设计的规则表达式来定义. 规则表达式 规则表达式通常包含静态地址和动态地址,或者两种地址的结合,例如下面都属于有效的规则表达式: 'my' ...

  5. LUOGU P2290 [HNOI2004]树的计数(组合数,prufer序)

    传送门 解题思路 \(prufer\)序,就是所有的不同的无根树,都可以转化为唯一的序列.做法就是每次从度数为\(1\)的点中选出一个字典序最小的,把这个点删掉,并把这个点相连的节点加入序列,直到只剩 ...

  6. Linux课程---14、linux下lamp环境如何安装

    Linux课程---14.linux下lamp环境如何安装 一.总结 一句话总结: 要按顺序安装,比如apache需要在php之前安装, 一.安装 gcc 编译器 二.卸载 rpm 安装的 http ...

  7. mysql mac客户端: sequel,mysql-workbench

    sequel: https://sequelpro.com/download#auto-start mysql-workbench:https://dev.mysql.com/downloads/fi ...

  8. node.js在ubuntu上和windows上的安装

    Ubuntu 上安装 Node.js Node.js 源码安装 以下部分我们将介绍在Ubuntu Linux下安装 Node.js . 其他的Linux系统,如Centos等类似如下安装步骤. 在 G ...

  9. 依赖注入(DI)

    Spring依赖注入(DI)的三种方式,分别为: 1.  接口注入 2.  Setter 方法注入 3.  构造方法注入 依赖注入是一种思想,或者说是一种设计模式,在java中是通过反射机制实现,与具 ...

  10. How to use view controller containment

    https://www.hackingwithswift.com/example-code/uikit/how-to-use-view-controller-containment private f ...