hooyantsing's Blog

第116次课程_Spring_MVC配置详解

字数统计: 4k阅读时长: 22 min
2020/03/01

源辰76班

第116次课程

2020.03.01

内容

Spring_MVC配置详解

项目路径:

1.@ResponseBody 和 @RequestBody

@ResponseBody:与@Controller()同用,由于@Controller的类返回的是具体的文件,使用该注解后,返回字符串。(回顾:@RestController的类返回字符串)

@RequestBody: 返回Post请求报文中的Body(请求体)内容,因此使用该注解之前请先注解@PostMapping(“…”)。

login.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<%@ page language="java" contentType="text/html;  charset=utf-8"
    pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core"  prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;  charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<c:if test="${!empty msg }">
     <font color="red">${msg}</font>
</c:if>
<form action="signin">
     username:<input name="username"/><br/>
     password:<input name="password"/><br/>
     <input type="submit"/><br/>
</form>
<form action="reg" method="post">
     username:<input name="username"/><br/>
     password:<input name="password"/><br/>
     <input type="submit"/><br/>
</form>
</body>
</html>

UserAction.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package com.yc.springmvc.web.d0301;
import java.util.Map;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import  org.springframework.web.bind.annotation.GetMapping;
import  org.springframework.web.bind.annotation.PostMapping;
import  org.springframework.web.bind.annotation.RequestBody;
import  org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.yc.springmvc.bean.User;
@Controller("UserAction0301")
public class UserAction {
     
     @GetMapping("tosign")
     public String tologin() {
          return "login";
     }
     
     /*
      * 实现数据推送的对象:
      * 1.Model
      * 2.ModelAndView  Model + View
      * 3.Map
      *
      */
     
     //Model
     /*@GetMapping("signin")
     public String login(User user,Model model) {
          if("yc".equals(user.getUsername()) &&  "123".equals(user.getPassword())) {
              user.setEmail("123123123@qq.com");
              user.setPhone("1313501111");
              model.addAttribute("user",user);
              return "success";
          } else {
              model.addAttribute("msg", "用户名或密码错误");
              return "login";
          }
     }*/
     
     //ModelAndView
     /*@GetMapping("signin")
     public ModelAndView login(User user) {
          ModelAndView mav = new ModelAndView("success");
          if("yc".equals(user.getUsername()) &&  "123".equals(user.getPassword())) {
              user.setEmail("123123123@qq.com");
              user.setPhone("1313501111");
              mav.addObject("user",user);
              return mav;
          } else {
              mav.addObject("msg", "用户名或密码错误");
              mav.setViewName("login");
              return mav;
          }
     }*/
     
     //Map
     @GetMapping("sign")
     public String login(User user,Map<String,Object>  map) {
          if("yc".equals(user.getUsername()) &&  "123".equals(user.getPassword())) {
              user.setEmail("123123123@qq.com");
              user.setPhone("1313501111");
              map.put("user",user);
              return "success";
          } else {
              map.put("msg", "用户名或密码错误");
              return "login";
          }
     }
     
     //紫色字:设置响应请求字符集 解决乱码问题
     @PostMapping(path="reg",produces="text/html;charset=utf-8")
     @ResponseBody()
     public String reg(User user,@RequestBody String  reqBody) {
          System.out.println(reqBody);
          return "成功!";
     }
}

2.ModelAttribute

UserAction.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package com.yc.springmvc.web.d0301;
import java.util.Map;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import  org.springframework.web.bind.annotation.GetMapping;
import  org.springframework.web.bind.annotation.ModelAttribute;
import  org.springframework.web.bind.annotation.PostMapping;
import  org.springframework.web.bind.annotation.RequestBody;
import  org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.yc.springmvc.bean.User;
@Controller("UserAction0301")
public class UserAction {
     
     /* @ModelAttribute
      * 1.加在方法上
      * 在所有控制器方法之前执行
      */
     @ModelAttribute
     public String init(Model model) {
          System.out.println("============");
          User user = new User();
          user.setUsername("hooy");
          user.setPassword("123");
          model.addAttribute("user",user);
          /*
           * 该方法返回值自动添加到model中 没有指定model名,那么数据的类型就是名字
           * 类名首字母小写:String -> string / User ->  user
           */
          return "你好";
     }
     
