Skip to content

Commit

Permalink
Merge branch 'bean-validation-plugin'
Browse files Browse the repository at this point in the history
  • Loading branch information
jogep committed May 28, 2015
2 parents 0a71c5c + 3219764 commit 6070b57
Show file tree
Hide file tree
Showing 25 changed files with 303 additions and 125 deletions.
17 changes: 17 additions & 0 deletions apps/showcase/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@
<artifactId>struts2-spring-plugin</artifactId>
</dependency>

<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-bean-validation-plugin</artifactId>
</dependency>

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
Expand Down Expand Up @@ -160,6 +165,18 @@
</exclusions>
</dependency>

<!-- BeanValidation Example -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.1.3.Final</version>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.el</artifactId>
<version>3.0.0</version>
</dependency>

</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

/**
* <!-- START SNIPPET: beanValidatationExample -->
*/
@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;
}
}

/**
* <!-- END SNIPPET: beanValidatationExample -->
*/


8 changes: 8 additions & 0 deletions apps/showcase/src/main/resources/struts-validation.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@
"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

<constant name="struts.beanValidation.providerClass" value="org.hibernate.validator.HibernateValidator"/>
<constant name="struts.beanValidation.ignoreXMLConfiguration" value="true"/>

<package name="bean-validation" extends="struts-bean-validation" namespace="/bean-validation">
<!-- Actions are configured via annotations -->
</package>

<package name="validation" extends="json-default" namespace="/validation">
<action name="index">
<result>/WEB-INF/validation/index.jsp</result>
Expand Down
2 changes: 1 addition & 1 deletion apps/showcase/src/main/resources/struts.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<constant name="struts.custom.i18n.resources" value="globalMessages" />
<constant name="struts.action.extension" value="action,," />

<constant name="struts.convention.package.locators.basePackage" value="org.apache.struts2.showcase.person" />
<constant name="struts.convention.package.locators.basePackage" value="org.apache.struts2.showcase" />
<constant name="struts.convention.result.path" value="/WEB-INF" />

<!-- Necessary for Showcase because default includes org.apache.struts2.* -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<%@taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Struts2 Showcase - Validation - Bean Validation Example</title>
<s:head theme="xhtml"/>
</head>
<body>

<div class="page-header">
<h1>Field Validation Examples</h1>
</div>

<div class="container-fluid">
<div class="row-fluid">
<div class="span12">

<!-- START SNIPPET: beanValidatationExample -->

<h3>All Action Errors Will Appear Here</h3>
<s:actionerror/>
<hr/>

<h3>All Field Errors Will Appear Here</h3>
<s:fielderror/>
<hr/>

<h3>Field Error due to 'Required String Validator Field' Will Appear Here</h3>
<s:fielderror>
<s:param value="%{'requiredStringValidatorField'}"/>
</s:fielderror>
<hr/>

<h3>Field Error due to 'String Length Validator Field' Will Appear Here</h3>
<s:fielderror>
<s:param>stringLengthValidatorField</s:param>
</s:fielderror>
<hr/>

<s:form action="bean-validation-example" namespace="/bean-validation" method="POST" theme="xhtml">
<s:textfield label="Required Validator Field" name="requiredValidatorField"/>
<s:textfield label="Required String Validator Field" name="requiredStringValidatorField"/>
<s:textfield label="Integer Validator Field" name="integerValidatorField"/>
<s:textfield label="Date Validator Field" name="dateValidatorField"/>
<s:textfield label="Email Validator Field" name="emailValidatorField"/>
<s:textfield label="URL Validator Field" name="urlValidatorField"/>
<s:textfield label="String Length Validator Field" name="stringLengthValidatorField"/>
<s:textfield label="Regex Validator Field" name="regexValidatorField"/>
<s:textfield label="Field Expression Validator Field" name="fieldExpressionValidatorField"/>
<s:submit label="Submit" cssClass="btn btn-primary"/>
</s:form>

