Spring MVC的标签库——form和input

一 控制器

package org.fkit.controller;

import org.fkit.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

/**
*  HelloWorldController是一个基于注解的控制器,
*  可以同时处理多个请求动作,并且无须实现任何接口。
*  org.springframework.stereotype.Controller注解用于指示该类是一个控制器
*/
@Controller
public class UserController{
     
     @GetMapping(value="/registerForm")
     public String registerForm(Model model) {
         User user = new User("jack","男",28);
         // model中添加属性command,值是user对象
         model.addAttribute("command",user);
         return "registerForm";
     }
     
     @GetMapping(value="/registerForm2")
     public String registerForm2(Model model) {
         User user = new User("jack","男",28);
         // model中添加属性user,值是user对象
         model.addAttribute("user",user);
         return "registerForm2";
     }

}

二 领域模型

package org.fkit.domain;
import java.io.Serializable;

// 域对象,实现序列化接口
public class User implements Serializable{
     
     private static final long serialVersionUID = 1L;
     
     // 私有字段
     private String username;
     private String sex;
     private Integer age;
     
     public User() {
           super();
     }
     public User(String username, String sex, Integer age) {
           super();
           this.username = username;
           this.sex = sex;
           this.age = age;
     }
     public String getUsername() {
           return username;
     }
     public void setUsername(String username) {
           this.username = username;
     }
     public String getSex() {
           return sex;
     }
     public void setSex(String sex) {
           this.sex = sex;
     }
     public Integer getAge() {
           return age;
     }
     public void setAge(Integer age) {
           this.age = age;
     }
     
     @Override
     public String toString() {
           return "User [username=" + username + ", sex=" + sex  + ", age=" + age
                     + "]";
     }
     
     
     
}

三 视图

1 registerForm.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="form"  uri="http://www.springframework.org/tags/form" %>




测试form标签


注册页面

姓名:
性别:
年龄:

2 registerForm2.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="form"  uri="http://www.springframework.org/tags/form" %>




测试form标签


注册页面

姓名:
性别:
年龄:

四 测试

Spring MVC的标签库——form和input_第1张图片

Spring MVC的标签库——form和input_第2张图片

你可能感兴趣的:(Spring,MVC)