     /* @ModelAttribute("")
      * 2.加在参数上
      * 从model里寻找一个对象名为user的对象,然后传递给形参变量
      */
     @GetMapping("tosign")
     public String tologin(@ModelAttribute("user") User user) {
          System.out.println(user);
          return "login";
     }
 
     //Map
     @GetMapping("sign")
     public String login(User user,Map<String,Object>  map) {
          if("yc".equals(user.getUsername()) &&  "123".equals(user.getPassword())) {
              user.setEmail("123123123@qq.com");
              user.setPhone("1313501111");
              map.put("user",user);
              return "success";
          } else {
              map.put("msg", "用户名或密码错误");
              return "login";
          }
     }
     
     @PostMapping(path="reg",produces="text/html;charset=utf-8")
     @ResponseBody()
     public String reg(User user,@RequestBody String  reqBody) {
          System.out.println(reqBody);
          return "成功!";
     }
}

3.@SessionAttributes() 和 @SessionAttribute()区别  响应重定向

UserAction.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package com.yc.springmvc.web.d0301;
import java.util.Map;
import  org.springframework.beans.factory.annotation.Required;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import  org.springframework.web.bind.annotation.GetMapping;
import  org.springframework.web.bind.annotation.ModelAttribute;
import  org.springframework.web.bind.annotation.PostMapping;
import  org.springframework.web.bind.annotation.RequestBody;
import  org.springframework.web.bind.annotation.ResponseBody;
import  org.springframework.web.bind.annotation.SessionAttribute;
import  org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;
import com.yc.springmvc.bean.User;
@Controller("UserAction0301")
/* @SessionAttributes("")
* 加在类上
* 用于监控Model中数据,如果出现指定名称或类型的数据,那么就将该数据添加到会话中去
*/
@SessionAttributes("loginedUser")
public class UserAction {
     
     /* @ModelAttribute
      * 1.加在方法上
      * 在所有控制器方法之前执行
      * 该方法还可以添加 Model对象
      */
     @ModelAttribute
     public String init(Model model) {
          System.out.println("============");
          User user = new User();
          user.setUsername("hooy");
          user.setPassword("123");
          model.addAttribute("user",user);
          /*
           * 该方法返回值自动添加到model中 没有指定model名,那么数据的类型就是名字
           * 类名首字母小写:String -> string / User ->  user
           */
          return "你好";
     }
     
     /* @ModelAttribute("")
      * 2.加在参数上
      * 从model里寻找一个对象名为user的对象,然后传递给形参变量
      */
     @GetMapping("tosign")
     public String tologin(@ModelAttribute("user") User  user) {
          System.out.println(user);
          return "login";
     }

     //ModelAndView
     @GetMapping("sign")
     public ModelAndView login(User user) {
          /*
           * 响应重定向:
           *        使用 "视图名" 方式默认的是请求转发方式
           *        响应重定向 使用 "redirect: 地址" 跳转
           */

          //请求转发
          //ModelAndView mav = new  ModelAndView("tosuccess");
          //响应重定向
          ModelAndView mav = new  ModelAndView("redirect:tosuccess");
          if("yc".equals(user.getUsername()) &&  "123".equals(user.getPassword())) {
              user.setEmail("123123123@qq.com");
              user.setPhone("1313501111");
              mav.addObject("loginedUser",user);
              return mav;
          } else {
              mav.addObject("msg", "用户名或密码错误");
              mav.setViewName("login");
              return mav;
          }
     }
     
     /* @SessionAttribute
      * 加在方法参数上
      * 用于将会话中的数据取出来, 注入到方法参数中
      */
     @GetMapping("tosuccess")//reuired=false 设置为false,如果session里没有的对象,也能正常访问页面。否则,报错。
     public String  toSuccess(@SessionAttribute(required=false,value="loginedUser") User user) {
          System.out.println("==========toSuccess==========");
          System.out.println(user);
          System.out.println("==========toSuccess==========");
          return "success";
     }
          
