我准备了一个示例spring mvc应用程序来理解Hibernate Validator
API。
当我运行它时,即使我拥有所有
,视图也不会显示准备好所需文件。
以下是我的档案:
_
@EnableWebMvc
@Configuration
@ComponentScan({"com.*"})
public class SpringConfig extends WebMvcConfigurerAdapter {
public void addHandler(ResourceHandlerRegistry registry)
{
registry.addResourceHandler("/resources/**/").addResourceLocations("/resources/"
);
}
@Bean
public InternalResourceViewResolver viewResolver()
{
InternalResourceViewResolver vr = new InternalResourceViewResolver();
vr.setPrefix("/WEB-INF/view/");
vr.setSuffix(".jsp");
return vr;
}
}
控制器
@RequestMapping(" /形式&#34) 公共类FormController { @RequestMapping(方法= RequestMethod.GET)
public String initForm(Model model) {
Form form = new Form();
model.addAttribute("form", form);
return "form";
}
@RequestMapping(method = RequestMethod.POST)
public String submitForm(@Valid Form form, BindingResult result) {
String returnVal = "successForm";
if(result.hasErrors()) {
returnVal = "form";
}
return returnVal;
}
}
Model
公共类表格{
@Size(min=5, max=10, message="Should be of 5-10 characters")
private String name;
@Min(value = 5, message="Should be atleast of 5 characters")
private String lastname;
@NotNull(message="Cannot be empty")
@Size(min=5, max=10, message="Should be of 5-10 characters")
private String password;
@Pattern(regexp="[0-9]+",message="Wrong Zip")
private String zip;
@Min(value=18,message="Should not be below 18")
private String age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getZip() {
return zip;
}
public void setZip(String zip) {
this.zip = zip;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
JSP
" http://www.w3.org/TR/html4/loose.dtd">
在此处插入标题
<form:form method="POST" commandName="form">
<table>
<tr>
<td>Enter your name:</td>
<td><form:input path="name" /></td>
<td><form:errors path="name" cssStyle="color: #ff0000;"/></td>
</tr>
<tr>
<td>Enter your lastname:</td>
<td><form:input path="lastname" /></td>
<td><form:errors path="lastname" cssStyle="color: #ff0000;" />
</tr>
<tr>
<td>Enter your password:</td>
<td><form:input path="password" /></td>
<td><form:errors path="password" cssStyle="color: #ff0000;" />
</tr>
<tr>
<td>Enter your zip:</td>
<td><form:input path="zip" /></td>
<td><form:errors path="zip" cssStyle="color: #ff0000;" /></td>
</tr>
<tr>
<td>Enter your email:</td>
<td><form:input path="email" /></td>
<td><form:errors path="email" cssStyle="color: #ff0000;" /></td>
</tr>
<tr>
<td>Enter your age:</td>
<td><form:input path="age" /></td>
<td><form:errors path="age" cssStyle="color: #ff0000;" /></td>
</tr>
<tr>
<td><input type="submit" name="submit" value="Submit"></td>
</tr>
<tr>
</table>
</form:form>
pom.xml
的xmlns:=的xsi&#34; HTTP://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation =&#34; http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd&#34;&GT;
4.0.0
org.springframework.samples.service.service
HibernateValidation
0.0.1-SNAPSHOT
战
<properties>
<!-- Generic properties -->
<java.version>1.6</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-
8
<!-- Web -->
<jsp.version>2.2</jsp.version>
<jstl.version>1.2</jstl.version>
<servlet.version>2.5</servlet.version>
<!-- Spring -->
<spring-framework.version>3.2.3.RELEASE</spring-framework.version>
<!-- Hibernate / JPA -->
<hibernate.version>4.2.1.Final</hibernate.version>
<!-- Logging -->
<logback.version>1.0.13</logback.version>
<slf4j.version>1.7.5</slf4j.version>
<!-- Test -->
<junit.version>4.11</junit.version>
</properties>
<dependencies>
<dependency>
javax.el
javax.el-API
2.2.4
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.1.0.Final</version>
</dependency>
org.glassfish.web
javax.el
2.2.4
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.3.10.Final</version>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>3.5.6-Final</version>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-commons-annotations</artifactId>
<version>3.3.0.ga</version>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2.2</version>
<groupId>asm</groupId>
<artifactId>asm-util</artifactId>
<version>3.3.1</version>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0</version>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.3</version>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.2.4.Final</version>
<groupId>javax.transaction</groupId>
<artifactId>jta</artifactId>
<version>1.1</version>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.1.0.Final</version>
<!-- Spring MVC -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring-framework.version}</version>
</dependency>
<!-- Other Web dependencies -->
<dependency>
<groupId>antlr</groupId>
<artifactId>antlr</artifactId>
<version>2.7.7</version>
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>${jstl.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>${servlet.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>${jsp.version}</version>
<scope>provided</scope>
</dependency>
<!-- Spring and Transactions -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring-framework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.5.RELEASE</version>
<!-- Logging with SLF4J & LogBack -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
<scope>runtime</scope>
</dependency>
<!-- Hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.2.4.Final</version>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate.version}</version>
</dependency>
<!-- Test Artifacts -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring-framework.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>