Spring mvc页面表单提交获取数据后台接收参数实现

lyg0 2020-06-04

1.新建一个web工程,搭建好Springmvc运行环境

2.新建两个页面(提交页面,成功页面)如下:

     2.1form 页面

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
    <!-- 提交路径 -->
    <form action="${pageContext.request.contextPath}/controller2/form"
        method="post" accept-charset="UTF-8">
        <table>
            <tr>
                <td>用户名:</td>
                <td><input type="text" name="userName"></td>
            </tr>
            <tr>
                <td>密码:</td>
                <td><input type="password" name="userPwd"></td>
            </tr>
            <tr>
                <td><input type="submit" value="登陆"></td>
            </tr>
        </table>
    </form>
</body>
</html>

   success页面

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
form page is success
</body>
</html>

  2.1、直接把表单的参数写在Controller相应的方法的形参中  

    (控制器中方法的形参String userName,String userPwd名称必须和登陆页面的name="userName",name="userPwd"相同)

package com.springmvc.tranfercontroller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * 基于注解带参数传输案例
 * 
 * @author Administrator
 *
 */
@Controller
@RequestMapping(value = "/controller2")
public class TranferController2 {
    // 成功提交跳转对应的方法
    @RequestMapping(value = "/form")
    public String form(String userName, Integer userPwd) {
        System.out.println("用户名:" + userName + "用户密码:" + userPwd);
        System.out.println("println login of controller");
        return "success";
    }

    // 访问页面路径对应的方法
    @RequestMapping(value = "/form2")
    public String form() {
        return "form";
    }
}

    2.2通过HttpServletRequest接收

(控制器中String userName=request.getParameter("userName");String userPwd=request.getParameter("userPwd");的参数名称必须和登陆页面的name="userName",name="userPwd"相同)

@Controller
    @RequestMapping("/controller2")
    public class TranferController2 {
        @RequestMapping("/form")
        public String form(HttpServletRequest request) {
            String userName=request.getParameter("userName");
            String userPwd=request.getParameter("userPwd");
            System.out.println("用户名:" + userName + "用户密码:" + userPwd);
            System.out.println("println login of controller");
            return "success";
        }
    }

3.在web项目中经常会提交表单,偶尔会遇到表单中文参数乱码

如:

  3.1、在html中head部分设置

1
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />

   3.2、在form标签中增加:

1
<form method="post" action="#" accept-charset="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>
      <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
      </init-param>
    </filter>
    <filter-mapping>
      <filter-name>encodingFilter</filter-name>
      <url-pattern>/*</url-pattern>
    </filter-mapping>

4.测试结果(注意访问路径不同)

Spring mvc页面表单提交获取数据后台接收参数实现

Spring mvc页面表单提交获取数据后台接收参数实现

相关推荐

玫瑰小妖 / 0评论 2019-11-04