Spring MVC 學(xué)習(xí) 之 - URL參數(shù)傳遞詳解
在學(xué)習(xí) Spring Mvc 過(guò)程中,有必要來(lái)先了解幾個(gè)關(guān)鍵參數(shù):
@Controller:
在類上注解,則此類將編程一個(gè)控制器,在項(xiàng)目啟動(dòng) Spring 將自動(dòng)掃描此類,并進(jìn)行對(duì)應(yīng)URL路由映射。
@Controller
public class UserAction{ }
@RequestMapping
指定URL映射路徑,如果在控制器上配置 RequestMapping ,具體請(qǐng)求方法也配置路徑則映射的路徑為兩者路徑的疊加 常用映射如:RequestMapping("url.html")
配置映射路徑:
@Controller
public class UserAction
{
@RequestMapping(value = "/get_alluser.html")
public ModelAndView GetAllUser(String Id)
{
}
}
以上配置映射
http://***:8080:web1/get_alluser.html:
如在 @Controller添加 @RequestMapping(value = "/user"),則映射路徑變成
http://***:8080:web1/user/get_alluser.html
@ResponseBody
將注解方法對(duì)應(yīng)的字符串直接返回
@RequestParam
自動(dòng)映射URL對(duì)應(yīng)的參數(shù)到Action上面的數(shù)值,RequestParam 默認(rèn)為必填參數(shù)。
@PathVariable
獲取@RequestMapping 配置指定格式的URL映射參數(shù)
/*
* 直接輸出 HTML,或JSON 字符串
* 請(qǐng)求路徑:
* /web1/urlinfo/getcontent.html?key=rhythmk
* /web1/urlinfo/getcontent.json?key=rhythmk
* */
@ResponseBody
@RequestMapping(value = "/getcontent.**")
public String GetContent(
@RequestParam("key") String key,
@RequestParam(value = "key2", required = false, defaultValue = "defaultValue") String key2) {
System.out.println("getcontent 被調(diào)用");
String result = "直接返回內(nèi)容 - key:" + key + ",key2:" + key2;
System.out.println(result);
return result;
}
/*
* RequestMapping 支持 Ant 風(fēng)格的URL配置 :
* 請(qǐng)求路徑:
* /urlinfo/geturlant/config.html?key=adddd
*/
@ResponseBody
@RequestMapping(value = "/geturlant/**.html")
public String getUrlAnt(HttpServletRequest request) {
String result = "?后面的參數(shù)為:" + request.getQueryString();
return result;
}
/*
* 配置指定格式的URL,映射到對(duì)應(yīng)的參數(shù)
* 請(qǐng)求路徑:/web1/urlinfo/geturlparam/12_123.html
*
* */
@RequestMapping(value = "/geturlparam/{id}_{menuId}.html")
public ModelAndView getUrlParam(@PathVariable("id") String id,
@PathVariable("menuId") String menuId) {
ModelAndView mode = new ModelAndView(ShowMsg);
mode.addObject("msg", "獲取到的Id:" + id + ",menuId:" + menuId);
return mode;
}
/*
* 只接收Post 請(qǐng)求
*/
@ResponseBody
@RequestMapping(value = "/posturl.html", method = RequestMethod.POST)
public String UrlMethod(@RequestParam String id) {
return "只能是Post請(qǐng)求,獲取到的Id:" + id;
}
/*
* 寫(xiě)入 cookie
* */
@RequestMapping("/writecookies.html")
public ModelAndView writeCookies(@RequestParam String value,
HttpServletResponse response) {
response.addCookie(new Cookie("key", value));
ModelAndView mode = new ModelAndView(ShowMsg);
mode.addObject("msg", "cookies 寫(xiě)入成功");
return mode ;
}
/*
* 通過(guò) @CookieValue 獲取對(duì)應(yīng)的key的值
* */
@RequestMapping("/getcookies.html")
public ModelAndView getCookie(@CookieValue("key") String cookvalue) {
ModelAndView mode = new ModelAndView(ShowMsg);
mode.addObject("msg", "cookies=" + cookvalue);
return mode;
}
/*
* 將 Servlet Api 作為參數(shù)傳入
* 可以在action中直接使用 HttpServletResponse,HttpServletRequest
* */
@RequestMapping("/servlet.html")
public String Servlet1(HttpServletResponse response,
HttpServletRequest request) {
Boolean result = (request != null && response != null);
ModelAndView mode = new ModelAndView();
mode.addObject("msg", "result=" + result.toString());
return ShowMsg;
}
/*
* 根據(jù)URL傳入的參數(shù)實(shí)例化對(duì)象
*
* 如: http://127.0.0.1:8080/web1/urlinfo/getobject.html?UserId=1&UserName=ad
* */
@RequestMapping("getobject.html")
public ModelAndView getObject(UserInfo user) {
String result = "用戶ID:" + user.getUserId().toString() + ",用戶名:"
+ user.getUserName().toString();
ModelAndView mode = new ModelAndView(ShowMsg);
mode.addObject("msg", "result=" + result.toString());
return mode;
}
實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn):
/*
* 實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)
* /web1/urlinfo/redirectpage.html
* */
@RequestMapping("/redirectpage.html")
public String RedirectPage()
{
return "redirect:getcookies.html?r=10";
}
直接回傳JSON
請(qǐng)求的URL地址一定是以.json結(jié)尾,否則異常
Failed to load resource: the server responded with a status of 406 (Not Acceptable) : The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers ()
回傳實(shí)體:
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
public class UserInfo {
private Integer UserId;
public Integer getUserId() {
return UserId;
}
public void setUserId(Integer userId) {
UserId = userId;
}
public String getUserName() {
return UserName;
}
public void setUserName(String userName) {
UserName = userName;
}
private String UserName;
}
回傳 action
@ResponseBody
@RequestMapping("/getuser.json")
public UserInfo GetUser()
{
System.out.println("getuser");
UserInfo model=new UserInfo();
model.setUserId(100);
model.setUserName("王坤");
return model;
}
請(qǐng)求:
/web1/urlinfo/getuser.json
輸出:
{"userId":100,"userName":"王坤"}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 解決springmvc關(guān)于前臺(tái)日期作為實(shí)體類對(duì)象參數(shù)類型轉(zhuǎn)換錯(cuò)誤的問(wèn)題
- 詳解SpringMVC——接收請(qǐng)求參數(shù)和頁(yè)面?zhèn)鲄?/a>
- 詳解SpringMVC重定向傳參數(shù)的實(shí)現(xiàn)
- 學(xué)習(xí)SpringMVC——如何獲取請(qǐng)求參數(shù)詳解
- spring MVC中傳遞對(duì)象參數(shù)示例詳解
- springmvc 傳遞和接收數(shù)組參數(shù)的實(shí)例
- 詳解SpringMVC 自動(dòng)封裝枚舉類的方法
- 詳解Spring MVC自動(dòng)為對(duì)象注入枚舉類型
- spring mvc4的日期/數(shù)字格式化、枚舉轉(zhuǎn)換示例
- Spring MVC處理參數(shù)中的枚舉類型通用實(shí)現(xiàn)方法
相關(guān)文章
JavaWeb動(dòng)態(tài)導(dǎo)出Excel可彈出下載
這篇文章主要介紹了JavaWeb動(dòng)態(tài)導(dǎo)出Excel,對(duì)Excel可彈出進(jìn)行下載操作,感興趣的小伙伴們可以參考一下2016-03-03
SpringBoot集成redis實(shí)現(xiàn)共享存儲(chǔ)session
這篇文章主要介紹了SpringBoot集成redis實(shí)現(xiàn)共享存儲(chǔ)session的流程步驟,文中通過(guò)代碼示例介紹的非常詳細(xì),并總結(jié)了一些常見(jiàn)的錯(cuò)誤及解決方法,需要的朋友可以參考下2024-03-03
Java實(shí)現(xiàn)簡(jiǎn)易圖書(shū)借閱系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)簡(jiǎn)易圖書(shū)借閱系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
java核心編程之文件過(guò)濾類FileFilter和FilenameFilter
這篇文章主要為大家詳細(xì)介紹了java文件過(guò)濾類FileFilter和FilenameFilter,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
Spring?Boot教程之提高開(kāi)發(fā)效率必備工具lombok
這篇文章主要介紹了Spring?Boot教程之提高開(kāi)發(fā)效率必備工具lombok的相關(guān)資料,需要的朋友可以參考下2022-08-08
redis?redisTemplate數(shù)據(jù)類型基礎(chǔ)操作
這篇文章主要介紹了redis?redisTemplate數(shù)據(jù)類型基礎(chǔ)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06
java中continue和break區(qū)別詳細(xì)解析
break和continue都是跳轉(zhuǎn)語(yǔ)句,它們將程序的控制權(quán)轉(zhuǎn)移到程序的另一部分,下面這篇文章主要給大家介紹了關(guān)于java中continue和break區(qū)別的相關(guān)資料,需要的朋友可以參考下2022-11-11
Java基礎(chǔ)教程之類型轉(zhuǎn)換與多態(tài)
這篇文章主要介紹了Java基礎(chǔ)教程之類型轉(zhuǎn)換與多態(tài),本文講解了 基本類型轉(zhuǎn)換、 upcast與多態(tài)、 Object類等內(nèi)容,需要的朋友可以參考下2014-09-09

