zoukankan      html  css  js  c++  java
  • SpringMVC获取页面表单参数的几种方式

         以下几种方式只有在已搭好的SpringMVC环境中,才能执行成功!

         首先,写一个登陆页面和一个Bean类

    复制代码
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>

    <!DOCTYPE html>

    <html>

    <head>

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

    <title>yyx博客后台登录</title>
    </head>

    <body>

    <form action="${pageContext.request.contextPath}/user/login.do"
    method
    ="post">

    <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 colspan="2"><input
    type="submit" value="登陆"></td>

    </tr>

    </table>

    </form>

    </body>

    </html>

    复制代码
    复制代码
    package com.yyx.model;
    

    public class User {

    private String userName;

    private String userPwd;

    </span><span style="color: #0000ff">public</span><span style="color: #000000"> String getUserName() {</br>
        </span><span style="color: #0000ff">return</span><span style="color: #000000"> userName;</br>
    }</br></br>
    
    </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span><span style="color: #000000"> setUserName(String userName) {</br>
        </span><span style="color: #0000ff">this</span>.userName =<span style="color: #000000"> userName;</br>
    }</br></br>
    
    </span><span style="color: #0000ff">public</span><span style="color: #000000"> String getUserPwd() {</br>
        </span><span style="color: #0000ff">return</span><span style="color: #000000"> userPwd;</br>
    }</br></br>
    
    </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span><span style="color: #000000"> setUserPwd(String userPwd) {</br>
        </span><span style="color: #0000ff">this</span>.userPwd =<span style="color: #000000"> userPwd;</br>
    }</br>
    

    }

    复制代码

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

    package com.yyx.controller;

    import org.springframework.stereotype.Controller;

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

    @Controller

    @RequestMapping("/user")

    public class UserController {

    @RequestMapping(
    "/login")

    public String loginByUser(String userName, String userPwd) {

    System.out.println(
    "userName is:" + userName);

    System.out.println(
    "userPwd is:" + userPwd);

    return "success";

    }

    }

    View Code

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

         2.通过HttpServletRequest接收

    package com.yyx.controller;

    import javax.servlet.http.HttpServletRequest;

    import org.springframework.stereotype.Controller;

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

    @Controller

    @RequestMapping("/user")

    public class UserController {

    @RequestMapping(
    "/login")

    public String loginByUser(HttpServletRequest request) {

    String userName
    =request.getParameter("userName");

    String userPwd
    =request.getParameter("userPwd");

    System.out.println(
    "userName is:" + userName);

    System.out.println(
    "userPwd is:" + userPwd);

    return "success";

    }

    }

    View Code

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

         3.通过一个bean来接收

    复制代码
    package com.yyx.controller;

    import org.springframework.stereotype.Controller;

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

    import com.yyx.model.User;

    @Controller

    @RequestMapping("/user")

    public class UserController {

    @RequestMapping(
    "/login")

    public String loginByUser(User user) {

    System.out.println(
    "userName is:" + user.getUserName());

    System.out.println(
    "userPwd is:" + user.getUserPwd());

    return "success";

    }

    }

    复制代码

    登陆页面的name="userName",name="userPwd"必须和Bean类的属性名称相同

         4.通过json数据接收

    复制代码
    <%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding
    ="UTF-8"%>

    <!DOCTYPE html>

    <html>

    <head>

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

    <script type="text/javascript"

    src
    ="${pageContext.request.contextPath}/static/jquery-3.1.1.min.js"></script>

    <title>yyx博客后台登录</title>
    <script type="text/javascript">

    $(document).ready(
    function() {

    $(
    "#button_submit").click(function() {

    var name = $("#userName").val();

    var pwd = $("#userPwd").val();

    var user = {

    userName : name,

    userPwd : pwd

    };
    //拼装成json格式

    $.ajax({

    type :
    "POST",

    url :
    "${pageContext.request.contextPath}/user/login.do",
    data : user,

    success :
    function(data) {
    alert(
    "成功");

    },

    error :
    function(e) {

    alert(
    "出错:" + e);

    }

    });

    });

    });

    </script>

    </head>

    <body>

    <form>

    <table>

    <tr>

    <td>用户名:</td>

    <td><input type="text"
    id="userName"></td>

    </tr>

    <tr>

    <td>密码:</td>

    <td><input type="password" id="userPwd"></td>

    </tr>

    <tr>

    <td colspan="2"><input type="button" id="button_submit"

    value
    ="提交"></td>

    </tr>

    </table>

    </form>

    </body>

    </html>

    复制代码
    package com.yyx.controller;

    import org.springframework.stereotype.Controller;

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

    import com.yyx.model.User;

    @Controller

    @RequestMapping("/user")

    public class UserController {

    @RequestMapping(
    "/login")

    public String loginByUser(User user) {

    System.out.println(
    "userName is:" + user.getUserName());

    System.out.println(
    "userPwd is:" + user.getUserPwd());

    return "success";

    }

    }

    View Code

    var user={userName:name,userPwd:pwd};//拼装成json格式 语句中userName,userPwd必须和Bean类中属性名称相同

  • 相关阅读:
    log4j 日志分级处理
    http接口调用,传递json格式带双引号问题
    测试输出方法执行时间
    关于 propertychange 兼容性问题
    表结构的修改
    固定table表头
    tomcat 的log4j配置问题
    ie 导出不行,不兼容问题,或只出现后缀文件无法识别
    Spring3.x错误----java.lang.ClassNotFoundException:org.aspectj.weaver.reflect.ReflectionWorld$ReflectionWorldException
    Spring3.x错误----java.lang.ClassNotFoundException:org.aopalliance.inter.MethodInterceptor
  • 原文地址:https://www.cnblogs.com/jpfss/p/8757607.html
Copyright © 2011-2022 走看看