Spring MVC 使用@RequestMapping 注解基本用法

SpringMVC4.0 + Tomcat7 + JDK7环境搭建 + (Spring4.0jar包+源码+logging+SpringIDE百度网盘下载)

首先给大家需要看看我上一篇博文,因为环境是随上一篇而来的。这一篇讲一讲Spring MVC中@RequestMapping这个注解的一般用法。

目录结构还是跟上一篇的一样,这里就不展示了,我会贴上改动了的文件。

SpringMVCTest.java

package com.hust.springmvc1;

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

@SessionAttributes(value={"user"}, types={String.class})
@Controller
@RequestMapping("/springmvc")
public class SpringMVCTest { 

    private static final String SUCCESS = "success";

    /**
     * @PathVariable 可以映射 URL 中的占位符到目标方法的参数中
     * @param id
     * @return
     */
    @RequestMapping("/testPathVariable/{id}")
    public String testPathVariable(@PathVariable("id") Integer id) {
        System.out.println("id=" + id);
        return SUCCESS;
    }

    @RequestMapping("/testAntPath/*/abc")
    public String testAntPath() {
        System.out.println("testAntPath");
        return SUCCESS;
    }

    /**
     * 了解:可以使用params和headers来更加精确的映射请求。params和headers支持简单的表达式。
     * 
     * @return
     */
    @RequestMapping(value = "testParamsAndHeaders", params = { "username", "age!=10" }, headers = {
            "Accept-Language:en-US,zh;q=0.8" })
    public String testParamsAndHeaders() {
        System.out.println("testParamsAndHeaders");
        return SUCCESS;
    }

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

    /**
     * 1.@RequestMapping 除了修饰方法,还可来修饰类
     * 2. 
     * 1).类定义处:提供初步的请求映射信息。相对于WEB应用的根目录
     * 2).方法处:提供进一步的细分映射信息。
     * 相对于类定义处的URL。若类定义处标注@RequestMapping,则方法处标记的URL相对于WEB应用的根目录
     * 
     * @return
     */
    @RequestMapping("/testRequestMapping")
    public String testRequestMapping() {
        System.out.println("testRequestMapping");
        return SUCCESS;
    }

}

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>
<body>


    <a href="springmvc/testPathVariable/1">test PathVariable</a>
    <br />
    <a href="springmvc/testAntPath/asdasd/abc">testAntPath</a>
    <br />
    <a href="springmvc/testParamsAndHeaders?username=atyexx&age=10">testMethod</a>
    <br />
    <form action="springmvc/testMethod" method="post">
        <input type="submit" value="submit">
    </form>
    <br />
    <a href="springmvc/testMethod">testMethod</a>
    <br />
    <a href="springmvc/testRequestMapping">testRequestMapping</a>
    <br />
    <a href="helloworld">Hello</a>
</body>
</html>

上面的解释都很详细,一定要动手写,写了之后基本都会理解。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值