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


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

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

         2.1form 页面

     1 <%@ page language="java" contentType="text/html; charset=utf-8"
     2     pageEncoding="utf-8"%>
     3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     4 <html>
     5 <head>
     6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
     7 <title>Insert title here</title>
     8 </head>
     9 <body>
    10     <!-- 提交路径 -->
    11     <form action="${pageContext.request.contextPath}/controller2/form"
    12         method="post" accept-charset="UTF-8">
    13         <table>
    14             <tr>
    15                 <td>用户名:</td>
    16                 <td><input type="text" name="userName"></td>
    17             </tr>
    18             <tr>
    19                 <td>密码:</td>
    20                 <td><input type="password" name="userPwd"></td>
    21             </tr>
    22             <tr>
    23                 <td><input type="submit" value="登陆"></td>
    24             </tr>
    25         </table>
    26     </form>
    27 </body>
    28 </html>

       success页面

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

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

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

      

     1 package com.springmvc.tranfercontroller;
     2 
     3 import org.springframework.stereotype.Controller;
     4 import org.springframework.web.bind.annotation.RequestMapping;
     5 
     6 /**
     7  * 基于注解带参数传输案例
     8  * 
     9  * @author Administrator
    10  *
    11  */
    12 @Controller
    13 @RequestMapping(value = "/controller2")
    14 public class TranferController2 {
    15     // 成功提交跳转对应的方法
    16     @RequestMapping(value = "/form")
    17     public String form(String userName, Integer userPwd) {
    18         System.out.println("用户名:" + userName + "用户密码:" + userPwd);
    19         System.out.println("println login of controller");
    20         return "success";
    21     }
    22 
    23     // 访问页面路径对应的方法
    24     @RequestMapping(value = "/form2")
    25     public String form() {
    26         return "form";
    27     }
    28 }

        2.2通过HttpServletRequest接收

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

     1 @Controller
     2     @RequestMapping("/controller2")
     3     public class TranferController2 {
     4         @RequestMapping("/form")
     5         public String form(HttpServletRequest request) {
     6             String userName=request.getParameter("userName");
     7             String userPwd=request.getParameter("userPwd");
     8             System.out.println("用户名:" + userName + "用户密码:" + userPwd);
     9             System.out.println("println login of controller");
    10             return "success";
    11         }
    12     }

      

    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.测试结果(注意访问路径不同)

  • 相关阅读:
    在ubuntu中安装photoshop cs6
    技术博客链接及相关资料
    VIM中括号的自动补全与删除
    ubuntu,jdk安装成功后,点击eclipse,提示信息A Java RunTime Environment (JRE) or Java Development Kit (JDK)
    系统安装
    css实现上下左右布局
    html+css实现选项卡功能
    jsp出现错误can not find the tag directory /web-inf/tags
    数据库类型空间效率探索(二)
    数据库类型空间效率探索(一)
  • 原文地址:https://www.cnblogs.com/ysource/p/13041821.html
Copyright © 2020-2023  润新知