     @PostMapping(path="reg",produces="text/html;charset=utf-8")
     @ResponseBody()
     public String reg(User user,@RequestBody String  reqBody) {
          System.out.println(reqBody);
          return "成功!";
     }
}

**4.**springMVC的页面标签

User.java

[Bean]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package com.yc.springmvc.bean;
import java.util.Arrays;
import java.util.Date;
public class User {
     
     private String[] likes;
     
     private String edu;
     
     private String subject;
     
     //---------------------------------------------------------------------------
     
     
     
     
     
     
     
     
     
     
     
     
     
    public String[] getLikes() {
          return likes;
     }
     public void setLikes(String[] likes) {
          this.likes = likes;
     }
     public String getEdu() {
          return edu;
     }
     public void setEdu(String edu) {
          this.edu = edu;
     }
     public String getSubject() {
          return subject;
     }
     public void setSubject(String subject) {
          this.subject = subject;
     }
     private Integer id;
    private String username;
    private String password;
    private String email;
    private String headImg;
    private String phone;
    private Integer status;
    private Date regtime;
    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 == null ? null :  username.trim();
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password == null ? null :  password.trim();
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email == null ? null : email.trim();
    }
    public String getHeadImg() {
        return headImg;
    }
    public void setHeadImg(String headImg) {
        this.headImg = headImg == null ? null :  headImg.trim();
    }
    public String getPhone() {
        return phone;
    }
    public void setPhone(String phone) {
        this.phone = phone == null ? null : phone.trim();
    }
    public Integer getStatus() {
        return status;
    }
    public void setStatus(Integer status) {
        this.status = status;
    }
    public Date getRegtime() {
        return regtime;
    }
    public void setRegtime(Date regtime) {
        this.regtime = regtime;
    }
     @Override
     public String toString() {
          return "User [likes=" + Arrays.toString(likes) +  ", edu=" + edu + ", subject=" + subject + ", id=" + id
                   + ", username=" + username + ",  password=" + password + ", email=" + email + ", headImg="  + headImg
                   + ", phone=" + phone + ", status=" +  status + ", regtime=" + regtime + "]";
     }
    
}

Subject.java

[Bean]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package com.yc.springmvc.bean;
public class Subject {
     private int id;
     private String name;
     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 Subject(int id, String name) {
          super();
          this.id = id;
          this.name = name;
     }
     public Subject() {
          super();
     }
}

reg.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<%@ page language="java" contentType="text/html;  charset=utf-8"
    pageEncoding="utf-8"%>
<%@ taglib uri="http://www.springframework.org/tags/form"  prefix="form" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;  charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<%-- <form action="sign">
     姓名:<input/><br/>
     密码:<input/><br/>
     爱好:
     学历:
     专业:
     <input type="submit" value="登录"/><br/>
</form> --%>
<!-- modelAttribute用于定义从model哪个对象对表单进行填充  -->
<form:form modelAttribute="user">
     姓名:<form:input path="username"/><br/>
     密码:<form:input path="password"/><br/>
     爱好:<form:checkboxes items="${likeItems }"  path="likes"/><br/>
     学历:<form:radiobuttons items="${eduItems }"  path="edu"/><br/>
     专业:<form:select items="${subjectItems }"  path="subject"
                             itemLabel="name"  itemValue="id"></form:select><br/>
     <input type="submit" value="登录"/><br/>
</form:form>
</body>
</html>

UserAction.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package com.yc.springmvc.web.d0301;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import  org.springframework.beans.factory.annotation.Required;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import  org.springframework.web.bind.annotation.GetMapping;
import  org.springframework.web.bind.annotation.ModelAttribute;
import  org.springframework.web.bind.annotation.PostMapping;
import  org.springframework.web.bind.annotation.RequestBody;
import  org.springframework.web.bind.annotation.ResponseBody;
import  org.springframework.web.bind.annotation.SessionAttribute;
import  org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;
import com.yc.springmvc.bean.Subject;
import com.yc.springmvc.bean.User;

