学习大数据——Spring MVC中@RequestMapping映射请求注解

@RequestMapping 概念

1) SpringMVC使用@RequestMapping注解为控制器指定可以处理哪些 URL 请求
2) 作用:DispatcherServlet 截获请求后,就通过控制器上 @RequestMapping 提供的映射信息确定请求所对应的处理方法。

@RequestMapping 可标注的位置

上一篇博客中的HelloWorld.java:

package com.learn.springmvc.helloworld;

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

//@RequestMapping 可标注的位置
//@RequestMapping注解可以添加到类上,也可以添加到方法上
//@RequestMapping("HelloWorld")
@Controller
public class HelloWorld {
	/*
	 * 方法的返回值会被SpringMVC配置文件中配置的InternalResourceViewResolver这个视图解析为真是的物理视图,
	 *  然后自动进行请求转发
	 *  真是的物理视图 = 前缀 + 方法返回值 + 后缀
	 *  即:/WEB-INF/views/success.jsp
	 */
	@RequestMapping("/hello")
	public String testHelloWorld() {
		System.out.println("Hello SpringMVC!");
		return "success";
	}
}

@RequestMapping 映射请求URL与请求方式

在上一篇博客的项目中,新建了SpringMVCHandler.java用于处理测试index.jsp页面的请求。

部分SpringMVCHandler.java:

package com.learn.springmvc.helloworld;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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

import com.learn.springmvc.entities.Employee;

@Controller
public class SpringMVCHandler {

	/*
	 * @RequestMapping注解中的属性
	 * 	1.value:用来设置映射的请求地址,值得类型是String类型的数组
	 * 		如果只映射一个请求地址,那么value的值不需要添加大括号{},value属性名可以省略不写
	 * 	2.method:用来设置要映射的请求方式
	 * 		如果没有设置该属性,那么只看映射的请求地址,不管请求方式
	 */
	@RequestMapping(value= {"/testValue","/testValue2"})
	public String testValue() {
		System.out.println("测试@RequestMapping注解value属性");
		return "success";
	}
//	@RequestMapping(value="/testMethod",method=RequestMethod.GET,params="age=18")
	@RequestMapping(value="/testMethod",method=RequestMethod.POST)
	public String testMethod() {
		System.out.println("测试@RequestMapping注解method属性");
		return "success";
	}

测试页面index.jsp(使用到的是第2个<a>中的testValue2和第3个<a>中的testMethod):

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<a href="${pageContext.request.contextPath }/hello">Hello SpringMVC</a><br>
	<a href="${pageContext.request.contextPath }/testValue2">Test value</a><br>
	<a href="${pageContext.request.contextPath }/testMethod">Test method</a><br>
	<form action="${pageContext.request.contextPath }/testMethod" method="post">
		<input type="submit" value="Test Method">
	</form>
<%-- 		<a href="${pageContext.request.contextPath }/testRequestParam?username=admin&age=18">Test RequestParam</a><br> --%>
		<a href="${pageContext.request.contextPath }/testRequestParam?username=admin">Test RequestParam</a><br>
	<form action="${pageContext.request.contextPath }/testPOJO" method="post">
		员工工号:<input type="text" name="id"><br>
		员工姓名:<input type="text" name="lastName"><br>
		员工邮箱:<input type="text" name="email"><br>
		部门编号:<input type="text" name="dept.id"><br>
		部门名称:<input type="text" name="dept.name"><br>
		<input type="submit" value="Test POJO">
	</form>
			<a href="${pageContext.request.contextPath }/testServletAPI?username=admin">Test ServletAPI</a><br>
</body>
</html>
发布了37 篇原创文章 · 获赞 7 · 访问量 677

猜你喜欢

转载自blog.csdn.net/qq_40394792/article/details/104411640
今日推荐