Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 276|回复: 0

[默认分类] Spring MVC 学习 之 - URL参数传递

[复制链接]
  • TA的每日心情
    开心
    2021-12-13 21:45
  • 签到天数: 15 天

    [LV.4]偶尔看看III

    发表于 2018-7-11 17:52:09 | 显示全部楼层 |阅读模式
      在学习 Spring Mvc 过程中,有必要来先了解几个关键参数:
       @Controller:
             在类上注解,则此类将编程一个控制器,在项目启动 Spring 将自动扫描此类,并进行对应URL路由映射。

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

      @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.    
    7.     @RequestMapping(value = "/geturlparam/{id}_{menuId}.html")
    8.     public ModelAndView getUrlParam(@PathVariable("id") String id,
    9.             @PathVariable("menuId") String menuId) {
    10.         ModelAndView mode = new ModelAndView(ShowMsg);
    11.         mode.addObject("msg", "获取到的Id:" + id + ",menuId:" + menuId);
    12.         return mode;
    13.     }
    复制代码



    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.                
    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 ()
    回传实体:

    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.       
    18.      
    19. }
    复制代码

    回传 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":"王坤"}
    复制代码
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2024-4-20 03:23 , Processed in 0.373055 second(s), 37 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表