Spring MVC注解

Spring MVC 注解
1、@Controller
@Controller 用于标记在一个类上,使用它标记的类就是一个SpringMVC Controller 对象。通过Spring 配置的注解扫描,将这个bean注册到Springmvc容器中。
2、@RequestMapping
@RequestMapping是一个用来处理请求地址映射的注解,可用于类或方法上。用于类上,表示类中的 所有响应请求的方法都是以该地址作为父路径,作用于方法上,表明该处理器的请求地址=父路径+方法 上url。
其拥有6个属性:
属性 说明
value 指定请求的实际地址,指定的地址可以是URI Template 模式
method 指定请求的method类型, GET、POST、PUT、DELETE等;
consumes 指定处理请求的提交内容类型(Content-Type),例如application/json, text/html
produces 指定返回的内容类型,仅当request请求头中的(Accept)类型中包含该指定类型才返回; params 指定request中必须包含某些参数值,才让该方法处理!
headers 指定request中必须包含某些指定的header值,才能让该方法处理请求。
Spring4.3 以后方法级别的@RequestMapping注解变体有如下几个:
注解 说明
@GetMapping 等价于:@RequestMapping(method = RequestMethod.GET) 在 @GetMapping注解的源码中有体现
@PostMapping 等价于:@RequestMapping(method = RequestMethod.POST)
@PutMapping 等价于:@RequestMapping(method = RequestMethod.PUT)
@DeleteMapping 等价于:@RequestMapping(method = RequestMethod.DELETE
3、@RequestBody
@RequestBody用于读取Request请求的body部分数据,使用系统默认配置的HttpMessageConverter 进行解析,然后把相应的数据绑定到要返回的对象上 ,再把 HttpMessageConverter返回的对象数据绑定到 controller中方法的参数上。 现在更多的是把一个 json 字符串转换成一个定义好映射关系的对象
4、@ResponseBody
ResponseBody用于将Controller的方法返回的对象,通过适当的 HttpMessageConverter转换为指定 格式后,写入到Response对象的body数据区。现在主要用于json返回。
5、@RequestParam
@RequestParam主要用于在SpringMVC后台控制层获取参数,类似request.getParameter(“name”); @RequestParam注解主要有3个参数:
value:参数名字,即入参的请求参数名字,如 value=“userName” 表示请求的参数区中的名字为 userName的参数的值将传入;
required:是否必须,默认是true,表示请求中一定要有相应的参数,否则将报404错误码;
defaultValue:默认值,表示如果请求中没有同名参数时的默认值, 默认值可以是SpEL表达式,如“#{systemProperties[‘java.vm.version’]}”。
6、@PathVariable
@PathVariable 映射 URL 绑定的占位。带占位符的 URL 是 Spring3.0 新增的功能,主要用于RestFull风 格的URL请求。 通过 @PathVariable 可以将 URL 中占位符参数绑定到控制器处理方法的入参中:URL 中的 {xxx} 占位 符可以通过@PathVariable(“xxx“) 绑定到操作方法的入参中。
@RequestMapping("/restfull/getUser/{id}/{userName}/{pass}")
public String restfull(@PathVariable(“id”)int id,@PathVariable String userName,@PathVariable(“pass”) String password){
}
7、@ModelAttribute
Controller的所有方法在调用前,先执行此@ModelAttribute方法,可用于注解和方法参数中。 可以把这个@ModelAttribute特性,应用在BaseController当中,所有的Controller继承 BaseController,即可实现在调用Controller时,先执行@ModelAttribute方法。
8、@RestController
@RestController注解就相当于@ResponseBody + @Controller合在一起的作用。
使用@RestController注解Controller,则Controller中的方法无法返回页面,配置的视图解析器(如: InternalResourceViewResolver)不起作用,返回的内容就是return 的内容。
9、SpringMVC 中文乱码解决
在web.xml中添加spring 中文过滤器
在这里插入图片描述

10、SpringMVC 获取请求参数
提前准备测试相关Controller和Jsp页面:
10.1、准备GetParameterController:
package com.gx.controller;
import com.gx.model.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
@Controller @RequestMapping("/getParameter")
public class GetParameterController {
/** *
测试页面
*/
@RequestMapping("/index")
public String index(){
return “/getParameter/index”;
}
}
10.2、页面: web/WEB-INF/jsp/getParamter/index ,用于编写测试表单:
在这里插入图片描述

10.3、页面 web/WEB-INF/jsp/getParamter/result.jsp ,用于显示表单提交结果:

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值