【Spring學習筆記-MVC-16】Spring MVC之重定向-解決中文亂碼


概述


spring MVC框架controller間跳轉,需重定向,主要有如下三種:


前台index.jsp





不帶參數跳轉



    /**
     * 不帶參數的重定向
     * 
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/myRedirectWithoutArgs")
    public String myRedirectWithoutArgs(ModelMap mmMap) throws Exception {
        System.out.println("在myRedirectWithoutArgs()方法內...");
        mmMap.addAttribute("msg""不帶參數的重定向");
        return "index";
    }  




帶參數拼接url形式跳轉




    /**
     * 帶參數的重定向--拼接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";
    }  



解決中文亂碼問題
在web.xml中加入如下配置

      
      
      
              
  1. <filter>
  2. <filter-name>CharacterEncodingFilter</filter-name>
  3. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  4. <init-param>
  5. <param-name>encoding</param-name>
  6. <param-value>utf-8</param-value>
  7. </init-param>
  8. </filter>
  9. <filter-mapping>
  10. <filter-name>CharacterEncodingFilter</filter-name>
  11. <url-pattern>/*</url-pattern>
  12. </filter-mapping>

配置完成后,中文亂碼解決掉了:





帶參數不拼接參數跳轉




    /**
     * 帶參數的重定向--不拼接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";
    } 







其他


參考文章:  




附件列表

     


    免責聲明!

    本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



     
    粵ICP備18138465號   © 2018-2025 CODEPRJ.COM