@Controller("UserAction0301")
@SessionAttributes("loginedUser")
public class UserAction {
     
     @ModelAttribute
     public String init(Model model) {
          System.out.println("============");
          User user = new User();
          user.setUsername("hooy");
          user.setPassword("123");
          model.addAttribute("user",user);
          return "你好";
     }

     //--------------------------------------------------------------------------------------------
     
     @GetMapping("toreg")
     public String toreg(Model  model,@ModelAttribute("user") User user) {
          user.setLikes(new String[] {"跳舞","手游"});
          user.setEdu("本科");
          user.setSubject("3");
          
          String[] likeItems = {"篮球","跳舞","唱歌","看书","手游"};
          //Array -> List
          List<String> eduItems = Arrays.asList(new  String[] {"初中","高中","专科","本科"});
          List<Subject> subjectItems = new ArrayList<>();
          
          subjectItems.add(new Subject(1,"计算机网络"));
          subjectItems.add(new Subject(2,"计算机应用计算"));
          subjectItems.add(new Subject(3,"财务管理"));
          subjectItems.add(new Subject(4,"市场营销"));
          subjectItems.add(new Subject(5,"企业管理"));
          
          model.addAttribute("likeItems",likeItems);
          model.addAttribute("eduItems",eduItems);
          model.addAttribute("subjectItems",subjectItems);
          
          return "reg";
     }
}

5.表单验证

pom.xml

首先要引入两个Jar包。

1
2
3
4
5
6
7
8
9
10
<dependency>
      <groupId>javax.validation</groupId>
      <artifactId>validation-api</artifactId>
      <version>2.0.0.Final</version>
</dependency>
<dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-validator</artifactId>
      <version>6.1.0.Final</version>
</dependency>

User.java

将注释加在属性上,即对该属性做对应验证。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package com.yc.springmvc.bean;
import java.util.Arrays;
import java.util.Date;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotEmpty;
import org.hibernate.validator.constraints.Length;
public class User {
     
     private String[] likes;
     
     private String edu;
     
     private String subject;
     
     //---------------------------------------------------------------------------
     
     
     
     
     
     
     
     
     
     
     
     
     
    public String[] getLikes() {
          return likes;
     }
     public void setLikes(String[] likes) {
          this.likes = likes;
     }
     public String getEdu() {
          return edu;
     }
     public void setEdu(String edu) {
          this.edu = edu;
     }
     public String getSubject() {
          return subject;
     }
     public void setSubject(String subject) {
          this.subject = subject;
     }
     private Integer id;
     @NotEmpty
     @Length(max=16,min=3,message="用户名长度必须在3~16位之间")
    private String username;
     @NotEmpty
     @Length(max=12,min=6)
    private String password;
     @Email(message="请输入正确的电子邮箱格式")
    private String email;
    private String headImg;
    private String phone;
    private Integer status;
    private Date regtime;
    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 == null ? null :  username.trim();
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password == null ? null :  password.trim();
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email == null ? null : email.trim();
    }
    public String getHeadImg() {
        return headImg;
    }
    public void setHeadImg(String headImg) {
        this.headImg = headImg == null ? null :  headImg.trim();
    }
    public String getPhone() {
        return phone;
    }
    public void setPhone(String phone) {
        this.phone = phone == null ? null : phone.trim();
    }
    public Integer getStatus() {
        return status;
    }
    public void setStatus(Integer status) {
        this.status = status;
    }
    public Date getRegtime() {
        return regtime;
    }
    public void setRegtime(Date regtime) {
        this.regtime = regtime;
    }
     @Override
     public String toString() {
          return "User [likes=" + Arrays.toString(likes) +  ", edu=" + edu + ", subject=" + subject + ", id=" + id
                   + ", username=" + username + ",  password=" + password + ", email=" + email + ", headImg="  + headImg
                   + ", phone=" + phone + ", status=" +  status + ", regtime=" + regtime + "]";
     }
    
}

