Sping MVC接收请求参数和传出处理结果

实例代码地址

springmvc-module2

实验目的

本次实验的业务是当客户端发出携带了表单数据的请求,经过Controller组件中的方法接收数据,并传出数据给跳转页面显示。

处理流程图

这里写图片描述

接收请求参数和传出处理结果的三种方法

首先新建Maven Spring MVC项目,结构如下
这里写图片描述

login.jsp的代码如下

<%@page language="java" contentType="text/html; charset=UTF-8" %>
<html>
<head>
<meta http-equiv="content-Type" content="text/html;charset=UTF-8">
<title>登录页面</title>
</head>
<body>
<form action="login.form" method="post">
  <p>用户名: <input type="text" name="username" /></p>
  <p>密    码: <input type="password" name="password" /></p>
  <input type="submit" value="登录" />
</form>
</body>
</html>

LoginController使用三种方法接收login.jsp中的username和password的值。
LoginController类代码:

package com.hnust.springmvc;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class LoginController {
    /*方法1.使用HttpServletRequest对象*/
    @RequestMapping("/login.form")
    public String checkLogin(HttpServletRequest req){
        //接收请求参数
        String name=(String) req.getParameter("username");
        String pwd=(String) req.getParameter("password");
        //传出处理结果
        req.setAttribute("name", name);
        req.setAttribute("psw", pwd);

        //TODO Controller可以进行一系列的数据处理
        return "ok";
    }

    /*方法2.使用RequestParam*/
    @RequestMapping("/login1.form")
    public String checkLogin1(@RequestParam("username")String name,
                              @RequestParam("password")String password,
                              Model model){
        //将参数放入数据模型,默认是放入Request对象中
        model.addAttribute("name", name);
        model.addAttribute("psw",password);

        //TODO Controller可以进行一系列的数据处理
        return "ok";
    }

    /*方法3
     * 适用于需要传递参数较多的情况
     * 需要定义一个User POJO类
     * 要求User类的变量名和传递的key名完全一致,由SpringMVC框架内实现参数传递
     * */
    @RequestMapping("/login2.form")
    public ModelAndView checkLogin2(User user){

        ModelAndView mav = new ModelAndView();
        System.out.println("-----------------");
        mav.setViewName("ok");
        mav.getModel().put("name", user.getUsername());
        mav.getModel().put("psw", user.getPassword());

        //TODO Controller可以进行一系列的数据处理
        return mav;
    }
}

解决页面请求传递中文参数乱码问题

JSP页面中有用户名和密码框,浏览器(前台)往服务器(后台)传递参数时使用的是GBK的编码方式,如果用户名是中文,那么request对象接收的参数会出现乱码情况,可以使用setCharacterEncoding()方法进行编码转换,一般使用utf-8,该方法要放置在getParameter()方法之前,也就是要用在获取参数之前。而且,该指定只对POST方法有效,对GET方法无效。分析原因,应该是在执行第一个getParameter()的时候,Java将会按照编码分析所有的提交内容,而后续的getParameter()不再进行分析,所以setCharacterEncoding()无效。而对于GET方法提交表单是,提交的内容在URL中,一开始就已经按照编码分析提交内容,setCharacterEncoding()自然就无效。前面介绍了三种参数传递的方法,只有第一种方法直接使用到了Request对象,所以只有第一种方法可以使用req.setCharacterEncoding(“utf-8”)来解决中文乱码问题。
使用过滤器可以轻松解决参数传递中中文乱码的问题,并且对于以上三种方法都适用。
只需要在web.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>*.form</url-pattern>
    </filter-mapping>

当表单提交发出login.form请求时,请求会被fiter过滤器拦截,进行相关的字符编码处理。

实验结果

这里写图片描述

这里写图片描述

猜你喜欢

转载自blog.csdn.net/superxiaolong123/article/details/81169031
今日推荐