一.注解开发

需求: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. C 自己实现strcpy,strcmp,strlen,strcat等函数

    // mystrlen() 测试字符长度方法 int mystrlen(char *str) { int cnt = 0; char *p= str; while(*p++ != '\0') { cn ...

  2. C++ 系列:函数可变长参数

    一.基础部分 1.1 什么是可变长参数 可变长参数:顾名思义,就是函数的参数长度(数量)是可变的.比如 C 语言的 printf 系列的(格式化输入输出等)函数,都是参数可变的.下面是 printf ...

  3. Android开发 Button的开发记录

    Button置顶层效果取消 android:stateListAnimator="@null" 在代码里执行点击 mButton.performClick(); //点击 mBut ...

  4. Git 获取项目git clone

    git clone 克隆项目 git clone 实际上是一个封装了其他几个命令的命令. 它创建了一个新目录,切换到新的目录,然后 git init 来初始化一个空的 Git 仓库, 然后为你指定的 ...

  5. COGS2355 【HZOI2015】 有标号的DAG计数 II

    题面 题目描述 给定一正整数n,对n个点有标号的有向无环图(可以不连通)进行计数,输出答案mod 998244353的结果 输入格式 一个正整数n 输出格式 一个数,表示答案 样例输入 3 样例输出 ...

  6. thinkphp 多层mvc

    hinkPHP基于MVC(Model-View-Controller,模型-视图-控制器)模式,并且均支持多层(multi-Layer)设计. 模型(Model)层 默认的模型层由Model类构成,但 ...

  7. 0916CSP-S模拟测试赛后总结

    40分-rank35. 不想找借口.实力不行. 赛时状态没出什么大问题.就是实力太弱了. 天皇又AK了.去问了一下,他说全是简单题…… 后来发现一些人用非正解AC了. 但是天皇题题正解题题首切啊. 还 ...

  8. vue知识点汇总

    一.学习vue必须了解的几个知识点  1.node.js介绍 node是一个让JavaScript运行在服务端的开发平台,使用JavaScript也可以开发后台服务.说明白些它仅仅是一个平台,我们使用 ...

  9. 尚学linux课程---4、linux网络配置及linux文件

    尚学linux课程---4.linux网络配置及linux文件 一.总结 一句话总结: linux下的etc目录是配置文件的目录,所以很多的文件配置操作都可以看到它的身影:比如 init系列命名,比如 ...

  10. day 51 阿里iconfont的使用

    阿里iconfont的使用   1. 找到阿里巴巴图标库 2.找到图标 3.搜索你想要的图标 4.将图标添加到购物车 5.点击右上角的购物车按钮,我这里添加了两个. 6.提示你登陆,不需要花钱,找其中 ...