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. java正则验证

    String regex ="[A-Z]{4}[0-9]{7}"; List<String> non= new ArrayList<String>(); f ...

  2. Oracle 入门

    一.安装Oracle 11g 服务端 服务端安装教程:https://jingyan.baidu.com/article/363872eccfb9266e4aa16f5d.html 二.安装客户端 客 ...

  3. Java 并发AQS

    转载出处:http://www.cnblogs.com/waterystone/ 一.概述 谈到并发,不得不谈ReentrantLock:而谈到ReentrantLock,不得不谈AbstractQu ...

  4. mongodb-参考其他

    MongoDB教程 http://www.runoob.com/mongodb/mongodb-window-install.html

  5. swiper轮播的slide高度自适应

    方式1:官方给的属性 autoHeight: true, //高度随内容变化 发现实际没效果 方式2:先定义了一个slide的高度数组, //设置slide父级高度 index为slide的索引 fu ...

  6. pta l2-11(玩转二叉树)

    题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805065406070784 题意:给定二叉树的结点个数n,其前 ...

  7. opencv: Rotate image by 90, 180 or 270 degrees

    opencv2: void rotate_cw(const cv::Mat& image, cv::Mat& dest, int degrees) { ) { : dest = ima ...

  8. wasserstein 距离

    https://blog.csdn.net/nockinonheavensdoor/article/details/82055147 注明:直观理解而已,正儿八经的严谨证明看最下面的参考. Earth ...

  9. MongoDB之$关键字及$修改器$set $inc $push $pull $pop

    一.查询中常见的  等于   大于  小于  大于等于  小于等于 等于:用':' 大于:用'$gt' 小于:用'$lt' 大于等于:用'$gte' 小于等于:用'$lte' MongoDB的操作就是 ...

  10. ATX 安卓设备 WiFi 统一管理以及设备自动化测试

    众所周知,安卓单台设备的UI自动化测试已经比较完善了,有数不清的自动化框架或者工具.但是介绍多设备管理的内容并不多,当手里的手机多了之后,要做自动化测试平台,这块的东西又不得不碰,摆脱USB限制,接入 ...