1、项目搭建见 项目链接
2、RequestMapper是一个可以用于类和方法的注解,当写在注解上的时候,向服务器发送的请求就是http://localhost/项目名/方法RequestMapper注解的value属性值,如果类上也有RequestMapper注解,那么请求就是http://localhost/项目名/类RequestMapper注解的value值/方法RequestMapper的注解value值。
1、当控制器的注解情况如下的时候
package com.springmvc.helloWorld;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* 请求处理器/控制器
* */
@Controller
@RequestMapping("/spring")
public class SpringMVCHandle {
/**
* 这个方法就是处理http:localhost:8080/Demo1/hello SpringMVC
* */
@RequestMapping("/hello SpringMVC")
public String handleHelloSpringMVC(){
System.out.println("hello SpringMVC");
return "success";
}
}
2、在jsp中写如下的请求,可以知道上面的是可以成功得到相应的,而下边的请求则会为找不到对应的请求相应方法
<%--
Created by IntelliJ IDEA.
User: 11955
Date: 2019/3/14
Time: 0:32
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>index.jsp</title>
</head>
<body>
<%--这一句请求因为可以与请求相应方法映射成功,所以可以实现跳转--%>
<a href="spring/hello SpringMVC">spring/hello SpringMVC</a>
<a href="hello SpringMVC">hello SpringMVC</a>
<%--这里只是写了一个简单的请求,它会给服务器发http://localhost:8080/Demo1/SpringMVC,这样的请求--%>
</body>
</html>
3、Request的两个属性
1、value:就像上边演示的那样,value的值会被拼成完成的请求发送给服务器
2、mathod,用于指定请求的方法,在下边的例子中,当使用post方法请求RequestMapper mehod属性为get的时候,会返回405的错误。
<%--
Created by IntelliJ IDEA.
User: 11955
Date: 2019/3/14
Time: 0:32
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>index.jsp</title>
</head>
<body>
<form action="spring/hello SpringMVC" method="post">
<input type="submit" value="submit" id="submit">
</form>
<br>
<%--这一句请求因为可以与请求相应方法映射成功,所以可以实现跳转--%>
<a href="spring/hello SpringMVC">spring/hello SpringMVC</a>
<br>
<a href="hello SpringMVC">hello SpringMVC</a>
<%--这里只是写了一个简单的请求,它会给服务器发http://localhost:8080/Demo1/SpringMVC,这样的请求--%>
</body>
</html>