SpringMVC_002_@RequestMapping

@RequestMapping是用来处理映射请求的注解;

@RequestMapping可以用于修饰方法和类;

类定义处的@RequestMapping相当于WEB应用的根目录;
方法定义处的@RequestMapping相对于类的URL;当类有@RequestMapping指定路径,则无法访问的请求要加上类定义的路径;

下面的直接testRequestMapping就没法找到对应的方法;springmvc/testRequestMapping才能访问;

1.value, method

value:指定请求的实际地址。value有三种赋值方式:

1)可以指定为普通的具体值;
2)可以指定为含有某变量的一类值(URI Template Patterns with Path Variables);
3)可以指定为含正则表达式的一类值( URI Template Patterns with Regular Expressions);

method:指定请求的method类型, GET、POST、PUT、DELETE;

value的普通传值

package com.ack.handler;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HelloWorld {
	@RequestMapping("firstDemo")
	public String hello(){
		System.out.println("Hello world");
		return "success";
	}
}
/firstDemo访问HelloWorld.java/hello方法;

只有value一个参数可以省略value=

package com.ack.handler;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("springmvc")
public class RequestMappingTest {
	private static final String SUCCESS = "success";
	
	@RequestMapping("testRequestMapping")
	public String testRequestMapping(){
		System.out.println("-->>springmvc/testRequestMapping方法");
		return SUCCESS;
	}
}

注意:@RequestMapping隐式地为我们加了个斜杠来划分目录层级(我们在value值前面并没有加/)。

添加method,request提交方式:

package com.ack.handler;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("springmvc")
public class RequestMappingTest {
	private static final String SUCCESS = "success";
	
	@RequestMapping(value="/testMethod", method=RequestMethod.POST)
	public String testMethod(){
		System.out.println("-->>springmvc/testMethod方法");
		return SUCCESS;
	}
}

springmvc/testMethod报错:HTTP Status 405 - Request method 'GET' not supported

因为a标签是GET请求,所以无法访问;

@PathVariable方式

package com.ack.handler;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("springmvc")
public class RequestMappingTest {
	private static final String SUCCESS = "success";
	
	@RequestMapping("userName/{user_name}")
	public String testMethodWithPathVariable(@PathVariable String user_name){
		System.out.println(user_name + "-->>springmvc/testMethodWithPathVariable方法");
		return SUCCESS;
	}
}

/springmvc/userName/chensan,通配符前的内容匹配,确定访问的方法,通配符参数为实际传递的参数,方法用@PathVariable方式接收参数;

正则匹配方式

也是用@PathVariable,只是用正则表达式匹配

package com.ack.handler;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("springmvc")
public class RequestMappingTest {
	private static final String SUCCESS = "success";
	
	@RequestMapping("regexp/{age:[\\d]{1,2}}")
	public String testMethodWithRegularExpression(@PathVariable int age){
		System.out.println(age + "-->>springmvc/testMethodWithRegularExpression方法");
		return SUCCESS;
	}
}
2.consumes,produces

consumes: 指定处理请求的提交内容类型(Content-Type),例如application/json, text/html;

produces:    指定返回的内容类型,仅当request请求头中的(Accept)类型中包含该指定类型才返回;

package com.ack.handler;

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.RequestMethod;

@Controller
@RequestMapping("springmvc")
public class RequestMappingTest {
	private static final String SUCCESS = "success";

	@RequestMapping(value="testConsumes", consumes="application/x-www-form-urlencoded")
	public String testConsumes(){
		System.out.println("-->>springmvc/testConsumes方法");
		return SUCCESS;
	}
}

之前consumes="text/html",a标签直接访问springmvc/testConsumes,始终是Http Status 415错误。The server refused this request because the request entity is in a format not supported by the requested resource for the requested method. 都快两天了网上也找不到答案,form表单也试过,准备放弃了的,不知道怎么突然把consumes="application/x-www-form-urlencoded",form表单提交,竟然通过了;既然知道问题那就是需要根据提交的情况去对应Content-Type就好了。

可参考:

HTTP中支持的Content-Type:http://tool.oschina.net/commons

Media Type介绍:http://www.iteye.com/topic/1127120

package com.ack.handler;

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.RequestMethod;

@Controller
@RequestMapping("springmvc")
public class RequestMappingTest {
	private static final String SUCCESS = "success";

	@RequestMapping(value="testProduces", produces="application/xml")
	public String testProduces(){
		System.out.println("-->>springmvc/testProduces方法");
		return SUCCESS;
	}
}
produces不知道怎么都可以访问通过,不清楚是不是Accept text/html, application/xhtml+xml, */*后面*号的问题;

3.params, header

params: 指定request中必须包含某些参数值时才让该方法处理请求。

params的参数必须是如果列举出来,访问链接就必须包含这个参数,否则就无法访问;

params参数比较运算的参数则访问时只能用等于或不等于,用大于或小于没法比较;需要注意当为比较参数为不等于时,省略该参数又不报错又不会报错;

headers: 指定request的header(请求头)必须包含某些指定的内容才能让该方法处理请求。

package com.ack.handler;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("springmvc")
public class RequestMappingTest {
	private static final String SUCCESS = "success";
	
	@RequestMapping(value="testParamsAndHeaders", params={"userName", "age != 20"})
	public String testParamsAndHeaders(){
		System.out.println("-->>springmvc/testParamsAndHeader方法");
		return SUCCESS;
	}
}

在上面的基础上加上header

@RequestMapping(value="testParamsAndHeaders", params={"userName", "age!=20"}, headers={"Accept-Language=zh-CN"})
public String testParamsAndHeaders(){
	System.out.println("-->>springmvc/testParamsAndHeader方法");
	return SUCCESS;
}

当前请求的Accept-Language=zh-CN可以正常访问,当将header改为Accept-Language=en-US就没法访问了




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值