Springmvc的请求注解可分为四类:

url请求地址(PathVariable)、

head请求头(RequestHeader、CookieValue)、

body请求体(RequestParam、RequestBody)、

请求类型(ModelAttribute、SeesionAttribute)、

异常处理(ControllerAdvice、ExceptionHandler)

1.注解@ModelAttribute

作用:返回响应数据到页面渲染。

@Controller
public class Hello2ModelController { @ModelAttribute
public User populateModel() {
User user=new User();
user.setAccount("ray");
return user;
}
@RequestMapping(value = "/helloWorld2")
public String helloWorld() {
return "helloWorld.jsp";
}
}

被ModelAttribute注解的方法,在执行本类的其他方法前都会先去执行该方法,所以这个大多在父类中使用,比如baseController。

2.注解@PathVariable

作用:讲ur中的参数放到方法中。

/**
* localhost:8080/springmvc/hello/pathVariable/bigsea
* localhost:8080/springmvc/hello/pathVariable/sea
* 这些URL 都会 执行此方法 并且将 <b>bigsea</b>、<b>sea</b> 作为参数 传递到name字段
* @param name
* @return
*/
@RequestMapping("/pathVariable/{name}")
public String pathVariable(@PathVariable("name")String name){
System.out.println("hello "+name);
return "helloworld";
}

3.注解@requestMapping

处理请求地址映射

4.注解@SessionAttributes

springmvc有个ModelMap用来返回数据到页面的,但是这些数据是request级别的,就是说下次再请求,这些数据就不在了,为了下次请求还能用到这些数据,可以用SessionAttributes(value=“test”),将数据存到session中,这样下次还能用,用完想要从session清除的话可以使用SessionStatus.

@Controller
@RequestMapping("/Demo.do")
@SessionAttributes(value={"attr1","attr2"})
public class Demo { @RequestMapping(params="method=index")
public ModelAndView index() {
ModelAndView mav = new ModelAndView("index.jsp");
mav.addObject("attr1", "attr1Value");
mav.addObject("attr2", "attr2Value");
return mav;
} @RequestMapping(params="method=index2")
public ModelAndView index2(@ModelAttribute("attr1")String attr1, @ModelAttribute("attr2")String attr2) {
ModelAndView mav = new ModelAndView("success.jsp");
return mav;
}
}
@RequestMapping(params="method=index3")
  public ModelAndView index4(SessionStatus status) {
  ModelAndView mav = new ModelAndView("success.jsp");
  status.setComplete();
  return mav;
}

5.ResponseBody

返回请求体,异步加载的时候使用。

springmvc相关的更多相关文章

  1. springBoot注解大全JPA注解springMVC相关注解全局异常处理

    https://www.cnblogs.com/tanwei81/p/6814022.html 一.注解(annotations)列表 @SpringBootApplication:包含了@Compo ...

  2. Spring以及SPringmvc相关问题: ServletContext -父子容器

    总结如下: 明确了Servlet规范中ServletContext的作用和意义.此外明确一个Tomcat中多个web应用,每个人web应用有唯一的一个ServletContext(全局上下文).[例子 ...

  3. springmvc相关配置和用法

    目录如下: 一.spring mvc 核心类与接口 二.spring mvc 核心流程图 三.spring mvc DispatcherServlet说明 四.spring mvc 父子上下文的说明 ...

  4. SpringMVC相关试题

    1.下列相关Spring自动装配的说法中,错误的是( ). (选择一项) A:在Spring配置文件中,可以通过<bean>元素的autowire属性指定自动装配方式B: autowire ...

  5. 项目总结SpringMVC相关

    流程文字概述1.用户发送请求至前端控制器DispatcherServlet2.DispatcherServlet收到请求调用HandlerMapping处理器映射器.3.处理器映射器找到具体的处理器, ...

  6. SpringMVC相关常用注解

    @Controller: @Controller 用于标记在一个类上,使用它标记的类就是一个SpringMVC Controller 对象 @RequestMapping: RequestMappin ...

  7. SpringMVC相关的面试题

    1.什么是springMVC springmvc是spirng框架的一个模块,是一个基于MVC框架的web框架 2.springmvc的流程 a.客户端发送请求 b.前端控制器DispatcherSe ...

  8. SpringMvc相关配置的作用

    1. <!-- 配置 handlerAdapter--> <bean class="org.springframework.web.servlet.mvc.SimpleCo ...

  9. SpringMvc 相关,bean map转换,百度天气,base64.js,jsBase64.java;

    1. Map<String, Object>与JavaBean[POJO, Model]转换; //model public class model{ private int id; pr ...

随机推荐

  1. 使用Js控制ReactRouter路由

    [使用Js控制ReactRouter路由] 首先引入PropTypes: const PropTypes = require('prop-types'); 然后定义context的router属性: ...

  2. PLM修改数据库密码

    在WINDCHILL SHELL中执行以下命令: xconfmanager -s wt.pom.dbPassword=数据库密码 -t db/db.properties -p 重启服务

  3. 无线渗透wep加密路由器

    停掉网络服务 service network-manager stop 检查现在的环境适不适合使用airmon-ng airmon-ng check 杀死可能冲突的进程 开启网卡monitor模式 a ...

  4. Charles界面介绍及使用方法

    本随笔主要内容: 一.Charles界面介绍 二.Charles使用 1.会话(Repeat.Focus.Compare.黑白名单等) 2.模拟请求做mock,使用断点.Map或Rewrite 3.指 ...

  5. windows 下 wamp php单元测试工具PHPUnit的安装

    phpunit下载网站 http://www.phpunit.cn/ 一.安装PHPUnit 1.选择版本 我用的是php版本是5.6.25 所以我选择的是PHPUnit 5.7 2.安装过程 为 P ...

  6. Ubuntu 16.04 LTS network DIASBLED解决办法

    问题 昨天正浏览着网页,突然无法连接.但右上角的wifi信号显示仍然是连接状态.于是我尝试断开再重新连接一次,没想到刚断开就报了个内部错误,然后wifi图标直接消失了.重启后虽然有wifi图标,但无法 ...

  7. cf-Global Round2-C. Ramesses and Corner Inversion(思维)

    题目链接:http://codeforces.com/contest/1119/problem/C 题意:给两个同型的由0.1组成的矩阵A.B,问A能否经过指定的操作变成B,指定操作为在矩阵A中选定一 ...

  8. poj1256(贪心+并查集)

    题目链接:http://poj.org/problem?id=1456 题意:给n件商品的价格和卖出截至时间,每一个单位时间最多只能卖出一件商品,求能获得的最大利润. 思路:首先是贪心,为获得最大利润 ...

  9. TOJ 3151: H1N1's Problem(欧拉降幂)

    传送门:http://acm.tzc.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=3151 时间限制(普通/Java): ...

  10. vue滚动行为控制——页面跳转返回上一个页面保留滚动位置

    需求分析: 一般这个功能在后台管理系统用的比较多,因为后台页面都是在当前页面打开,对于某些列表筛选页,如果列表数据比较多,页面就会滚动.当页面发生滚动,对列表数据进行查看或者编辑的时候,跳转到下一级页 ...