Spring MVC URL傳參
在學習 Spring Mvc 過程中,有必要來先了解幾個關鍵引數:
@Controller:
在類上註解,則此類將程式設計一個控制器,在專案啟動 Spring 將自動掃描此類,並進行對應URL路由對映。
1 2 3 4 5 |
@Controller
public class UserAction
{
}
|
@RequestMapping
指定URL對映路徑,如果在控制器上配置 RequestMapping ,具體請求方法也配置路徑則對映的路徑為兩者路徑的疊加 常用對映如: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
將註解方法對應的字串直接返回
@RequestParam
自動對映URL對應的引數到Action上面的數值,RequestParam 預設為必填引數。
@PathVariable
獲取@RequestMapping 配置指定格式的URL對映引數
/* * 直接輸出 HTML,或JSON 字串 * 請求路徑: * /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 被呼叫"); String result = "直接返回內容 - key:" + key + ",key2:" + key2; System.out.println(result); return result; }
/* * RequestMapping 支援 Ant 風格的URL配置 : * 請求路徑: * /urlinfo/geturlant/config.html?key=adddd */ @ResponseBody @RequestMapping(value = "/geturlant/**.html") public String getUrlAnt(HttpServletRequest request) { String result = "?後面的引數為:" + request.getQueryString(); return result; }
/* * 配置指定格式的URL,對映到對應的引數 * 請求路徑:/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 請求 */ @ResponseBody @RequestMapping(value = "/posturl.html", method = RequestMethod.POST) public String UrlMethod(@RequestParam String id) { return "只能是Post請求,獲取到的Id:" + id; }
/* * 寫入 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 寫入成功"); return mode ; }
/* * 通過 @CookieValue 獲取對應的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 作為引數傳入 * 可以在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; }
/* * 根據URL傳入的引數例項化物件 * * 如: 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; }
實現頁面跳轉:
/* * 實現頁面跳轉 * /web1/urlinfo/redirectpage.html * */ @RequestMapping("/redirectpage.html") public String RedirectPage() { return "redirect:getcookies.html?r=10"; }
直接回傳JSON
請求的URL地址一定是以.json結尾,否則異常
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 ()
回傳實體:
@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("aa"); return model; }
請求:
/web1/urlinfo/getuser.json
輸出:
{"userId":100,"userName":"aa"}
相關推薦
Spring MVC URL傳參
在學習 Spring Mvc 過程中,有必要來先了解幾個關鍵引數: @Controller: 在類上註解,則此類將程式設計一個控制器,在專案啟動 Spring 將自動掃描此類,並進行對應URL路由對映。 1 2 3 4 5
Spring Swagger URL傳參問題(轉)
官方文檔 require rac 必須 master 傳參 都沒有 github ecif 代碼例子: @ApiOperation(value="獲取用戶詳細信息", notes="根據url的id來獲取用戶詳細信息") @ApiImplicitParam
spring mvc 綁定參數據默認值,是否必傳,(RequestParam(value="id",defaultValue="1",required=true) )
host 模型 pri 默認 處理 ood 通過 定義 參數 @RequestMapping(value = "/detail", method = RequestMethod.GET) public String newDetail(@RequestParam(value
html--對URL傳參數進行解析
-- earch com turn indexof repl span 需要 lac 跳轉頁面需要傳參數到另外一個html頁面,跳轉鏈接可寫一個js的function function doView(articleId) { window.location.hre
通過URL傳參數,然後第二個頁面需要獲取參數
banner chan rom base its [0 val success escape /** * 方法說明:通過url參數鍵值名稱獲取參數的值 * @method getQueryString * @param name
js的form表單提交url傳參數(包含+等特殊字符)的解決方法
字符 www. mit function form表單提交 sub win tno wiki 方法一:(偽裝form表單提交) linkredwin = function(A,B,C,D,E,F,G){ var formredwin = document.cr
Spring MVC 上傳、下載、顯示圖片
title type sta direct 自動 ctu tco path stp 通過這篇文章你可以了解到: 使用 SpringMVC 框架,上傳圖片,並將上傳的圖片保存到文件系統,並將圖片路徑持久化到數據庫 在 JSP 頁面上實現顯示圖片、下載圖片 [TOC] 1.
spring mvc URL忽略大小寫
ant case color url 小寫 post config rri mvcc @Configuration public class SpringWebConfig extends WebMvcConfigurationSupport { @Overri
Spring框架學習(8)spring mvc上傳下載
class tor XML smart details targe resp imp common 內容源自:spring mvc上傳下載 如下示例: 頁面: web.xml: <?xml version="1.0" encoding="UTF-8"?>
thinkphp5 url傳參
man AS blank lan tps 手冊 HR php5 href url(‘index/blog/read‘,[‘id‘=>5,‘name‘=>‘thinkphp‘]); 手冊https://www.kancloud.cn/manual/thinkp
【spring mvc】spring mvc接收單個參數
delete div 示例 public req TP 單個 class AR spring mvc接收單個參數 示例代碼: public AjaxResult<String> deleteList(@RequestParam(value = "uid") S
js 獲取url傳參
ref col js代碼 clas The mat indexof www. In js代碼: <Script language="javascript"> function GetRequest() { var url = locati
URL 傳參中需要處理的特殊字符
tor 進行 sharp 字符 表示 light decode class 編程 例如實際請求URL如下: http://www.douwansha.com/mdeditor?data=[{"address":null,"name":"公司名稱=阿裏巴巴集團","id":
spring mvc 上傳檔案幫助類(留備用)
package com.service.impl; import com.entity.UploadInfo; import com.service.UploadHelp; import org.springframework.web.context.ContextLoader; import o
Vue中通過URL傳參
本文采用的為使用query; 傳參: this.$router.push({ path: '/urlpass', query: { id: 'a' } }) 取值:
URL傳參中特殊的字元處理
一、問題: 在做專案中,如果要傳遞的url中包含特殊字元,例如"+",但是這個+會被url會被編碼成空格。尤其是當傳遞的url是經過Base64加密或者RSA加密後的,存在特殊字元時,這裡的特殊字元一旦被url處理,就不是原先你加密的結果
spring mvc --上傳檔案,檔案和其他資料一起提交
jsp: var formdata = new FormData(); formdata.append('file', $('#file')[0].files[0]); //上傳檔案 formdata.append('id', $('#id').val(
js 獲取純web位址列中URL傳參
function GetQueryString(name) { var reg = new RegExp("(^|&)"+ name +"=
(四)flask框架使用教程系列——URL傳參、反轉URL
一、URL傳引數 1. 引數的作用 可以在相同的URL ,但是指定不同的引數,後來載入不同的資料。 2. 在flask中如何使用引數 引數需要放在兩個尖括號中; 檢視函式中需要放和url中的
Vue之url傳參
在router.js定義需要傳遞的引數 import Vue from 'vue' import Router from 'vue-router' import Home from './views/Home.vue' import Error from './view