博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpringMVC注解
阅读量:6848 次
发布时间:2019-06-26

本文共 3918 字,大约阅读时间需要 13 分钟。

hot3.png

1.PathVariable注解类

@RequestMapping注解中使用占位符的情况下,需要使用@PathVariable注解指定占位符参数,通过@RequestMapping可以将URL中占位符参数绑定到控制器处理方法的入参中

SpringMVCTest.java

package com.f145a.springmvc.handlers;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;@RequestMapping("/springmvc")@Controllerpublic class SpringMVCTest {		private static final String SUCCESS="success";		/**	 * @PathVariable可以来映射URL中的占位符到目标方法的参数中	 */	@RequestMapping("/testPathVariable/{id}")	public String testPathVariable(@PathVariable("id")Integer id){		System.out.println("testPathVariable:"+id);		return SUCCESS;	}}

index.jsp页面,点击链接可以看到值被传给方法输出在控制台

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%>
Insert title heretestPathVariable

2.RequestParam注解类

在springMVC后台控制层获取参数的方式主要有两种,一种是request.getParameter("name"),另一种是用注解@RequestParam直接获取

@RequestParam有三个属性:value、required、defaultValue

-required的值为false时,如果不传值的话,会给参数赋值为null,而required的值为true时,表示必须传值,否则抛出异常

-defaultValue=“0”,表示在没有值传入的情况下默认值为0

不带任何参数,表示需要的参数名与标注的变量名相同;参数类型尽量不要使用基本类型int、long等,而应该使用具体的对象类Integer、Long、String等

SpringMVC.java

package com.f145a.springmvc.handlers;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;;@RequestMapping("/springmvc")@Controllerpublic class SpringMVCTest {		private static final String SUCCESS="success";		/**	 * @RequestParam来映射请求参数	 * value		请求参数的参数名	 * required		该参数是否是必须的,默认为true	 * defaulValue	请求参数的默认值	 */	@RequestMapping(value="/testRequestParam")	public String testRequestParam(@RequestParam(value="name")String name,			@RequestParam(value="age",required=false,defaultValue="2")Integer age){		System.out.println("testRequestParam:name="+name+"age="+age);		return SUCCESS;	}}

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%>
Insert title heretestRequestParam

3.RequestHeader注解类

@RequestHeader注解,可以把Request请求header部分的值绑定到方法的参数上

SpringMVC.java

package com.f145a.springmvc.handlers;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestHeader;import org.springframework.web.bind.annotation.RequestMapping;@RequestMapping("/springmvc")@Controllerpublic class SpringMVCTest {		private static final String SUCCESS="success";		/**	 * 映射请求头信息	 */	@RequestMapping("/testRequestHeader")	public String testRequestHeader(@RequestHeader(value="Accept-Language")String al){		System.out.println("testRequestHeader,Accept-Language:"+al);		return SUCCESS;	}}

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%>
Insert title heretestRequestHeader

4.CookieValue注解

@CookieValue注解,读取Cookie中的值,并赋值给变量

SpringMVC.java

package com.f145a.springmvc.handlers;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.CookieValue;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestHeader;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RequestParam;;@RequestMapping("/springmvc")@Controllerpublic class SpringMVCTest {		private static final String SUCCESS="success";		/**	 * @CookieValue	映射一个Cookie值	 */	@RequestMapping("/testCookieValue")	public String testCookieValue(@CookieValue("JSESSIONID")String sessid){		System.out.println("testCookieValue:sessionId="+sessid);		return SUCCESS;	}}

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%>
Insert title heretestCookieValue

欢迎访问我的个人博客

转载于:https://my.oschina.net/chengzequn/blog/780076

你可能感兴趣的文章