<!-- END SNIPPET: beanValidatationExample -->
</div>
</div>
</div>
</body>
</html>
26 changes: 24 additions & 2 deletions apps/showcase/src/main/webapp/WEB-INF/decorators/main.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv='Content-Type' content='text/html; charset=UTF-8'/>
<meta name="description" content="Struts2 Showcase for Apache Struts Project">
<meta name="author" content="The Apache Software Foundation">

Expand Down Expand Up @@ -195,7 +194,30 @@
<li><s:a value="/skill/index.html">CRUD</s:a></li>
<li><s:a value="/wait/index.html">Execute &amp; Wait</s:a></li>
<li><s:a value="/token/index.html">Token</s:a></li>
<li><s:a value="/validation/index.action">Validation</s:a></li>
<li class="dropdown-submenu">
<a href="#">Validation</a>
<ul class="dropdown-menu">

<s:url var="quizBasic" namespace="/validation" action="quizBasic" method="input"/>
<s:url var="quizClient" namespace="/validation" action="quizClient" method="input"/>
<s:url var="quizClientCss" namespace="/validation" action="quizClientCss" method="input"/>
<s:url var="fieldValidatorUrl" action="showFieldValidatorsExamples" namespace="/validation"/>
<s:url var="nonFieldValidatorUrl" action="showNonFieldValidatorsExamples" namespace="/validation"/>
<s:url var="visitorValidatorUrl" action="showVisitorValidatorsExamples" namespace="/validation"/>
<s:url var="clientSideValidationUrl" action="clientSideValidationExample" namespace="/validation"/>
<s:url var="storeMessageAcrossRequestExample" namespace="/validation" action="storeErrorsAcrossRequestExample"/>
<s:url var="beanValidationUrl" action="bean-validation" namespace="/bean-validation"/>
<li><s:a href="%{beanValidationUrl}">Bean Validation</s:a></li>
<li><s:a href="%{fieldValidatorUrl}">Field Validators</s:a></li>
<li><s:a href="%{clientSideValidationUrl}">Field Validators with client-side JavaScript</s:a></li>
<li><s:a href="%{nonFieldValidatorUrl}">Non Field Validator</s:a></li>
<li><s:a href="%{storeMessageAcrossRequestExample}">Store across request using MessageStoreInterceptor (Example)</s:a></li>
<li><s:a href="%{quizBasic}">Validation (basic)</s:a></li>
<li><s:a href="%{quizClient}">Validation (client)</s:a></li>
<li><s:a href="%{quizClientCss}">Validation (client using css_xhtml theme)</s:a></li>
<li><s:a href="%{visitorValidatorUrl}">Visitor Validator</s:a></li>
</ul>
</li>
<li><s:url var="url" namespace="/modelDriven" action="modelDriven"/><s:a
href="%{url}">Model Driven</s:a></li>
</ul>
Expand Down
2 changes: 0 additions & 2 deletions apps/showcase/src/main/webapp/WEB-INF/tags/ui/example.vm
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
#sreset("cssClass=btn btn-danger")
#end

#surl ("id=url" "value=index.jsp")
<a href="${url}" class="btn btn-info"><i class="icon icon-arrow-left"></i> Back to index.jsp</a>
</div>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,6 @@
</td>
</tr>
</table>

#surl ("id=url" "value=index.jsp")
#sa("href=${url}")Back to index.jsp#end
</div>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,6 @@
</s:form>

<!-- END SNIPPET: fieldValidatorsExample -->


<s:include value="footer.jsp"/>
</div>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@
</s:form>

<!-- END SNIPPET: fieldValidatorsExample -->

<s:include value="footer.jsp"/>
</div>
</div>
</div>
Expand Down
8 changes: 0 additions & 8 deletions apps/showcase/src/main/webapp/WEB-INF/validation/footer.jsp

This file was deleted.

Loading

0 comments on commit 6070b57

Please sign in to comment.