Spring表单标签
在以往开发JSP项目时,通常会使用EL表达式和JSTL标签来完成页面视图的开发工作,其实Spring也提供了自己的一套表单标签库。通过使用Spring标签,可以很容易地将模型数据中的表单对象绑定到HTML表单元素中
标签姓名 | 说明 |
<fm:form/> | 渲染表单元素 |
<fm:input/> | 文本框组件标签 |
<fm:password/> | 密码框组件标签 |
<fm:hidden/> | 隐藏组件标签 |
<fm:textarea/> | 多行文本框组件标签 |
<fm:checkbox/> | 复选框组件标签 |
<fm:select/> | 下拉列表组件标签 |
数据校验
使用Spring MVC框架时,有两种常用的方法校验输入的数据
- 利用Spring框架自带的验证框架
- 利用JSR303实现
现在我们采用JSR303实现服务器端的数据验证
JSR303是Java为Bean数据 合法性校验所提供的标准框架,并通过标准的验证接口对Bean进行验证
maven依赖
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.1.2.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging</artifactId>
<version>3.3.2.Final</version>
</dependency>
<dependency>
<groupId>jakarta.validation</groupId>
<artifactId>jakarta.validation-api</artifactId>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml</groupId>
<artifactId>classmate</artifactId>
<version>1.3.4</version>
</dependency>
使用JSR 303不需要编写验证器,它定义了一套可标注在成员变量、属性方法上的校验注解
约束 | 说明 |
@Null | 被注释的元素必须为null |
@NotNull | 被注释的元素必须不为null |
@AssertTrue | 被注释的元素必须为true |
@AssertFalse | 被注释的元素必须为false |
@Past | 被注释的元素必须是一个过去的日期 |
@Pattern | 被注释的元素必须符合指定的正则表达式 |
使用SpringMVC实现文件上传
maven依赖
<!-- 文件上传依赖-->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.2.2</version>
</dependency>
单文件上传
@PostMapping("/add")
public String add(SysUser sysUser, HttpSession session, HttpServletRequest request, @RequestParam(value = "idPic",required = false)MultipartFile attach){
String idPicPath = null;
String path = request.getSession().getServletContext().getRealPath("statics" + File.separator + "uploadfiles");
logger.info("上传文件路径" + path);
String oIdFileName = attach.getOriginalFilename(); // 原文件名
logger.info("原文件名为:" + oIdFileName);
String suffix = FilenameUtils.getExtension(oIdFileName); // 原文件名后缀
logger.info("原文件后缀为:" + suffix);
int filesize = 5000000;
if (attach.getSize() > filesize){
request.setAttribute("uploadFileError","*上传大小不得超过500KB");
return "sysUser/add";
}else if (
suffix.equalsIgnoreCase("jpg") ||
suffix.equalsIgnoreCase("png") ||
suffix.equalsIgnoreCase("jpeg") ||
suffix.equalsIgnoreCase("pneg")
){
// fileName的值格式如:1539053918588_Personal.jpg
String fileName = System.currentTimeMillis() + RandomUtils.nextInt(1000000) + "_Personal.jpg";
File targetFile = new File(path);
if (!targetFile.exists()){
targetFile.mkdirs();
}
try {
attach.transferTo(new File(targetFile,fileName));
} catch (IOException e) {
e.printStackTrace();
request.setAttribute("uploadFileError","*上传文件失败!");
return "sysUser/add";
}
idPicPath = File.separator + "statics" + File.separator + "uploadfiles" + File.separator + fileName;
}
SysUser create = (SysUser) session.getAttribute("userSession");
sysUser.setCreatedUserId(create.getId());
sysUser.setIdPicPath(idPicPath);
if (sysUserService.add(sysUser)) {
return "redirect:/sys/user/list";
}
return "sysUser/add";
}
多文件上传
@PostMapping("/add")
public String add(SysUser sysUser, HttpSession session, HttpServletRequest request, @RequestParam(value = "attachs",required = false)MultipartFile[] attachs){
String idPicPath = null;
String workPicPath = null;
String errorInfo = null;
boolean flag = true;
String path = request.getSession().getServletContext().getRealPath("statics" + File.separator + "upload");
logger.info("上传文件路径" + path);
for (int i = 0 ; i < attachs.length ; i++){
MultipartFile attach = attachs[i];
if (!attach.isEmpty()){
if (i == 0){
errorInfo = "uploadFileError";
}else if(i == 1){
errorInfo = "uploadWpError";
}
}
String oIdFileName = attach.getOriginalFilename(); // 原文件名
logger.info("原文件名为:" + oIdFileName);
String suffix = FilenameUtils.getExtension(oIdFileName); // 原文件名后缀
logger.info("原文件后缀为:" + suffix);
int filesize = 5000000;
if (attach.getSize() > filesize){
request.setAttribute("uploadFileError","*上传大小不得超过500KB");
return "sysUser/add";
}else if (
suffix.equalsIgnoreCase("jpg") ||
suffix.equalsIgnoreCase("png") ||
suffix.equalsIgnoreCase("jpeg") ||
suffix.equalsIgnoreCase("pneg")
){
// fileName的值格式如:1539053918588_Personal.jpg
String fileName = System.currentTimeMillis() + RandomUtils.nextInt(1000000) + "_Personal.jpg";
File targetFile = new File(path);
if (!targetFile.exists()){
targetFile.mkdirs();
}
// 保存
try {
attach.transferTo(new File(targetFile,fileName));
} catch (IOException e) {
e.printStackTrace();
request.setAttribute("uploadFileError","*上传文件失败!");
flag = false;
}
if (i == 0){
idPicPath = File.separator + "statics" + File.separator + "upload" + File.separator + fileName;
}else if(i == 1){
workPicPath = File.separator + "statics" + File.separator + "upload" + File.separator + fileName;
}
logger.debug("idPicPath:" + idPicPath);
logger.debug("workPicPath:" + workPicPath);
}else{
request.setAttribute(errorInfo,"*上传图片格式不正确");
flag = false;
}
}
if (flag){
SysUser create = (SysUser) session.getAttribute("userSession");
sysUser.setCreatedUserId(create.getId());
sysUser.setIdPicPath(idPicPath);
sysUser.setWorkPicPath(workPicPath);
if (sysUserService.add(sysUser)) {
return "redirect:/sys/user/list";
}
}
return "sysUser/add";
}