SpringMVC常用注解實例詳解1:@Controller,@RequestMapping,@RequestParam,@PathVariable
我的開發環境
框架: springmvc+spring+freemarker
開發工具: springsource-tool-suite-2.9.0
JDK版本: 1.6.0_29
tomcat版本:apache-tomcat-7.0.26
前置文章-SpirngMVC配置入門 http://www.cnblogs.com/sunang/p/3419544.html
Spring整合Freemarker http://www.cnblogs.com/sunang/p/3419676.html
本文地址:http://www.cnblogs.com/sunang/p/3421707.html 轉載請注明出處^_^
要注意的點已经用 標注,請大家要特別注意。
1.@Controller,@RequestMapping
用@Controller註釋的類才會被SpringMVC解析器搜索到,這個註釋是必須的。@RequestMapping註釋用於指定controller方法的URL索引。這兩個注解的具體用法請看以下代碼:
controller层LearnMVCController.java代码:
package www.asuan.com.controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; @Controller
@RequestMapping("/learnMVC")//父索引 可省略
public class LearnMVCController {
@RequestMapping("/ex") //子索引
public String learnMVC() {
return "learnMVC.ftl";
}
}
视图learnMVC.ftl代码:
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h2>Hello World!</h2>
</body>
</html>
在有父索引的请况下,访问:http://localhost:8080/你的工程名/learnMVC/ex.htm
在省略父索引的请况下,访问:http://localhost:8080/你的工程名/ex.htm 就可将请求发送到所需controller方法。
运行结果:

2.@RequestParam
該註釋用於controller方法註釋中,用於綁定由視圖傳過來的參數,代碼如下:
controller代碼
package www.asuan.com.controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam; @Controller
@RequestMapping("/learnMVC")
public class LearnMVCController {
@RequestMapping("/ex")
public String learnMVC(@RequestParam(value = "userId", required = false) Integer userIdGot, Model model) {
String str = "成功得到綁定數據:" + userIdGot;
model.addAttribute("userIdGot", str);
return "learnMVC.ftl";
}
}
@RequestParam註釋參數中,控制器會掃描視圖傳遞過來的、參數名與value值相同的參數,此處為userId,掃描到參數后將參數的值綁定到註釋後面聲明的變量中,此處為userIdGot.
例如我們訪問地址:http://localhost:8080/你的工程名/learnMVC/ex.htm?userId=10001
那麼首先控制器掃描到URL鏈接後面帶的變量userId與value的值相同,所以控制器把變量userId的值10001賦給了@RequestParam後面聲明的變量userIdGot,這樣我們就得到了前臺傳過來的參數。
required參數為true的時候,如果你沒傳所需的參數,,程序將報錯。required參數可省略,省略時,其值默認為false.
注意:這個例子當中,如果聲明變量Integer userIdGot改成int userIdGot,而你又沒有傳相應的參數,訪問頁面將報錯。因為當控制器找不到匹配的變量時,會把null賦給這個變量,而null值裝化為int的時候會報錯,所以此處應用包裝類。
編寫視圖文件如下:
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h2>${userIdGot}</h2>
</body>
</html>
在瀏覽器訪問:http://localhost:8080/你的工程名/learnMVC/ex.htm?userId=10001
運行結果如下:

3.@PathVariable
@PathVariable用於綁定URL路徑上添加的參數,例如本文的連接:http://www.cnblogs.com/sunang/p/3421707.html 如果http://www.cnblogs.com/sunang是我們訪問控制層的路徑的話,那麼/p/3421707就是我們添加的參數,通過@PathVariable就可以綁定獲取這些參數在服務端進行相應操作。詳細使用方法情況以下代碼:
首先編寫learnMVC.ftl,代碼如下:
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h2>${msgGot}</h2>
</body>
</html>
編寫controller層,代碼如下:
package www.asuan.com.controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping; @Controller
@RequestMapping("/learnMVC")
public class LearnMVCController {
//子索引由 ex+參數+參數 組成
@RequestMapping("/ex/{type}/{articleId}")
public String learnMVC(@PathVariable(value = "type") String typeGot,
@PathVariable(value = "articleId") Integer articleIdGot, Model model) {
String str = "URL後面所傳輸的參數為:" + typeGot + "/" + articleIdGot;
model.addAttribute("msgGot", str);
return "learnMVC.ftl";
}
}
@PathVariable的實現原理和@RequestParam相似,控制器掃描@RequestMapping路徑中、{}號裏面的參數名,把參數值綁定到與@PathVariable的value值相等的變量中,比如上例中,{type}的值被綁定到value="type"的註釋後面聲明的變量typeGot中,參數的個數可以自由設定,這裡為兩個參數。
注意:在訪問URL時,必須用ex/參數值/參數值 的格式才能訪問到上例中的controller方法,另外還要注意參數的類型。
現在讓我們運行工程,在瀏覽器訪問:http://localhost:8080/你的工程名/learnMVC/ex/p/10086.htm 注:此處p對應{type},10086對應{articleId}
運行結果:

