在路上

 找回密码
 立即注册
在路上 站点首页 学习 查看内容

Spring MVC 学习 之 - URL参数传递详解

2017-3-7 12:50| 发布者: zhangjf| 查看: 949| 评论: 0

摘要: 在学习 Spring Mvc 过程中,有必要来先了解几个关键参数: @Controller: 在类上注解,则此类将编程一个控制器,在项目启动 Spring 将自动扫描此类,并进行对应URL路由映射。 @Controllerpublic class UserAction ...

在学习 Spring Mvc 过程中,有必要来先了解几个关键参数:

@Controller:

在类上注解,则此类将编程一个控制器,在项目启动 Spring 将自动扫描此类,并进行对应URL路由映射。

  1. @Controller
  2. public class UserAction{ }
复制代码

@RequestMapping

指定URL映射路径,如果在控制器上配置 RequestMapping ,具体请求方法也配置路径则映射的路径为两者路径的叠加 常用映射如:RequestMapping("url.html")

配置映射路径:

  1. @Controller
  2. public class UserAction
  3. {
  4. @RequestMapping(value = "/get_alluser.html")
  5. public ModelAndView GetAllUser(String Id)
  6. {
  7. }
  8. }
复制代码

以上配置映射

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映射参数

  1. /*
  2. * 直接输出 HTML,或JSON 字符串
  3. * 请求路径:
  4. * /web1/urlinfo/getcontent.html?key=rhythmk
  5. * /web1/urlinfo/getcontent.json?key=rhythmk
  6. * */
  7. @ResponseBody
  8. @RequestMapping(value = "/getcontent.**")
  9. public String GetContent(
  10. @RequestParam("key") String key,
  11. @RequestParam(value = "key2", required = false, defaultValue = "defaultValue") String key2) {
  12. System.out.println("getcontent 被调用");
  13. String result = "直接返回内容 - key:" + key + ",key2:" + key2;
  14. System.out.println(result);
  15. return result;
  16. }
复制代码
  1. /*
  2. * RequestMapping 支持 Ant 风格的URL配置 :
  3. * 请求路径:
  4. * /urlinfo/geturlant/config.html?key=adddd
  5. */
  6. @ResponseBody
  7. @RequestMapping(value = "/geturlant/**.html")
  8. public String getUrlAnt(HttpServletRequest request) {
  9. String result = "?后面的参数为:" + request.getQueryString();
  10. return result;
  11. }
复制代码
  1. /*
  2. * 配置指定格式的URL,映射到对应的参数
  3. * 请求路径:/web1/urlinfo/geturlparam/12_123.html
  4. *
  5. * */
  6. @RequestMapping(value = "/geturlparam/{id}_{menuId}.html")
  7. public ModelAndView getUrlParam(@PathVariable("id") String id,
  8. @PathVariable("menuId") String menuId) {
  9. ModelAndView mode = new ModelAndView(ShowMsg);
  10. mode.addObject("msg", "获取到的Id:" + id + ",menuId:" + menuId);
  11. return mode;
  12. }
复制代码
  1. /*
  2. * 只接收Post 请求
  3. */
  4. @ResponseBody
  5. @RequestMapping(value = "/posturl.html", method = RequestMethod.POST)
  6. public String UrlMethod(@RequestParam String id) {
  7. return "只能是Post请求,获取到的Id:" + id;
  8. }
复制代码
  1. /*
  2. * 写入 cookie
  3. * */
  4. @RequestMapping("/writecookies.html")
  5. public ModelAndView writeCookies(@RequestParam String value,
  6. HttpServletResponse response) {
  7. response.addCookie(new Cookie("key", value));
  8. ModelAndView mode = new ModelAndView(ShowMsg);
  9. mode.addObject("msg", "cookies 写入成功");
  10. return mode ;
  11. }
复制代码
  1. /*
  2. * 通过 @CookieValue 获取对应的key的值
  3. * */
  4. @RequestMapping("/getcookies.html")
  5. public ModelAndView getCookie(@CookieValue("key") String cookvalue) {
  6. ModelAndView mode = new ModelAndView(ShowMsg);
  7. mode.addObject("msg", "cookies=" + cookvalue);
  8. return mode;
  9. }
复制代码
  1. /*
  2. * 将 Servlet Api 作为参数传入
  3. * 可以在action中直接使用 HttpServletResponse,HttpServletRequest
  4. * */
  5. @RequestMapping("/servlet.html")
  6. public String Servlet1(HttpServletResponse response,
  7. HttpServletRequest request) {
  8. Boolean result = (request != null && response != null);
  9. ModelAndView mode = new ModelAndView();
  10. mode.addObject("msg", "result=" + result.toString());
  11. return ShowMsg;
  12. }
复制代码
  1. /*
  2. * 根据URL传入的参数实例化对象
  3. *
  4. * 如: http://127.0.0.1:8080/web1/urlinfo/getobject.html?UserId=1&UserName=ad
  5. * */
  6. @RequestMapping("getobject.html")
  7. public ModelAndView getObject(UserInfo user) {
  8. String result = "用户ID:" + user.getUserId().toString() + ",用户名:"
  9. + user.getUserName().toString();
  10. ModelAndView mode = new ModelAndView(ShowMsg);
  11. mode.addObject("msg", "result=" + result.toString());
  12. return mode;
  13. }
复制代码

实现页面跳转:

  1. /*
  2. * 实现页面跳转
  3. * /web1/urlinfo/redirectpage.html
  4. * */
  5. @RequestMapping("/redirectpage.html")
  6. public String RedirectPage()
  7. {
  8. return "redirect:getcookies.html?r=10";
  9. }
复制代码

直接回传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 ()

回传实体:

  1. @JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
  2. public class UserInfo {
  3. private Integer UserId;
  4. public Integer getUserId() {
  5. return UserId;
  6. }
  7. public void setUserId(Integer userId) {
  8. UserId = userId;
  9. }
  10. public String getUserName() {
  11. return UserName;
  12. }
  13. public void setUserName(String userName) {
  14. UserName = userName;
  15. }
  16. private String UserName;
  17. }
复制代码

回传 action

  1. @ResponseBody
  2. @RequestMapping("/getuser.json")
  3. public UserInfo GetUser()
  4. {
  5. System.out.println("getuser");
  6. UserInfo model=new UserInfo();
  7. model.setUserId(100);
  8. model.setUserName("王坤");
  9. return model;
  10. }
复制代码

请求:

/web1/urlinfo/getuser.json

输出:

  1. {"userId":100,"userName":"王坤"}
复制代码

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持程序员之家。

最新评论

小黑屋|在路上 ( 蜀ICP备15035742号-1 

;

GMT+8, 2025-5-4 01:46

Copyright 2015-2025 djqfx

返回顶部