返回顶部
首页 > 资讯 > 后端开发 > Python >SpringMVC中@RequestMapping注解用法实例
  • 815
分享到

SpringMVC中@RequestMapping注解用法实例

2024-04-02 19:04:59 815人浏览 独家记忆

Python 官方文档:入门教程 => 点击学习

摘要

目录1 修饰类和方法2 value3 method4 params和headers5 Ant路径5 @PathVariable6 HiddenHttpMethodFilter7 @R

1 修饰类和方法

package site.exciter.springMVC.handlers;

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

@RequestMapping("/springmvc")
@Controller
public class SpringMVCTest {

    
    @RequestMapping("/testRequestMapping")
    public String testRequestMapping() {
        System.out.println("testRequestMapping");
        return "success";
    }
}

2 value

@RequestMapping("/springmvc")可以改为@RequestMapping(value = "springmvc")

3 method

post请求

controller

 
@RequestMapping(value = "testMethod", method = RequestMethod.POST)
    public String testMethod() {
        System.out.println("testMethod");
        return "success";
    }

jsp

<fORM action="/springmvc/testMethod" method="post">
    <input type="submit" value="submit">
</form>

4 params和headers


    @RequestMapping(value = "testPramsAndHeaders",
            params = {"username", "age!=10"}, headers = {})
    public String testPramsAndHeaders() {
        System.out.println("testPramsAndHeaders");
        return "success";
    }
<a href="/springmvc/testPramsAndHeaders?username=exciter&age=10" rel="external nofollow" >testPramsAndHeaders</a>

当age=10时,访问会出错;当age为其他值时,正常访问。

http://localhost:8080/springmvc/testPramsAndHeaders?username=exciter&age=10

5 Ant路径

 
    @RequestMapping("/testAntPath
    @RequestMapping("/testPathVariable/{id}")
    public String testPathVariable(@PathVariable("id") Integer id) {
        System.out.println("testPathVariable:" + id);
        return "success";
    }
&lt;a href="/springmvc/testPathVariable/1" rel="external nofollow" &gt;testPathVariable&lt;/a&gt;

6 HiddenHttpMethodFilter

HiddenHttpMethodFilter:浏览器 form 表单只支持 GET、POST、HEAD 请求,而DELETE、PUT 等 method 并不支 持,Spring3.0 添加了一个过滤器,可以将这些请求转换为标准的 http 方法,使得支持 GET、POST、PUT 与DELETE 请求。

web.xml中配置HiddenHttpMethodFilter

<!--  配置org.springframework.web.filter.HiddenHttpMethodFilter,可以把 POST 请求转为 DELETE 或 PUT 请求   -->
    <filter>
        <filter-name>HiddenHttpMethodFilter</filter-name>
        <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>HiddenHttpMethodFilter</filter-name>
        <url-pattern>
    @RequestMapping(value = "/testRest/{id}", method = RequestMethod.DELETE)
    public String testRestDelete(@PathVariable Integer id) {
        System.out.println("testRestDelete:" + id);
        return "success";
    }
<a href="/springmvc/testRest/1" rel="external nofollow" >
    <button>TestRest GET</button>
</a>
<form action="/springmvc/testRest" method="post">
    <input type="submit" value="TestRest POST">
</form>
<form action="/springmvc/testRest/1" method="post">
    <input type="hidden" name="_method" value="PUT">
    <input type="submit" value="TestRest PUT">
</form>
<form action="/springmvc/testRest/1" method="post">
    <input type="hidden" name="_method" value="DELETE">
    <input type="submit" value="TestRest DELETE">
</form>

注意:Tomcat8.0以上请求完接口之后返回页会报错:

HTTP状态 405 - 方法不允许
类型 状态报告

消息 JSP 只允许 GET、POST 或 HEAD。Jasper 还允许 OPTIONS

描述 请求行中接收的方法由源服务器知道,但目标资源不支持

临时解决方案:在返回的jsp中添加isErrorPage="true"

<%@ page contentType="text/html;charset=UTF-8" language="java" isErrorPage="true" %>

7 @RequestParam


    @RequestMapping("/testRequestParam")
    public String testRequestParam(@RequestParam(value = "username") String username,
                                   @RequestParam(value = "age", required = false, defaultValue = "0") int age) {
        System.out.println("testRequestParam: username=" + username + "  age=" + age);
        return "success";
    }
<a href="/springmvc/testRequestParam?username=exciter&age=20" rel="external nofollow"  rel="external nofollow" >testRequestParam</a>

8 @RequestHeader


@RequestMapping("/testRequestHeader")
    public String testRequestHeader(@RequestHeader(value = "Accept-Language") String al) {
        System.out.println("testRequestHeader:" + al);
        return "success";
    }
<a href="/springmvc/testRequestParam?username=exciter&age=20" rel="external nofollow"  rel="external nofollow" >testRequestParam</a>

9 @CookieValue

  @RequestMapping("/testCookieValue")
    public String testCookieValue(@CookieValue(value = "JSESSIONID") String sessionId) {
        System.out.println("testCookieValue:" + sessionId);
        return "success";
    }

总结

到此这篇关于SpringMVC中@RequestMapping注解用法的文章就介绍到这了,更多相关SpringMVC @RequestMapping用法内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: SpringMVC中@RequestMapping注解用法实例

本文链接: https://lsjlt.com/news/152359.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作