From 3219764efcbce65d703f829bc37d71ac4b270f3f Mon Sep 17 00:00:00 2001 From: Johannes Geppert Date: Thu, 28 May 2015 20:12:53 +0200 Subject: [PATCH] WW-4505 Add plugin to support bean validation Add bean validation example to the showcase application --- apps/showcase/pom.xml | 17 ++ .../BeanValidationExampleAction.java | 170 ++++++++++++++++++ .../src/main/resources/struts-validation.xml | 8 + apps/showcase/src/main/resources/struts.xml | 2 +- .../bean-validation/bean-validation.jsp | 57 ++++++ .../main/webapp/WEB-INF/decorators/main.jsp | 3 +- .../main/webapp/WEB-INF/tags/ui/example.vm | 2 - .../webapp/WEB-INF/tags/ui/exampleSubmited.vm | 3 - .../clientSideValidationExample.jsp | 3 - .../validation/fieldValidatorsExample.jsp | 2 - .../main/webapp/WEB-INF/validation/footer.jsp | 8 - .../validation/nonFieldValidatorsExample.jsp | 3 - .../webapp/WEB-INF/validation/quiz-basic.jsp | 3 - .../WEB-INF/validation/quiz-client-css.jsp | 3 - .../webapp/WEB-INF/validation/quiz-client.jsp | 2 - .../WEB-INF/validation/quiz-success.jsp | 2 - .../storeErrorsAcrossRequestCancel.jsp | 2 - .../storeErrorsAcrossRequestExample.jsp | 2 - .../validation/storeErrorsAcrossRequestOk.jsp | 2 - .../successClientSideValidationExample.jsp | 2 - .../successFieldValidatorsExample.jsp | 2 - .../successNonFieldValidatorsExample.jsp | 28 ++- .../successVisitorValidatorsExample.jsp | 28 ++- .../validation/visitorValidatorsExample.jsp | 2 - 24 files changed, 281 insertions(+), 75 deletions(-) create mode 100644 apps/showcase/src/main/java/org/apache/struts2/showcase/validation/BeanValidationExampleAction.java create mode 100644 apps/showcase/src/main/webapp/WEB-INF/bean-validation/bean-validation.jsp delete mode 100644 apps/showcase/src/main/webapp/WEB-INF/validation/footer.jsp diff --git a/apps/showcase/pom.xml b/apps/showcase/pom.xml index bf70001df..e1c9b77d5 100644 --- a/apps/showcase/pom.xml +++ b/apps/showcase/pom.xml @@ -86,6 +86,11 @@ struts2-spring-plugin + + org.apache.struts + struts2-bean-validation-plugin + + javax.servlet servlet-api @@ -160,6 +165,18 @@ + + + org.hibernate + hibernate-validator + 5.1.3.Final + + + org.glassfish + javax.el + 3.0.0 + + diff --git a/apps/showcase/src/main/java/org/apache/struts2/showcase/validation/BeanValidationExampleAction.java b/apps/showcase/src/main/java/org/apache/struts2/showcase/validation/BeanValidationExampleAction.java new file mode 100644 index 000000000..b9f2b3575 --- /dev/null +++ b/apps/showcase/src/main/java/org/apache/struts2/showcase/validation/BeanValidationExampleAction.java @@ -0,0 +1,170 @@ +/* + * $Id$ + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.struts2.showcase.validation; + +import com.opensymphony.xwork2.ActionSupport; +import org.apache.struts.beanvalidation.constraints.FieldMatch; +import org.apache.struts2.convention.annotation.Action; +import org.apache.struts2.convention.annotation.Namespace; +import org.apache.struts2.convention.annotation.ParentPackage; +import org.apache.struts2.convention.annotation.Result; +import org.apache.struts2.interceptor.validation.SkipValidation; +import org.hibernate.validator.constraints.Email; +import org.hibernate.validator.constraints.NotBlank; +import org.hibernate.validator.constraints.ScriptAssert; +import org.hibernate.validator.constraints.URL; + +import javax.validation.constraints.*; +import java.sql.Date; + +/** + * + */ +@Namespace("/bean-validation") +@ParentPackage("bean-validation") +@Action(results = { + @Result(name = "input", location = "bean-validation.jsp"), + @Result(name = "success", location = "/WEB-INF/validation/successFieldValidatorsExample.jsp") +}) +@FieldMatch(first = "fieldExpressionValidatorField", second = "requiredValidatorField", message = "requiredValidatorField and fieldExpressionValidatorField are not matching") +@ScriptAssert(lang = "javascript", script = "_this.dateValidatorField != null && _this.dateValidatorField.before(new java.util.Date())", message = "Date need to before now") +public class BeanValidationExampleAction extends ActionSupport { + + @NotNull + private String requiredValidatorField = null; + + @NotBlank + private String requiredStringValidatorField = null; + + @NotNull + @Min(1) + @Max(10) + private Integer integerValidatorField = null; + + @NotNull + private Date dateValidatorField = null; + + @NotNull + @Size(min = 4, max = 64) + @Email + private String emailValidatorField = null; + + @NotNull + @Size(min = 4, max = 64) + @URL + private String urlValidatorField = null; + + @NotNull + @Size(min = 2, max = 4) + private String stringLengthValidatorField = null; + + @Pattern(regexp = "[^<>]+") + private String regexValidatorField = null; + + private String fieldExpressionValidatorField = null; + + @Action(value = "bean-validation", results = { + @Result(name = "success", location = "bean-validation.jsp") + }) + @SkipValidation + public String beanValidation(){ + return SUCCESS; + } + + public Date getDateValidatorField() { + return dateValidatorField; + } + + public void setDateValidatorField(Date dateValidatorField) { + this.dateValidatorField = dateValidatorField; + } + + public String getEmailValidatorField() { + return emailValidatorField; + } + + public void setEmailValidatorField(String emailValidatorField) { + this.emailValidatorField = emailValidatorField; + } + + public Integer getIntegerValidatorField() { + return integerValidatorField; + } + + public void setIntegerValidatorField(Integer integerValidatorField) { + this.integerValidatorField = integerValidatorField; + } + + public String getRegexValidatorField() { + return regexValidatorField; + } + + public void setRegexValidatorField(String regexValidatorField) { + this.regexValidatorField = regexValidatorField; + } + + public String getRequiredStringValidatorField() { + return requiredStringValidatorField; + } + + public void setRequiredStringValidatorField(String requiredStringValidatorField) { + this.requiredStringValidatorField = requiredStringValidatorField; + } + + public String getRequiredValidatorField() { + return requiredValidatorField; + } + + public void setRequiredValidatorField(String requiredValidatorField) { + this.requiredValidatorField = requiredValidatorField; + } + + public String getStringLengthValidatorField() { + return stringLengthValidatorField; + } + + public void setStringLengthValidatorField(String stringLengthValidatorField) { + this.stringLengthValidatorField = stringLengthValidatorField; + } + + public String getFieldExpressionValidatorField() { + return fieldExpressionValidatorField; + } + + public void setFieldExpressionValidatorField( + String fieldExpressionValidatorField) { + this.fieldExpressionValidatorField = fieldExpressionValidatorField; + } + + public String getUrlValidatorField() { + return urlValidatorField; + } + + public void setUrlValidatorField(String urlValidatorField) { + this.urlValidatorField = urlValidatorField; + } +} + +/** + * + */ + + diff --git a/apps/showcase/src/main/resources/struts-validation.xml b/apps/showcase/src/main/resources/struts-validation.xml index ee40dc920..bab906e4c 100755 --- a/apps/showcase/src/main/resources/struts-validation.xml +++ b/apps/showcase/src/main/resources/struts-validation.xml @@ -4,6 +4,14 @@ "http://struts.apache.org/dtds/struts-2.3.dtd"> + + + + + + + + /WEB-INF/validation/index.jsp diff --git a/apps/showcase/src/main/resources/struts.xml b/apps/showcase/src/main/resources/struts.xml index 4761eb383..5a12a9e16 100644 --- a/apps/showcase/src/main/resources/struts.xml +++ b/apps/showcase/src/main/resources/struts.xml @@ -15,7 +15,7 @@ - + diff --git a/apps/showcase/src/main/webapp/WEB-INF/bean-validation/bean-validation.jsp b/apps/showcase/src/main/webapp/WEB-INF/bean-validation/bean-validation.jsp new file mode 100644 index 000000000..101ef2360 --- /dev/null +++ b/apps/showcase/src/main/webapp/WEB-INF/bean-validation/bean-validation.jsp @@ -0,0 +1,57 @@ +<%@taglib prefix="s" uri="/struts-tags" %> + + + Struts2 Showcase - Validation - Bean Validation Example + + + + + + +
+
+
+ + + +

