springMvc-05 mvc标签库
1、导入标签库
在Jsp页面的顶部位置导入
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%--jstl--%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%--标签库--%>
<%@ taglib prefix="f" uri="http://www.springframework.org/tags/form" %>
<html>
<head>
<title>测试页面</title>
</head>
<body>
<h3>测试输出</h3>
</body>
</html>
2、标签
2.1、form
渲染<form action=""></form>标签
自动绑定Model对象中的属性到 form 对应的实体对象,默认绑定的属性 key 名为command,在form内部的标签中可以通过path直接引入该对象内的属性。
提供除 GET 和 POST 的以外的 HTTP 请求,使用methodParam属性
常使用commandName来定制Model对象中的属性,若不明确指定,默认值是command
若Model中不存在指定属性,则抛出异常,信息为Neither BindingResult nor plain target object for bean name 'command' available as request attribute

2.2、input

2.3、password

2.4、hidden

2.5、textarea

2.6、checkbox和checkboxes


2.7、radiobutton和radiobuttons


2.8、select、option、options



2.9、errors

path="*"表示显示所有的错误信息
path="abc*"表示 显示abc 属性的错误信息
path="abc.att"表示 显示abc 对象的 att 属性的错误信息
3、例子演示
3.1、模型类
package rui.db.Model;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public class User {
private String loginId;
private String userName;
private String telephone;
private String sex;
private String departmentId;
private List<String> loves;
private String intro;
public String getLoginId() {
return loginId;
}
public void setLoginId(String loginId) {
this.loginId = loginId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getDepartmentId() {
return departmentId;
}
public void setDepartmentId(String departmentId) {
this.departmentId = departmentId;
}
public List<String> getLoves() {
return loves;
}
public void setLoves(List<String> loves) {
this.loves = loves;
}
public String getIntro() {
return intro;
}
public void setIntro(String intro) {
this.intro = intro;
}
}
3.2、辅助方法
package rui.tool;
import java.util.HashMap;
import java.util.Map;
public class listHelper {
public static Map<String,String> getSex()
{
Map<String,String> map = new HashMap<String,String>();
map.put("F","男");
map.put("M","女");
return map;
}
public static Map<String,String> getDept()
{
Map<String,String> map = new HashMap<String,String>();
map.put("D01","计算机");
map.put("D02","外语");
return map;
}
public static Map<String,String> getLove()
{
Map<String,String> map = new HashMap<String,String>();
map.put("足球","足球");
map.put("篮球","篮球");
map.put("羽毛球","羽毛球");
return map;
}
}
3.3、控制器
package rui.web;
import jakarta.validation.Valid;
import org.springframework.boot.context.properties.bind.BindResult;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.Errors;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import rui.db.Model.User;
import javax.servlet.http.HttpServletResponse;
import javax.xml.transform.Source;
import java.util.Arrays;
import java.util.List;
/*测试控制器*/
@Controller
@RequestMapping(value = "/test")
public class TestController {
//用户信息请求
@RequestMapping(value = "register", method = RequestMethod.GET)
public String register(Model model) {
System.out.println("执行TestController==1");
User u = new User();
u.setLoginId("001");
u.setUserName("王");
u.setSex("F");
u.setTelephone("123456789");
u.setDepartmentId("D01");
u.setLoves(Arrays.asList("足球", "篮球"));
model.addAttribute("user", u);
model.addAttribute("sexList", rui.tool.listHelper.getSex());
model.addAttribute("deptList", rui.tool.listHelper.getSex());
model.addAttribute("loveList", rui.tool.listHelper.getLove());
return "/test/register";
}
//用户注册提交
@RequestMapping(value = "register", method = RequestMethod.POST)
public String register(Model model,User user) {
System.out.println("执行TestController==2");
model.addAttribute("user", user);
return "forward:/test/showInfo";
}
//用户信息显示
@RequestMapping(value = "showInfo")
public String showInfo() {
return "/test/showInfo";
}
}
3.4、视图
register.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%--jstl--%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%--标签库--%>
<%@ taglib prefix="f" uri="http://www.springframework.org/tags/form" %>
<html>
<head>
<title>测试页面</title>
</head>
<body>
<h3>测试输出123</h3>
<f:form modelAttribute="user" method="post">
<div>用户账号:<f:input path="loginId" data-id="123" /></div>
<div>用户名:<f:input path="userName"/></div>
<div>用户电话:<f:input path="telephone"/></div>
<div>性别:<f:radiobuttons path="sex" items="${sexList}" /> </div>
<div>部门:<f:select path="departmentId" items="${deptList}" /> </div>
<div>爱好:<f:checkboxes path="loves" items="${loveList}" /> </div>
<input type="submit" value="提交">
</f:form>
</body>
</html>
showInfo.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%--jstl--%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%--标签库--%>
<%@ taglib prefix="f" uri="http://www.springframework.org/tags/form" %>
<html>
<head>
<title>测试页面</title>
</head>
<body>
<h3>测试输出123</h3>
<div>用户账号:${user.loginId}</div>
<div>用户姓名:${user.userName}</div>
<div>用户性别:${user.sex}</div>
</body>
</html>
3.5、运行效果



浙公网安备 33010602011771号