UserAction.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package com.yc.springmvc.web.d0301;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import javax.validation.Valid;
import  org.springframework.beans.factory.annotation.Required;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.Errors;
import  org.springframework.web.bind.annotation.GetMapping;
import  org.springframework.web.bind.annotation.ModelAttribute;
import  org.springframework.web.bind.annotation.PostMapping;
import  org.springframework.web.bind.annotation.RequestBody;
import  org.springframework.web.bind.annotation.ResponseBody;
import  org.springframework.web.bind.annotation.SessionAttribute;
import  org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;
import com.yc.springmvc.bean.Subject;
import com.yc.springmvc.bean.User;
@Controller("UserAction0301")
/* @SessionAttributes("")
* 加在类上
* 用于监控 Model中数据,如果出现指定名称或类型的数据,那么就将该数据添加到会话中去
*/
@SessionAttributes("loginedUser")
public class UserAction {
     
     /* @ModelAttribute
      * 1.加在方法上
      * 在所有控制器方法之前执行
      * 该方法还可以添加 Model对象
      */
     @ModelAttribute
     public String init(Model model) {
          System.out.println("============");
          User user = new User();
          user.setUsername("hooy");
          user.setPassword("123");
          model.addAttribute("user",user);
          
          String[] likeItems = {"篮球","跳舞","唱歌","看书","手游"};
          List<String> eduItems = Arrays.asList(new  String[] {"初中","高中","专科","本科"});
          List<Subject> subjectItems = new ArrayList<>();
          
          subjectItems.add(new Subject(1,"计算机网络"));
          subjectItems.add(new Subject(2,"计算机应用计算"));
          subjectItems.add(new Subject(3,"财务管理"));
          subjectItems.add(new Subject(4,"市场营销"));
          subjectItems.add(new Subject(5,"企业管理"));
          
          model.addAttribute("likeItems",likeItems);
          model.addAttribute("eduItems",eduItems);
          model.addAttribute("subjectItems",subjectItems);
          /*
           * 该方法返回值自动添加到model中 没有指定model名,那么数据的类型就是名字
           * 类名首字母小写:String -> string / User ->  user
           */
          return "你好";
     }

     //--------------------------------------------------------------------------------------------
     
     @GetMapping("toreg")
     public String toreg(Model  model,@ModelAttribute("user") User user) {
          user.setLikes(new String[] {"跳舞","手游"});
          user.setEdu("本科");
          user.setSubject("3");
          return "reg";
     }
     
     @GetMapping("reg")//@Valid 必须后面紧接着Errors
     public String reg(@Valid User user,Errors error) {
          if(error.hasFieldErrors()) {
              return "reg";
          } else {
              return "success";
          }
     }
}

reg.jsp

前端页面

后端传过来的错误信息所显示的位置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<%@ page language="java" contentType="text/html;  charset=utf-8"
    pageEncoding="utf-8"%>
<%@ taglib uri="http://www.springframework.org/tags/form"  prefix="form" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;  charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<%-- <form action="sign">
     姓名:<input/><br/>
     密码:<input/><br/>
     爱好:
     学历:
     专业:
     <input type="submit" value="登录"/><br/>
</form> --%>
<!-- modelAttribute用于定义从model哪个对象对表单进行填充  -->
<form:form modelAttribute="user"  method="GET"  action="reg">
     姓名:<form:input path="username"/>
     <form:errors path="username"></form:errors>
     <br/>
     密码:<form:input path="password"/>
     <form:errors path="password"></form:errors>
     <br/>
     邮箱:<form:input path="email"/>
     <form:errors path="email"></form:errors>
     <br/>
     爱好:<form:checkboxes items="${likeItems }"  path="likes"/><br/>
     学历:<form:radiobuttons items="${eduItems }"  path="edu"/><br/>
     专业:<form:select items="${subjectItems }"  path="subject"
                             itemLabel="name"  itemValue="id"></form:select><br/>
     <input type="submit" value="登录"/><br/>
</form:form>
</body>
</html>

效果图:

.png

CATALOG