Spring MVC 学习 之 - URL参数传递详解
在学习SpringMvc过程中,有必要来先了解几个关键参数:
@Controller:
在类上注解,则此类将编程一个控制器,在项目启动Spring将自动扫描此类,并进行对应URL路由映射。
@Controller publicclassUserAction{}
@RequestMapping
指定URL映射路径,如果在控制器上配置RequestMapping ,具体请求方法也配置路径则映射的路径为两者路径的叠加常用映射如:RequestMapping("url.html")
配置映射路径:
@Controller publicclassUserAction { @RequestMapping(value="/get_alluser.html") publicModelAndViewGetAllUser(StringId) { } }
以上配置映射
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.**") publicStringGetContent( @RequestParam("key")Stringkey, @RequestParam(value="key2",required=false,defaultValue="defaultValue")Stringkey2){ System.out.println("getcontent被调用"); Stringresult="直接返回内容-key:"+key+",key2:"+key2; System.out.println(result); returnresult; }
/* *RequestMapping支持Ant风格的URL配置: *请求路径: */urlinfo/geturlant/config.html?key=adddd */ @ResponseBody @RequestMapping(value="/geturlant/**.html") publicStringgetUrlAnt(HttpServletRequestrequest){ Stringresult="?后面的参数为:"+request.getQueryString(); returnresult; }
/* *配置指定格式的URL,映射到对应的参数 *请求路径:/web1/urlinfo/geturlparam/12_123.html * **/ @RequestMapping(value="/geturlparam/{id}_{menuId}.html") publicModelAndViewgetUrlParam(@PathVariable("id")Stringid, @PathVariable("menuId")StringmenuId){ ModelAndViewmode=newModelAndView(ShowMsg); mode.addObject("msg","获取到的Id:"+id+",menuId:"+menuId); returnmode; }
/* *只接收Post请求 */ @ResponseBody @RequestMapping(value="/posturl.html",method=RequestMethod.POST) publicStringUrlMethod(@RequestParamStringid){ return"只能是Post请求,获取到的Id:"+id; }
/* *写入cookie **/ @RequestMapping("/writecookies.html") publicModelAndViewwriteCookies(@RequestParamStringvalue, HttpServletResponseresponse){ response.addCookie(newCookie("key",value)); ModelAndViewmode=newModelAndView(ShowMsg); mode.addObject("msg","cookies写入成功"); returnmode; }
/* *通过@CookieValue获取对应的key的值 **/ @RequestMapping("/getcookies.html") publicModelAndViewgetCookie(@CookieValue("key")Stringcookvalue){ ModelAndViewmode=newModelAndView(ShowMsg); mode.addObject("msg","cookies="+cookvalue); returnmode; }
/* *将ServletApi作为参数传入 *可以在action中直接使用HttpServletResponse,HttpServletRequest **/ @RequestMapping("/servlet.html") publicStringServlet1(HttpServletResponseresponse, HttpServletRequestrequest){ Booleanresult=(request!=null&&response!=null); ModelAndViewmode=newModelAndView(); mode.addObject("msg","result="+result.toString()); returnShowMsg; }
/* *根据URL传入的参数实例化对象 * *如:http://127.0.0.1:8080/web1/urlinfo/getobject.html?UserId=1&UserName=ad **/ @RequestMapping("getobject.html") publicModelAndViewgetObject(UserInfouser){ Stringresult="用户ID:"+user.getUserId().toString()+",用户名:" +user.getUserName().toString(); ModelAndViewmode=newModelAndView(ShowMsg); mode.addObject("msg","result="+result.toString()); returnmode; }
实现页面跳转:
/* *实现页面跳转 */web1/urlinfo/redirectpage.html **/ @RequestMapping("/redirectpage.html") publicStringRedirectPage() { return"redirect:getcookies.html?r=10"; }
直接回传JSON
请求的URL地址一定是以.json结尾,否则异常
Failedtoloadresource:theserverrespondedwithastatusof406(NotAcceptable):Theresourceidentifiedbythisrequestisonlycapableofgeneratingresponseswithcharacteristicsnotacceptableaccordingtotherequest"accept"headers()
回传实体:
@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL) publicclassUserInfo{ privateIntegerUserId; publicIntegergetUserId(){ returnUserId; } publicvoidsetUserId(IntegeruserId){ UserId=userId; } publicStringgetUserName(){ returnUserName; } publicvoidsetUserName(StringuserName){ UserName=userName; } privateStringUserName; }
回传action
@ResponseBody @RequestMapping("/getuser.json") publicUserInfoGetUser() { System.out.println("getuser"); UserInfomodel=newUserInfo(); model.setUserId(100); model.setUserName("王坤"); returnmodel; }
请求:
/web1/urlinfo/getuser.json
输出:
{"userId":100,"userName":"王坤"}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。