以下几种方式只有在已搭好的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;

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getUserPwd() {
        return userPwd;
    }

    public void setUserPwd(String userPwd) {
        this.userPwd = userPwd;
    }

}

     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

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-08-31
  • 2021-12-16
猜你喜欢
  • 2021-06-15
  • 2021-12-28
相关资源
相似解决方案