0
点赞
收藏
分享

微信扫一扫

【Spring学习笔记-MVC-16】Spring MVC之重定向-解决中文乱码

概述

spring MVC框架controller间跳转,需重定向,主要有如下三种:

  • 不带参数跳转:形如:http://localhost:8080/SpringMVCTest/test/myRedirectWithArgs
  • 带参数拼接url形式跳转:形如:http://localhost:8080/SpringMVCTest/test/myRedirectWithArgs?username="zhangsan"&..
  • 带参数不拼接参数跳转:形如:​​http://localhost:8080/SpringMVCTest/test/myRedirectWithArgs,但是可以传参;​​


前台index.jsp

【Spring学习笔记-MVC-16】Spring MVC之重定向-解决中文乱码_代做毕设




不带参数跳转

【Spring学习笔记-MVC-16】Spring MVC之重定向-解决中文乱码_代做毕设_02

    /**

     * 不带参数的重定向

     * 

     * @return

     * @throws Exception

     */

    @RequestMapping(value = "/myRedirectWithoutArgs")

    public String myRedirectWithoutArgs(ModelMap mmMap) throws Exception {

        System.out.println("在myRedirectWithoutArgs()方法内...");

        mmMap.addAttribute("msg", "不带参数的重定向");

        return "index";

    }  

输入:​​http://localhost:8080/SpringMVCTest/test/index/1​​ 

【Spring学习笔记-MVC-16】Spring MVC之重定向-解决中文乱码_代做毕设_03【Spring学习笔记-MVC-16】Spring MVC之重定向-解决中文乱码_代做毕设_04



带参数拼接url形式跳转

【Spring学习笔记-MVC-16】Spring MVC之重定向-解决中文乱码_Spring MVC_05


    /**

     * 带参数的重定向--拼接URL

     * 

     * @return

     * @throws Exception

     */

    @RequestMapping(value = "/myRedirectWithArgsURL")

    public String myRedirectWithArgsURL(ModelMap mmMap, Person p)

            throws Exception {

        System.out.println("在myRedirectWithArgsURL()方法内...");

        System.out.println("参数为:" + p.getUsername() + p.getPasswd());

        mmMap.addAttribute("msg",

                "带参数的重定向,参数为==>" + p.getUsername() + p.getPasswd());

        return "index";

    }  

输入:​​http://localhost:8080/SpringMVCTest/test/index/2​​  

【Spring学习笔记-MVC-16】Spring MVC之重定向-解决中文乱码_Spring MVC重定向_06【Spring学习笔记-MVC-16】Spring MVC之重定向-解决中文乱码_代做毕设_07


解决中文乱码问题

在web.xml中加入如下配置

【Spring学习笔记-MVC-16】Spring MVC之重定向-解决中文乱码_代做毕设_08

配置完成后,中文乱码解决掉了:


【Spring学习笔记-MVC-16】Spring MVC之重定向-解决中文乱码_Spring MVC重定向_09【Spring学习笔记-MVC-16】Spring MVC之重定向-解决中文乱码_spring_10





带参数不拼接参数跳转

【Spring学习笔记-MVC-16】Spring MVC之重定向-解决中文乱码_中文乱码_11


    /**

     * 带参数的重定向--不拼接URL

     * 

     * @return

     * @throws Exception

     */

    @RequestMapping(value = "/myRedirectWithArgs")

    public String myRedirectWithArgs(ModelMap mmMap, HttpServletRequest request)

            throws Exception {

        System.out.println("在myRedirectWithArgs()方法内...");

        Map<String, ?> map = RequestContextUtils.getInputFlashMap(request);

        System.out.println((String)map.get("username")+map.get("passwd"));

        mmMap.addAttribute("msg", "带参数的重定向,不拼接URL");

        return "index";

    } 


【Spring学习笔记-MVC-16】Spring MVC之重定向-解决中文乱码_中文乱码_12


输入:http://localhost:8080/SpringMVCTest/test/index/3  


【Spring学习笔记-MVC-16】Spring MVC之重定向-解决中文乱码_中文乱码_13【Spring学习笔记-MVC-16】Spring MVC之重定向-解决中文乱码_中文乱码_14





举报

相关推荐

Spring MVC学习笔记

0 条评论