美文网首页
SpringMVC-跳转/Session/乱码

SpringMVC-跳转/Session/乱码

作者: 通灵路耳 | 来源:发表于2020-06-22 16:15 被阅读0次

客户端跳转

基于前面“视图定位”

package com.llhc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.llhc.model.Product;
@Controller
public class ProductController {

   @RequestMapping("/index")
   public ModelAndView add(Product product) throws Exception {
       ModelAndView mav = new ModelAndView("index");
       mav.addObject("message","SpringMvC");
       return mav;
   }
   //客户端跳转
   @RequestMapping("/jump")
   public ModelAndView jump() {
       ModelAndView mav = new ModelAndView("redirect:/addProduct");
       return mav;
   }  
    
}
图片.png

Session域

基于前面“视图定位”

package com.llhc.controller;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.llhc.model.Product;
@Controller
public class ProductController {

   @RequestMapping("/index")
   public ModelAndView add(Product product) throws Exception {
       ModelAndView mav = new ModelAndView("index");
       mav.addObject("message","SpringMvC");
       return mav;
   }
 
   //Session
   @RequestMapping("/check")
    public ModelAndView check(HttpSession session) {
        Integer i = (Integer) session.getAttribute("count");
        if (i == null)
            i = 0;
        i++;
        session.setAttribute("count", i);
        ModelAndView mav = new ModelAndView("index");
        return mav;
    }
}
图片.png

中文乱码

只需要配置过滤器,设置UTF-8编码即可!

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <filter> 
        <filter-name>CharacterEncodingFilter</filter-name> 
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 
        <init-param> 
            <param-name>encoding</param-name> 
            <param-value>utf-8</param-value> 
        </init-param> 
    </filter> 
    <filter-mapping> 
        <filter-name>CharacterEncodingFilter</filter-name> 
        <url-pattern>/*</url-pattern> 
    </filter-mapping>    
</web-app>

相关文章

网友评论

      本文标题:SpringMVC-跳转/Session/乱码

      本文链接:https://www.haomeiwen.com/subject/mmzxfktx.html