form标签和input标签的使用

1.视图

<!DOCTYPE HTML>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" >
<head>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <meta content="text/html;charset=UTF-8"/>
<title>sessionAttributes</title>
</head>
<body>
<h2>registerForm</h2>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<form:form method="post" action="register">
姓名<form:input path="username" />
<br>
性别<form:input path="sex" />
<br>
年龄<form:input path="age" />
</form:form>
</body>
</html>
如果model中存在一个属性名称为command的javaBean,而且该JavaBean拥有属性username,sex,age,则渲染上面的代码时,就会取command得对应属性值给对应标签的属性。
2.模型
package org.fkit.domain;
import java.io.Serializable;
public class User implements Serializable
{
private String username;
private String sex;
private Integer age;
public User()
{
super();
}
public User(
String username,
String sex,
Integer age
)
{
super();
this.setUsername(username);
this.setSex(sex);
this.setAge(age);
}
…

3.控制器

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.RequestMapping;
@Controller
public class UserController
{
@RequestMapping("/registerForm")
public String registerForm(
Model model
)
{
User user = new User("姓名","男",20);
//向model中添加command 值是user对象
model.addAttribute("command", user);
return "registerForm";
}
}

该代码将user设置到Model当中,属性名为command

生成的HTML代码: