博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring MVC 使用@RequestMapping 注解基本用法
阅读量:7235 次
发布时间:2019-06-29

本文共 2332 字,大约阅读时间需要 7 分钟。

首先给大家需要看看我上一篇博文,因为环境是随上一篇而来的。这一篇讲一讲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"%>
Insert title here test PathVariable
testAntPath
testMethod
testMethod
testRequestMapping
Hello

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

转载地址:http://btmfm.baihongyu.com/

你可能感兴趣的文章
Redis 缓存服务配置与使用
查看>>
easyui-tabs图标(获取焦点时显示图标,失去焦点时隐藏图标)
查看>>
Android L中间RecyclerView 、CardView 、Palette使用
查看>>
Crystal Reports "Access to report file denied. Another program may be using it."
查看>>
sun.misc.BASE64Encoder我找不到jar一揽子解决方案
查看>>
Github上传代码菜鸟超详细教程
查看>>
iOS中FMDB的使用
查看>>
Oracle学习(七):集合运算
查看>>
Eclipse开发Java程序入门,HelloWord
查看>>
udhcpc和udhcpd移植
查看>>
关于Entity Framework中的Attached报错相关解决方案的总结
查看>>
不同风格的网页登录界面设计学习
查看>>
Android custom View AirConditionerView hacking
查看>>
DateTime还是DateTimeOffset?Now还是UtcNow?
查看>>
js中arguments,caller,callee,apply的用法小结
查看>>
HDU2037 今年暑假不AC 【贪心】
查看>>
[Oracle] - 性能优化工具(1) - AWR
查看>>
memcached Java Client
查看>>
codeforces #261 C题 Pashmak and Buses(瞎搞)
查看>>
体系结构复习2——指令级并行(分支预測和VLIW)
查看>>