springmvc-210803-01—@RequestMapping属性value和method
value属性
@RequestMapping 注解可以定义处理器对于请求的映射规则。
该注解可以注解在方法上,也可以注解在类上,但意义是不同的。
@RequestMapping 的 value 属性用于定义所匹配请求的 URI。
如果在类上使用,则是抽取相同的url。此时的这个 URI 表示模块的名称。
method属性
用于对被注解方法所处理请求的提交方式进行限制,
即只有满足该 method 属性指定的提交方式的请求,才会执行该被注解方法。
若不指定 method 属性,则无论是 GET 还是 POST 提交方式,均可匹配。
即对于请求的提交方式无要求。
案例演示
MyController.java(处理器—使用value,method属性)
package com.bgy.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
/*
@Controller,创建处理器对象,对象发在springmvc容器中
*/
/*
@RequestMapping:
value:
用在类上面,所有请求地址的公共部分
*/
@Controller
@RequestMapping("/test")
public class MyController {
@RequestMapping(value = "/some.do")
public ModelAndView doSome(){ // 相当于doGet()
ModelAndView mv= new ModelAndView();
mv.addObject("msg","这是我第一次使用springmvc框架");
mv.addObject("function","执行了doSome方法");
mv.setViewName("show");
// 返回mv
return mv;
}
// 也可以绑定多个地址
@RequestMapping(value = {"/other.do","/second.do"})
public ModelAndView doOther(){ // 相当于doGet()
ModelAndView mv= new ModelAndView();
mv.addObject("msg","这是我第一次使用springmvc框架");
mv.addObject("function","执行了doOther方法");
mv.setViewName("other");
return mv;
}
// 测试注解@RequestMapping的method属性
@RequestMapping(value = {"/testMethod.do"} , method = RequestMethod.POST)
public ModelAndView testMethod(){
ModelAndView mv= new ModelAndView();
mv.addObject("msg","这是我第一次使用springmvc框架");
mv.addObject("function","执行了testMethod方法");
mv.setViewName("testMethod");
return mv;
}
}
index.jsp(主页面)
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>springmvc</title>
</head>
<body>
<p>第一个springmvc项目</p>
<p><a href="test/some.do">发起some.do请求</a></p>
<p><a href="test/other.do">发起other.do请求</a> </p>
<!-- 这里没有使用post提交,就会报405错误 -->
<p><a href="test/testMethod.do">测试method属性</a> </p>
<!-- 因为注解@RequestMapping定义了属性了method = RequestMethod.POST,所以这里需要一个表单 -->
<form action="test/testMethod.do" method="post">
<button type="submit">测试method属性</button>
</form>
</body>
</html>
testMethod.jsp(结果页面)
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>testMethod</title>
</head>
<body>
<h3>testMethod.jsp,,,request作用域获取结果</h3>
<br/>
<h3>msg数据:${msg}</h3>
<br/>
<h3>function数据:${function}</h3>
</body>
</html>
springmvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 开启组件扫描 -->
<context:component-scan base-package="com.bgy.controller"></context:component-scan>
<!-- 声明springmvc框架中的视图解析器,帮助开发人员设置视图文件的路径 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 前缀 -->
<property name="prefix" value="/WEB-INF/view/"></property>
<!-- 后缀 -->
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0"
metadata-complete="true">
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
运行结果
使用表单提交,表单设置了method=“post”:
method = RequestMethod.POST
不使用表单提交:
本文详细介绍了SpringMVC中@RequestMapping注解的value和method属性的使用,包括value用于指定请求URL,method用于限定HTTP请求类型。通过案例演示了如何在MyController.java中设置这两个属性,并展示了index.jsp和testMethod.jsp页面以及配置文件的设置,最后呈现了运行结果。

被折叠的 条评论
为什么被折叠?



