表单校验:
Jar包:
<dependency>
<groupId>org.hibernategroupId>
<artifactId>hibernate-validatorartifactId>
<version>4.0.1.GAversion>
dependency>
<dependency>
<groupId>org.jboss.logginggroupId>
<artifactId>jboss-loggingartifactId>
<version>3.3.0.Finalversion>
dependency>
<dependency>
<groupId>javax.validationgroupId>
<artifactId>validation-apiartifactId>
<version>1.0.0.GAversion>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-apiartifactId>
<version>1.7.21version>
dependency>
类:
package cn.biaodanjiaoyan;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import javax.validation.Valid;
import static java.awt.SystemColor.info;
/**
* Created by 席洪丽 on 2017/8/30.
*/
@Controller
public class FirstController {
@RequestMapping("/first")
public ModelAndView doFirst(@Valid UserInfo info, BindingResult br) {
ModelAndView mv = new ModelAndView();
mv.setViewName("/welcome.jsp");
int errorCount = br.getErrorCount();
if (errorCount > 0) {
FieldError score = br.getFieldError("score");
FieldError name = br.getFieldError("name");
FieldError phone = br.getFieldError("name");
if (score != null) {
mv.addObject("scoremsg", score.getDefaultMessage());
}
if (name != null) {
mv.addObject("namemsg", score.getDefaultMessage());
}
if (phone != null) {
mv.addObject("phonemsg", score.getDefaultMessage());
}
mv.setViewName("/index.jsp");
}
return mv;
}
}
package cn.biaodanjiaoyan;
import org.hibernate.validator.constraints.NotEmpty;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
public class UserInfo {
@Min(value = 0,message = "成绩最小值为{va}")
@Max(value = 100,message = "成绩最大值为{va}")
private Integer score;
public Integer getScore() {
return score;
}
public void setScore(Integer score) {
this.score = score;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@NotEmpty(message = "手机号不允许为空")
@Pattern(regexp = "^1[3,4,5,6,7,8,9]\\d{9}$",message = "手机号码不正确")
private String phone;
@NotEmpty(message = "用户名不能为空")
@Size(min = 6,message = "名称至少6个字符")
private String name;
}
配置文件:
xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="cn.wenjianshangchuan"/>
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<mvc:annotation-driven/>
beans>
表单:
<h1>数据校验h1>
<form action="/first" method="post">
成绩:<input name="score" /> <span>${scoremsg }span><br/><br/>
姓名:<input name="name"/><span>${namemsg }span><br/><br/>
电话:<input name="phone"/><span>${phonemsg }span><br/><br/>
<input type="submit" value="注册"/>
form>
文件上传:
Jar包:
<dependency>
<groupId>commons-fileuploadgroupId>
<artifactId>commons-fileuploadartifactId>
<version>1.3.1version>
dependency>
<dependency>
<groupId>commons-iogroupId>
<artifactId>commons-ioartifactId>
<version>1.4version>
dependency>
类1:package cn.wenjianshangchuan;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
@Controller
public class FirstController {
@RequestMapping("/first")
public String doFirst(MultipartFile upload, HttpSession session){
String childpath=upload.getOriginalFilename();
String parenPath=session.getServletContext().getRealPath("/upload");
File filepath=new File(parenPath,childpath);
try {
upload.transferTo(filepath);
return "/welcome.jsp";
} catch (IOException e) {
e.printStackTrace();
return "/fileupload.jsp";
}
}
}
类2:
xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="cn.wenjianshangchuan"/>
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<mvc:annotation-driven/>
beans>
表单:<h1>文件上传h1>
<form action="/first" method="post" enctype="multipart/form-data">
文件<input type="file" name="upload"/>
<input type="submit"/>
form>