SpringMVC——4——向页面传值、重定向、请求参数乱码问题

先准备一个含有表单的页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>登录页面</title>
</head>
<body>
    <h1>登录页面</h1>
    <%--
        /demo/login1.do
        /demo/login2.do
        /demo/login3.do
        /demo/login4.do
        /demo/login5.do
        /demo/login6.do
        /demo/login7.do
        /demo/login8.do
    --%>
    <form action="${pageContext.request.contextPath}/demo/login9.do" method="post">
        编  号*:<input type="text" name="num"><br>
        用户名:<input type="text" name="username"><br>
        密  码: <input type="password" name="password"><br>
        <input type="submit" value="登录">
    </form>
</body>
</html>

一、向页面传值的方式

1、使用HttpServletRequest对象绑定数据转发给jsp

使用HttpServletRequest对象绑定数据转发给jsp,通过JavaBean封装请求参数值,将数据绑定到HttpServletRequest

@RequestMapping("/login4.do")
  public String login4(UserParam userParam,HttpServletRequest request){
    System.out.println("login4()");
    int num = userParam.getNum();
    String username = userParam.getUsername();
    String password = userParam.getPassword();
    //将数据绑定到request
    request.setAttribute("num",num);
    request.setAttribute("username",username);
    request.setAttribute("password",password);
    request.setAttribute("userParam",userParam);
    //默认情况下,DispatcherServlet会默认使用转发。转发:forward:index,重定向:redirect:index
    return "index";
  }

2、使用ModelAndView对象

通过JavaBean封装请求参数值放到map中,然后将数据封装到ModelAndView对象作为方法的返回值

@RequestMapping("/login5.do")
  public ModelAndView login5(UserParam userParam){
    System.out.println("login5()");
    int num = userParam.getNum();
    String username = userParam.getUsername();
    String password = userParam.getPassword();
    //创建一个Map
    Map<String,Object> data = new HashMap<String,Object>();
    //底层会把key作为绑定名,相当于执行了req.setAttribute()方法
    data.put("num",num);
    data.put("username",username);
    data.put("password",password);
    //创建ModelAndView对象
    ModelAndView mav = new ModelAndView("index", data);
    mav.addObject(userParam);
    return mav;
  }

3、使用ModelMap对象

将该对象作为方法的参数,然后将数据绑定到该对象。

@RequestMapping("/login6.do")
  public String login6(UserParam userParam, ModelMap modelMap){
    System.out.println("login6()");
    int num = userParam.getNum();
    String username = userParam.getUsername();
    String password = userParam.getPassword();
    //相当于执行了request.setAttribute()
    modelMap.addAttribute("num",num);
    modelMap.addAttribute("username",username);
    modelMap.addAttribute("password",password);
    modelMap.addAttribute("userParam",userParam);
    return "index";
  }

4、使用HttpSession对象

将数据绑定到session对象

注:(若重定向302使用session绑定数据,使用请求对象的话在请求之后就被销毁了)

@RequestMapping("/login7.do")
  public String login7(UserParam userParam, HttpSession session){
    System.out.println("login7()");
    int num = userParam.getNum();
    String username = userParam.getUsername();
    String password = userParam.getPassword();
    session.setAttribute("num",num);
    session.setAttribute("username",username);
    session.setAttribute("password",password);
    session.setAttribute("userParam",userParam);
    return "index";
  }

