SpringMVC4.3x教程之五SpringMVC表单标签的使用

SpringMVC自己提供了一套form表单标签,来看看使用
这套标签的好处:
1、可以将域中的内容显示到标签上
2、配套的校验
3、支持GET\POST\PUT\DELETE
其实我们用在修改页面,比较好。不足之处就是必须要知道request的属性。
1、
表单
常用属性:
commandName:标记当前表单标签的的内容来自的bean对象—域中
modelAttribute:跟上一样
method:请求方式
enctype:标记类型,一般用于文件上传
2、—-
文本输入框
常用属性:
path:对象中的属性名
3、
密码框
4、

单选
5、

复选
6、


下拉框的使用
7、
文本域
8、
submit提交按钮
9、
隐藏域
10、
文本标签
看使用:

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

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>SpringMVC自带的表单标签title>
head>
<body>
<h1 class="h" style="">h1>

<form:form action="update" method="get" commandName="stu" cssClass="f1" cssStyle="color:red">
<table>
<tr>

<td><form:label path="id">学号form:label><form:hidden path="id"/>td>

<td><form:input path="id"/>td>
tr>
<tr>
<td>姓名td>

<td><form:input path="name"/>td>
tr>
<tr>
<td>性别td>
<td>

<form:radiobutton path="sex" value="女"/><form:radiobutton path="sex" value="男"/>td>
tr>
<tr>

<td>性别集合:td>
<td><form:radiobuttons items="${sexs }" path="sex"/>td>
tr>
<tr>

<td>爱好:td>
<td>
<form:checkbox path="hobby" value="码字"/>写代码
<form:checkbox path="hobby" value="美食"/>吃货
<form:checkbox path="hobby" value="养神"/>睡觉
td>
tr>
<tr>

<td>住址td>
<td>
<form:select path="address">
<form:option value="请选择住址">请选择住址form:option>
<form:option value="杭州">杭州form:option>
<form:options items="${adds }"/>
form:select>td>
tr>
<tr>
<td colspan="2"><form:button >确认修改form:button>td>
tr>
table>
form:form>
body>
html>

要求在请求该页面只有必须在HttpServletRequest中存储key为stu的对象。而且还要准备2个Map集合
Student类,就是为stu赋值

public class Student {

    private int id;
    private String name;
    private String sex;
    private int age;
    private String address;
    private String phone;
    private String[] hobby;

    public String[] getHobby() {
        return hobby;
    }
    public void setHobby(String[] hobby) {
        this.hobby = hobby;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    public String getPhone() {
        return phone;
    }
    public void setPhone(String phone) {
        this.phone = phone;
    }
    public Student(int id, String name, String sex, int age, String address, String phone) {
        super();
        this.id = id;
        this.name = name;
        this.sex = sex;
        this.age = age;
        this.address = address;
        this.phone = phone;
    }
    public Student() {
        super();
    }
    @Override
    public String toString() {
        return "Student [id=" + id + ", name=" + name + ", sex=" + sex + ", age=" + age + ", address=" + address
                + ", phone=" + phone + ", hobby=" + Arrays.toString(hobby) + "]";
    }

}

控制器

@Controller
public class FormController {
    //为表单准备对象
    @ModelAttribute("stu")
    public Student test2(){
        return new Student(1, "小强", "男", 20, "东京", "110");
    }
    //  form:radioButtons准备的数据
    @ModelAttribute("sexs")
    public Map<String, String> test3(){
         Map<String, String> map=new HashMap<>();
         map.put("1", "男");
         map.put("2", "女");
         map.put("3", "未知");
         return map;

    }
    @ModelAttribute("adds")
    public Map<String, String> test4(){
        Map<String, String> map=new LinkedHashMap<>();
        for(int i=1;i<20;i++){
            map.put(i+"", "天津"+i);
        }
        return map;
    }

    @RequestMapping("/springform")
    public String test6(){
        return "springform1";
    }
    @RequestMapping("/update")
    public String test5(Student student,Model model){
        System.out.println(student);
        model.addAttribute("stu", student);
        return "springform1";
    }
}

你可能感兴趣的:(SpringMVC教程)