SpringMVC学习(二)-RequestMapping修饰类&请求参数&请求方式&请求头

本文通过具体的示例代码展示了Spring MVC框架中控制器类及其方法的使用方式,包括路径映射、参数处理及请求方法限制等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.测试类

package com.springMVC.Helloworld;

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

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

    /*
     * 
     */
    @RequestMapping(value="testParamsAndHeaders",params= {"username","age!=10"})
    public String testParamsAndHeaders() {
        System.out.println("testParamsAndHeaders");
        return "success";
    }

    /*
     * 使用method属性来指定请求方式
     */
    @RequestMapping(value="/testMethod",method=RequestMethod.POST)
    public String testMethod() {
        System.out.println("testMethod");
        return "success";
    }

    /*
     *1.@ RequestMapping既可以修饰方法也可以修饰类
     *2.
     *  1)类定义处:提供初步映射信息(相对于WEB应用根目录)
     *  2)方法处:提供进一步的细分映射信息
     */
    @RequestMapping("/testRequestMapping")
    public String TestRequestMapping() {
        System.out.println("TestRequestMapping");
        return "success";
    }

}

2.index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head> 

<a href="springmvc/testParamsAndHeaders?username=hello&age=10">TestParamsAndHeaders</a>
<br><br>

<body>
<a href="springmvc/testRequestMapping">TestRequestMapping</a>
<br><br>
<a href="helloworld">Hello World</a>
<br><br>
<a href="springmvc/testMethod">TestMethod</a>
<br><br>
<form action="springmvc/testMethod" method="POST">
    <input type="submit" value="submit"/>
</form>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值