5、通过EL表达式获取绑定的值

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>SpringMVC请求与响应</title>
</head>
<body>
    <h1>登录成功...</h1>
    <h2>向页面传值方式</h2>
    <ol>
        <li>绑定数据到request</li>
        <li>使用ModelAndView对象作为方法返回值</li>
        <li>使用ModelMap对象</li>
        <li>绑定数据到HttpSession</li>
    </ol>
    <%----%>
    <hr>
    <h2>1.通过HttpServletRequest</h2>
    <h3>${num}</h3>
    <h3>${username}</h3>
    <h3>${password}</h3>
    <h3>${requestScope.userParam}</h3>
    <hr>
    <h2>2.通过ModelAndView</h2>
    <h3>${requestScope.num}</h3>
    <h3>${requestScope.username}</h3>
    <h3>${requestScope.password}</h3>
    <%--<h3>${pageScope.password}</h3>
    <h3>${sessionScope.password}</h3>
    <h3>${applicationScope.password}</h3>--%>
    <h3>${requestScope.userParam}</h3>

    <hr>
    <h2>3.通过ModelMap</h2>
    <h3>${requestScope.num}</h3>
    <h3>${requestScope.username}</h3>
    <h3>${requestScope.password}</h3>
    <h3>${requestScope.userParam}</h3>

    <hr>
    <h2>4.通过HttpSession</h2>
    <h3>${sessionScope.num}</h3>
    <h3>${sessionScope.username}</h3>
    <h3>${sessionScope.password}</h3>
    <h3>${sessionScope.userParam}</h3>
</body>
</html>

二、重定向

参考原文

1、方法返回值是String类型

        如果返回值是String(视图名),在重定向地址前加 "redirect:xxx.do",这样跳转的请求和原请求就是两个不相干的请求,跳转的请求会丢失掉原请求的中的所有数据,一般的解决方法是将原请求中的数据放到跳转请求的链接中这样来获取数据。

        重定向是浏览器端向一个新地址发送请求,应写成:"redirect:xxx.do"的形式。

@RequestMapping("/login8.do")
  public String login8(){
    System.out.println("login8()");
    return "redirect:toIndex.do";
  }

  @RequestMapping("/toIndex.do")
  public String toIndex(){
    System.out.println("toIndex()");
    return "index";
  }

2、返回值是ModelAndView

使用RedirectView对象↓

   springMVC源码分析--页面跳转RedirectView(三)_归田的博客-CSDN博客_redirectview
   简单来说RedirectView实现了链接的重定向,并且将数据保存到FlashMap中, 这样在跳转后的链接中可以获取一些数据。

RequestMapping("/login9.do")
  public ModelAndView login9(){
    //创建RedirectView
    System.out.println("login9()");
    RedirectView redirectView = new RedirectView("toIndex.do");
    ModelAndView modelAndView = new ModelAndView(redirectView);

    return modelAndView;
  }

三、请求参数乱码问题

1、为什么会产生乱码?

        表单提交时,浏览器会对中文进行编码(会使用打开表单所在页面时的字符集来编码,比如使用utf-8来编码).而服务器端默认使用ISO-8859-1来解码,所以会产生乱码。

2、如何解决乱码?

        springmvc提供了一个过滤器(CharacterEncodingFilter), 只需要配置该过滤器即可。

注意:

(1)表单的提交方式必须是"post"。

(2)编码与浏览器端的一致。

<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>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring MVC中,POST请求参数出现乱码问题通常是由于字符编码不一致导致的。为了解决这个问题,可以按照以下步骤进行处理: 1. 首先,在你的Spring MVC配置文件中添加一个字符编码过滤器。在web.xml文件中添加以下代码: ```xml <filter> <filter-name>encodingFilter</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>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> ``` 这将确保所有请求都使用UTF-8编码。 2. 如果你正在使用Spring Boot,你可以在application.properties或application.yml文件中添加以下配置: ```properties spring.http.encoding.charset=UTF-8 spring.http.encoding.force=true spring.http.encoding.enabled=true ``` 3. 确保发送请求页面或客户端也使用相同的字符编码。例如,在HTML页面中,可以通过以下方式设置编码: ```html <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> ``` 4. 如果以上步骤都没有解决问题,你可以尝试在处理请求的方法上添加`@RequestMapping`注解,并指定`produces`和`consumes`属性。例如: ```java @RequestMapping(value = "/yourEndpoint", produces = "application/json; charset=UTF-8", consumes = "application/x-www-form-urlencoded; charset=UTF-8") public String handleRequest(@RequestParam("param") String param) { // 处理请求参数 ... } ``` 这样可以确保请求和响应都使用UTF-8编码。 希望这些步骤能够帮助你解决Spring MVC请求参数POST出现乱码问题

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值