Spring MVC 学习 之 - URL参数传递详解
在学习 spring mvc 过程中,有必要来先了解几个关键参数:
@controller:
在类上注解,则此类将编程一个控制器,在项目启动 spring 将自动扫描此类,并进行对应url路由映射。
@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("王坤"); return model; }
请求:
/web1/urlinfo/getuser.json
输出:
{"userid":100,"username":"王坤"}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。