spring mvc - Hibernate Form Validation Display Error -
i have prepared sample spring mvc application understand hibernate validator
api.
the view not getting displayed, when run it, though have
required files ready.
the following files :
- list item
_
@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; } }
controller
@requestmapping("/form") public class formcontroller { @requestmapping(method=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
public class form {
@size(min=5, max=10, message="should of 5-10 characters") private string name; @min(value = 5, message="should atleast of 5 characters") private string lastname; @notnull(message="cannot empty") @size(min=5, max=10, message="should of 5-10 characters") private string password; @pattern(regexp="[0-9]+",message="wrong zip") private string zip; @min(value=18,message="should not 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">
insert title here
fill form!
<form:form method="post" commandname="form"> <table> <tr> <td>enter name:</td> <td><form:input path="name" /></td> <td><form:errors path="name" cssstyle="color: #ff0000;"/></td> </tr> <tr> <td>enter lastname:</td> <td><form:input path="lastname" /></td> <td><form:errors path="lastname" cssstyle="color: #ff0000;" />
</tr> <tr> <td>enter password:</td> <td><form:input path="password" /></td> <td><form:errors path="password" cssstyle="color: #ff0000;" />
</tr> <tr> <td>enter zip:</td> <td><form:input path="zip" /></td> <td><form:errors path="zip" cssstyle="color: #ff0000;" /></td> </tr> <tr> <td>enter email:</td> <td><form:input path="email" /></td> <td><form:errors path="email" cssstyle="color: #ff0000;" /></td> </tr> <tr> <td>enter 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="http://www.w3.org/2001/xmlschema-instance"
xsi:schemalocation="http://maven.apache.org/pom/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
org.springframework.samples.service.service
hibernatevalidation
0.0.1-snapshot
war
<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 , 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 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>
Comments
Post a Comment