Spring MVC 表单处理

在项目目录 src/main/java 的包 com.shiyanlou.entity 下新建类 User.java,包含 id、username、password 和 age 属性,代码如下:

package com.shiyanlou.entity;

import java.io.Serializable;

public class User implements Serializable {

    private static final long serialVersionUID = 1L;
    private Integer id;
    private String username;
    private String password;
    private Integer age;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

}

在包 com.shiyanlou.controller 下新建 Controller 类 UserController.java,具体解释注释已经给出,代码如下:

package com.shiyanlou.controller;

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

import com.shiyanlou.entity.User;

/**
 * UserController 是一个基于注解的控制器
 * 可以同时处理多个请求动作
 */
@Controller
public class UserController {
    /**
     * RequestMapping 用来映射一个请求和请求的方法
     * value = "/register" 表示请求由 register 方法进行处理
     */
    @RequestMapping(value = "/register")
    public String Register(User user, Model model) {  // user:视图层传给控制层的表单对象;model:控制层返回给视图层的对象
        // 在 model 中添加一个名为 "user" 的 user 对象
        model.addAttribute("user", user);
        // 返回一个字符串 "success" 作为视图名称
        return "success";
    }
}

在 webapp 目录下新建一个 JSP 页面命名为 register.jsp,代码如下:

<%@ 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>register page</title>
  </head>
  <body>
    <form action="register" method="post">
      <h5>User registration</h5>
      <p>
        <label>name </label>
        <input type="text" id="username" name="username" tabindex="1" />
      </p>

      <p>
        <label>password </label>
        <input type="text" id="password" name="password" tabindex="2" />
      </p>

      <p>
        <label>age </label>
        <input type="text" id="age" name="age" tabindex="3" />
      </p>
      <p id="buttons">
        <input id="submit" type="submit" tabindex="4" value="register" />
        <input id="reset" type="reset" tabindex="5" value="reset" />
      </p>
    </form>
  </body>
</html>

在 webapp/WEB-INF/views 新建一个 JSP 页面命名为 success.jsp,代码如下:

<%@ 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>success page</title>
  </head>
  <body>
    <h5>login was successful</h5>
    <p>
      name:${requestScope.user.username}<br />
      password:${requestScope.user.password}<br />
      age:${requestScope.user.age}<br />
    </p>
  </body>
</html>

输入 mvn jetty:run 运行程序,点击工具--web 服务按钮,访问 spring mvc 填入数据,点击注册。

结果:

posted @ 2020-11-19 21:02  上杉家主-上杉绘梨衣  阅读(144)  评论(0编辑  收藏  举报