博客
关于我
强烈建议你试试无所不能的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/

你可能感兴趣的文章
Compare Version Numbers leetcode
查看>>
我的友情链接
查看>>
配置WebLogic数据源
查看>>
something about kali
查看>>
rsync数据同步工具(三)
查看>>
定义图形模板
查看>>
php生成随机数:自定义函数 randstr($length)
查看>>
python学习--random和列表
查看>>
IPV4网段划分
查看>>
解决Oracle 解决Oracle ORA-12505, TNS:listener does not currently know of SID
查看>>
详解Linux下安装配置Nginx
查看>>
Maven使用笔记一
查看>>
STL deque
查看>>
Words For Today [2011-07-08]
查看>>
图像像素的获取和操作(第三天)
查看>>
(转)Array.prototype.slice.call自解
查看>>
测试1
查看>>
使用CSS3的appearance属性改变元素的外观
查看>>
inotify + rsync 实现 linux 文件实时同步
查看>>
RecyclerView的使用,打造一个通用的Adapter
查看>>