All Action Errors Will Appear Here

+ +
+ +

All Field Errors Will Appear Here

+ +
+ +

Field Error due to 'Required String Validator Field' Will Appear Here

+ + + +
+ +

Field Error due to 'String Length Validator Field' Will Appear Here

+ + stringLengthValidatorField + +
+ + + + + + + + + + + + + + + +
+
+
+ + diff --git a/apps/showcase/src/main/webapp/WEB-INF/decorators/main.jsp b/apps/showcase/src/main/webapp/WEB-INF/decorators/main.jsp index 0c296b1ae..b84cbee85 100644 --- a/apps/showcase/src/main/webapp/WEB-INF/decorators/main.jsp +++ b/apps/showcase/src/main/webapp/WEB-INF/decorators/main.jsp @@ -36,7 +36,6 @@ - @@ -207,6 +206,8 @@ + +
  • Bean Validation
  • Field Validators
  • Field Validators with client-side JavaScript
  • Non Field Validator
  • diff --git a/apps/showcase/src/main/webapp/WEB-INF/tags/ui/example.vm b/apps/showcase/src/main/webapp/WEB-INF/tags/ui/example.vm index 64d6431d7..399c3cf95 100644 --- a/apps/showcase/src/main/webapp/WEB-INF/tags/ui/example.vm +++ b/apps/showcase/src/main/webapp/WEB-INF/tags/ui/example.vm @@ -27,8 +27,6 @@ #sreset("cssClass=btn btn-danger") #end - #surl ("id=url" "value=index.jsp") - Back to index.jsp diff --git a/apps/showcase/src/main/webapp/WEB-INF/tags/ui/exampleSubmited.vm b/apps/showcase/src/main/webapp/WEB-INF/tags/ui/exampleSubmited.vm index 9646a4f53..fc6d9f3c9 100644 --- a/apps/showcase/src/main/webapp/WEB-INF/tags/ui/exampleSubmited.vm +++ b/apps/showcase/src/main/webapp/WEB-INF/tags/ui/exampleSubmited.vm @@ -51,9 +51,6 @@ - - #surl ("id=url" "value=index.jsp") - #sa("href=${url}")Back to index.jsp#end diff --git a/apps/showcase/src/main/webapp/WEB-INF/validation/clientSideValidationExample.jsp b/apps/showcase/src/main/webapp/WEB-INF/validation/clientSideValidationExample.jsp index f199c311b..42103e8aa 100644 --- a/apps/showcase/src/main/webapp/WEB-INF/validation/clientSideValidationExample.jsp +++ b/apps/showcase/src/main/webapp/WEB-INF/validation/clientSideValidationExample.jsp @@ -54,9 +54,6 @@ - - - diff --git a/apps/showcase/src/main/webapp/WEB-INF/validation/fieldValidatorsExample.jsp b/apps/showcase/src/main/webapp/WEB-INF/validation/fieldValidatorsExample.jsp index 13a29f0bd..f9332eb2d 100644 --- a/apps/showcase/src/main/webapp/WEB-INF/validation/fieldValidatorsExample.jsp +++ b/apps/showcase/src/main/webapp/WEB-INF/validation/fieldValidatorsExample.jsp @@ -54,8 +54,6 @@ - - diff --git a/apps/showcase/src/main/webapp/WEB-INF/validation/footer.jsp b/apps/showcase/src/main/webapp/WEB-INF/validation/footer.jsp deleted file mode 100644 index 20fa2ad8b..000000000 --- a/apps/showcase/src/main/webapp/WEB-INF/validation/footer.jsp +++ /dev/null @@ -1,8 +0,0 @@ - <%@taglib prefix="s" uri="/struts-tags" %> - -
    - - - - - Back To Validation Examples diff --git a/apps/showcase/src/main/webapp/WEB-INF/validation/nonFieldValidatorsExample.jsp b/apps/showcase/src/main/webapp/WEB-INF/validation/nonFieldValidatorsExample.jsp index 27c8f8b2b..7860b6642 100644 --- a/apps/showcase/src/main/webapp/WEB-INF/validation/nonFieldValidatorsExample.jsp +++ b/apps/showcase/src/main/webapp/WEB-INF/validation/nonFieldValidatorsExample.jsp @@ -35,9 +35,6 @@ - - - diff --git a/apps/showcase/src/main/webapp/WEB-INF/validation/quiz-basic.jsp b/apps/showcase/src/main/webapp/WEB-INF/validation/quiz-basic.jsp index e7cb1f9da..5ec5aaa53 100644 --- a/apps/showcase/src/main/webapp/WEB-INF/validation/quiz-basic.jsp +++ b/apps/showcase/src/main/webapp/WEB-INF/validation/quiz-basic.jsp @@ -29,9 +29,6 @@ - - - diff --git a/apps/showcase/src/main/webapp/WEB-INF/validation/quiz-client-css.jsp b/apps/showcase/src/main/webapp/WEB-INF/validation/quiz-client-css.jsp index c6758935f..04d104cb3 100644 --- a/apps/showcase/src/main/webapp/WEB-INF/validation/quiz-client-css.jsp +++ b/apps/showcase/src/main/webapp/WEB-INF/validation/quiz-client-css.jsp @@ -24,9 +24,6 @@ - - - diff --git a/apps/showcase/src/main/webapp/WEB-INF/validation/quiz-client.jsp b/apps/showcase/src/main/webapp/WEB-INF/validation/quiz-client.jsp index 27268b897..ceec697de 100644 --- a/apps/showcase/src/main/webapp/WEB-INF/validation/quiz-client.jsp +++ b/apps/showcase/src/main/webapp/WEB-INF/validation/quiz-client.jsp @@ -25,8 +25,6 @@ - - diff --git a/apps/showcase/src/main/webapp/WEB-INF/validation/quiz-success.jsp b/apps/showcase/src/main/webapp/WEB-INF/validation/quiz-success.jsp index 58180bba0..6f3672d5f 100644 --- a/apps/showcase/src/main/webapp/WEB-INF/validation/quiz-success.jsp +++ b/apps/showcase/src/main/webapp/WEB-INF/validation/quiz-success.jsp @@ -18,8 +18,6 @@ Thank you, . Your answer has been submitted as: - - diff --git a/apps/showcase/src/main/webapp/WEB-INF/validation/storeErrorsAcrossRequestCancel.jsp b/apps/showcase/src/main/webapp/WEB-INF/validation/storeErrorsAcrossRequestCancel.jsp index fd9fb55f3..23565a8bd 100644 --- a/apps/showcase/src/main/webapp/WEB-INF/validation/storeErrorsAcrossRequestCancel.jsp +++ b/apps/showcase/src/main/webapp/WEB-INF/validation/storeErrorsAcrossRequestCancel.jsp @@ -21,8 +21,6 @@ Try Again - - diff --git a/apps/showcase/src/main/webapp/WEB-INF/validation/storeErrorsAcrossRequestExample.jsp b/apps/showcase/src/main/webapp/WEB-INF/validation/storeErrorsAcrossRequestExample.jsp index 939e0ace1..7d2e7b413 100644 --- a/apps/showcase/src/main/webapp/WEB-INF/validation/storeErrorsAcrossRequestExample.jsp +++ b/apps/showcase/src/main/webapp/WEB-INF/validation/storeErrorsAcrossRequestExample.jsp @@ -45,8 +45,6 @@ Because of the redirect, the input values are not retained.

    - - diff --git a/apps/showcase/src/main/webapp/WEB-INF/validation/storeErrorsAcrossRequestOk.jsp b/apps/showcase/src/main/webapp/WEB-INF/validation/storeErrorsAcrossRequestOk.jsp index 6c5ddb13e..27d43314a 100644 --- a/apps/showcase/src/main/webapp/WEB-INF/validation/storeErrorsAcrossRequestOk.jsp +++ b/apps/showcase/src/main/webapp/WEB-INF/validation/storeErrorsAcrossRequestOk.jsp @@ -21,8 +21,6 @@ Try Again - - diff --git a/apps/showcase/src/main/webapp/WEB-INF/validation/successClientSideValidationExample.jsp b/apps/showcase/src/main/webapp/WEB-INF/validation/successClientSideValidationExample.jsp index cb56e9c1f..103785e6b 100644 --- a/apps/showcase/src/main/webapp/WEB-INF/validation/successClientSideValidationExample.jsp +++ b/apps/showcase/src/main/webapp/WEB-INF/validation/successClientSideValidationExample.jsp @@ -55,8 +55,6 @@ Field Expression Validator Field: - - diff --git a/apps/showcase/src/main/webapp/WEB-INF/validation/successFieldValidatorsExample.jsp b/apps/showcase/src/main/webapp/WEB-INF/validation/successFieldValidatorsExample.jsp index f795bcc46..2847d2942 100644 --- a/apps/showcase/src/main/webapp/WEB-INF/validation/successFieldValidatorsExample.jsp +++ b/apps/showcase/src/main/webapp/WEB-INF/validation/successFieldValidatorsExample.jsp @@ -52,8 +52,6 @@ Field Expression Validator Field: - - diff --git a/apps/showcase/src/main/webapp/WEB-INF/validation/successNonFieldValidatorsExample.jsp b/apps/showcase/src/main/webapp/WEB-INF/validation/successNonFieldValidatorsExample.jsp index 40333006d..95024f1ee 100644 --- a/apps/showcase/src/main/webapp/WEB-INF/validation/successNonFieldValidatorsExample.jsp +++ b/apps/showcase/src/main/webapp/WEB-INF/validation/successNonFieldValidatorsExample.jsp @@ -23,21 +23,19 @@
    - - - - - - - - - - - - -
    Some Text:
    Some Text Retyped:
    Some Text Retyped Again:
    - - + + Some Text: + + + + Some Text Retyped: + + + + Some Text Retyped Again: + + +
    diff --git a/apps/showcase/src/main/webapp/WEB-INF/validation/successVisitorValidatorsExample.jsp b/apps/showcase/src/main/webapp/WEB-INF/validation/successVisitorValidatorsExample.jsp index 6eecc629a..1e8ad5079 100644 --- a/apps/showcase/src/main/webapp/WEB-INF/validation/successVisitorValidatorsExample.jsp +++ b/apps/showcase/src/main/webapp/WEB-INF/validation/successVisitorValidatorsExample.jsp @@ -23,21 +23,19 @@
    - - - - - - - - - - - - -
    User Name:
    User Age:
    User Birthday:
    - - + + User Name: + + + + User Age: + + + + User Birthday: + + +
    diff --git a/apps/showcase/src/main/webapp/WEB-INF/validation/visitorValidatorsExample.jsp b/apps/showcase/src/main/webapp/WEB-INF/validation/visitorValidatorsExample.jsp index 06ee70e6e..c9db5fdb0 100644 --- a/apps/showcase/src/main/webapp/WEB-INF/validation/visitorValidatorsExample.jsp +++ b/apps/showcase/src/main/webapp/WEB-INF/validation/visitorValidatorsExample.jsp @@ -34,8 +34,6 @@ - -