complete!
SpringMVC常用注解實例詳解1:@Controller,@RequestMapping,@RequestParam,@PathVariable的更多相关文章
- SpringMVC常用注解實例詳解3:@ResponseBody
我的開發環境框架: springmvc+spring+freemarker開發工具: springsource-tool-suite-2.9.0JDK版本: 1.6.0_29tomcat ...
- SpringMVC常用注解實例詳解2:@ModelAttribute
我的開發環境框架: springmvc+spring+freemarker開發工具: springsource-tool-suite-2.9.0JDK版本: 1.6.0_29tomcat ...
- SpringMVC 常用注解 详解
SpringMVC 常用注解 详解 SpringMVC 常用注解 1.@RequestMapping 路径映射 2.@Requ ...
- 转:springmvc常用注解标签详解
Spring5:@Autowired注解.@Resource注解和@Service注解 - IT·达人 - 博客园--这篇顺序渐进,讲得超级好--此人博客很不错http://www.cnblogs.c ...
- springmvc常用注解与类型转换
springmvc常用注解与类型转换 一:前置 spring -servlet.xml 注入 <!-- 启用spring mvc 注解 --> <context:annotation ...
- SpringMVC常用注解@Controller,@Service,@repository,@Component
SpringMVC常用注解@Controller,@Service,@repository,@Component controller层使用@controller注解 @Controller 用于标记 ...
- 一 : springmvc常用注解
springmvc常用注解详解1.@Controller在SpringMVC 中,控制器Controller 负责处理由DispatcherServlet 分发的请求,它把用户请求的数据经过业务处理层 ...
- Spring 和 SpringMVC 常用注解和配置(@Autowired、@Resource、@Component、@Repository、@Service、@Controller的区别)
Spring 常用注解 总结内容 一.Spring部分 1.声明bean的注解 2.注入bean的注解 3.java配置类相关注解 4.切面(AOP)相关注解 5.事务注解 6.@Bean的属性支持 ...
- springmvc常用注解标签详解
1.@Controller 在SpringMVC 中,控制器Controller 负责处理由DispatcherServlet 分发的请求,它把用户请求的数据经过业务处理层处理之后封装成一个Model ...
随机推荐
- java.lang.IllegalStateException: Couldn't read row 1, col 0 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data fr
Android中操作Sqlite遇到的错误:java.lang.IllegalStateException: Couldn't read row 1, col 0 from CursorWindow. ...
- 去除bootstrap模态框半透明阴影
当使用bootstrap模态框默认自带半透明阴影,如果想要去除阴影,需要怎么做呢? 今天在项目中我遇到了这个问题,想要去除模态框的阴影,试了好久都没解决.后来问同事的时候才知道,当模态框弹出后,会加上 ...
- cf727e
题意:给你一个模式串和一堆长度相同的不相同的匹配串,问是否有一个方案可以让这个模式串由这些匹配串首尾相连组成,每个串只能出现一次. 思路:还是比较简单的,显然模式串每个位置最多匹配一个匹配串,因为所有 ...
- MyBatis怎么防止SQL注入
SQL注入是一种代码注入技术,用于攻击数据驱动的应用,恶意的SQL语句被插入到执行的实体字段中(例如,为了转储数据库内容给攻击者).[摘自] SQL injection - Wikipedia SQL ...
- JS入门之ActiveXObject对象(转载)
JS入门之ActiveXObject对象 此对象提供自动化对象的接口. function ActiveXObject(ProgID : String [, location : String] ...
- .Net Mail SMTP 发送网络邮件
刚刚迈入"开发"的行列 一直有一个想法 我什么时候能给我庞大的用户信息数据库给每一位用户邮箱发送推荐信息呢? 刚迈入"编程两个月的时间" 我采用 SMTP 发送 ...
- NGUI 屏幕自适应大屏与小屏(初始设定宽高为1280x720,能适应比其小或者更大的屏)
具体细节可以参考另外一篇随笔! 以下提供的算法完成的事: 1.自适应1280x720分辨率以下的屏幕 2.自适应1280x720分辨率以上的屏幕 在我设定的要求内包括的分辨率大部分都测过了,背景图.全 ...
- 使用 Date 和 SimpleDateFormat 类表示时间、Calendar类和Math类
一. Date 和 SimpleDateFormat类表示时间 在程序开发中,经常需要处理日期和时间的相关数据,此时我们可以使用 java.util 包中的 Date 类.这个类最主要的作用就是获取当 ...
- strstr函数的用法
C语言函数 编辑 包含文件:string.h 函数名: strstr 函数原型: extern char *strstr(char *str1, const char *str2); 语法: ...
- pip 加速方案
每当我pip install * 的时候,总是发现速度很慢,通过google,发现还是有方法来解决这种状况的 在~/ 命令下,创建 .pip/pip.conf,我用的是阿里的镜像,速度还是杠杠的 mk ...