一.注解开发

需求: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 11章_索引、触发器

    一. 索引: . 为什么要使用索引: 一本书需要目录能快速定位到寻找的内容,同理,数据表中的数据很多时候也可以为他们创建相应的“目录”,称为索引,当创建索引后查询数据也会更加高效 . Mysql中的索 ...

  2. Spark运行基本流程

  3. bootstrap-----流体布局解析

    流体布局容器 容器的width为auto,只是两边加了15px的padding. 流体布局容器 容器的width为auto,只是两边加了15px的padding. <div class=&quo ...

  4. python3 enum模块

    枚举是绑定到唯一的常量值的一组符号名称(成员).在枚举中,成员可以通过身份进行比较,枚举本身可以迭代. 1.Enum模块 该模块定义了四个枚举类,可用于定义唯一的名称和值集:Enum,IntEnum, ...

  5. MFC打开/保存文件对话框:CFileDialog

    MFC打开/保存文件对话框:CFileDialog CFileDialog   文件选择对话框的使用:首先构造一个对象并提供相应的参数,构造函数原型如下: CFileDialog::CFileDial ...

  6. echarts折线区域图

    一.使用场景 当舒张压和收缩压超过或低于他们对应的范围时,折线应该给与不同颜色.两个指标对应的范围也要填充不同的颜色. 二.实现方案 主要使用了echarts中的visualMap,series.ma ...

  7. 服务器断过一次电之后,mysql启动不了了

    公司内部服务器,周末会直接拉闸断电,之前也没问题,但这次回来发现mysql启动不了了. service mysqld start 提示: Starting MySQL.The server quit ...

  8. svg实现渐变进度圆环

    效果图 <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="ut ...

  9. nginx css,js合并插件,淘宝nginx合并js,css插件

    先下载Nginx_concat_module,下载后把它放在/usr/local/src/文件夹中,新建文件夹nginx-http-concat把下载的config  ngx_http_concat_ ...

  10. Activity详解三 启动activity并返回结果 转载 https://www.cnblogs.com/androidWuYou/p/5886991.html

    首先看演示: 1 简介 .如果想在Activity中得到新打开Activity 关闭后返回的数据,需要使用系统提供的startActivityForResult(Intent intent, int ...