diff --git a/apps/showcase/src/main/java/org/apache/struts2/showcase/validation/AjaxFormSubmitAction.java b/apps/showcase/src/main/java/org/apache/struts2/showcase/validation/AjaxFormSubmitAction.java new file mode 100644 index 000000000..b179370dd --- /dev/null +++ b/apps/showcase/src/main/java/org/apache/struts2/showcase/validation/AjaxFormSubmitAction.java @@ -0,0 +1,152 @@ +package org.apache.struts2.showcase.validation; + +import java.sql.Date; + +import com.opensymphony.xwork2.validator.annotations.DateRangeFieldValidator; +import com.opensymphony.xwork2.validator.annotations.EmailValidator; +import com.opensymphony.xwork2.validator.annotations.FieldExpressionValidator; +import com.opensymphony.xwork2.validator.annotations.IntRangeFieldValidator; +import com.opensymphony.xwork2.validator.annotations.RegexFieldValidator; +import com.opensymphony.xwork2.validator.annotations.RequiredFieldValidator; +import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator; +import com.opensymphony.xwork2.validator.annotations.StringLengthFieldValidator; +import com.opensymphony.xwork2.validator.annotations.UrlValidator; + +/** + * + */ +/** + * Example Action that shows how forms can be validated and submitted via AJAX + * only. Form-submit-and-page-reload functionality of browsers is not used for + * this action. + *

Some things to note: + *

+ *

+ * + */ +public class AjaxFormSubmitAction extends AbstractValidationActionSupport { + + private String requiredValidatorField = null; + private String requiredStringValidatorField = null; + private Integer integerValidatorField = null; + private Date dateValidatorField = null; + private String emailValidatorField = null; + private String urlValidatorField = null; + private String stringLengthValidatorField = null; + private String regexValidatorField = null; + private String fieldExpressionValidatorField = null; + + @Override + public void validate() { + if (hasFieldErrors()) { + addActionError("Errors present!"); + } + } + + public Date getDateValidatorField() { + return dateValidatorField; + } + + @DateRangeFieldValidator( + min="01/01/1990", + max="01/01/2000", + message="must be a min 01-01-1990 max 01-01-2000 if supplied") + public void setDateValidatorField(Date dateValidatorField) { + this.dateValidatorField = dateValidatorField; + } + + public String getEmailValidatorField() { + return emailValidatorField; + } + + @EmailValidator(message="must be a valid email if supplied") + public void setEmailValidatorField(String emailValidatorField) { + this.emailValidatorField = emailValidatorField; + } + + public Integer getIntegerValidatorField() { + return integerValidatorField; + } + + @IntRangeFieldValidator(min="1", max="10", message="must be integer min 1 max 10 if supplied") + public void setIntegerValidatorField(Integer integerValidatorField) { + this.integerValidatorField = integerValidatorField; + } + + public String getRegexValidatorField() { + return regexValidatorField; + } + + @RegexFieldValidator( + regex="[^<>]+", + message="regexValidatorField must match a regexp (.*\\.txt) if specified") + public void setRegexValidatorField(String regexValidatorField) { + this.regexValidatorField = regexValidatorField; + } + + public String getRequiredStringValidatorField() { + return requiredStringValidatorField; + } + + @RequiredStringValidator(trim=true, message="required and must be string") + public void setRequiredStringValidatorField(String requiredStringValidatorField) { + this.requiredStringValidatorField = requiredStringValidatorField; + } + + public String getRequiredValidatorField() { + return requiredValidatorField; + } + + @RequiredFieldValidator(message="required") + public void setRequiredValidatorField(String requiredValidatorField) { + this.requiredValidatorField = requiredValidatorField; + } + + public String getStringLengthValidatorField() { + return stringLengthValidatorField; + } + + @StringLengthFieldValidator( + minLength="2", + maxLength="4", + trim=true, + message="must be a String of a specific greater than 1 less than 5 if specified") + public void setStringLengthValidatorField(String stringLengthValidatorField) { + this.stringLengthValidatorField = stringLengthValidatorField; + } + + public String getFieldExpressionValidatorField() { + return fieldExpressionValidatorField; + } + + @FieldExpressionValidator( + expression = "(fieldExpressionValidatorField == requiredValidatorField)", + message = "must be the same as the Required Validator Field if specified") + public void setFieldExpressionValidatorField( + String fieldExpressionValidatorField) { + this.fieldExpressionValidatorField = fieldExpressionValidatorField; + } + + public String getUrlValidatorField() { + return urlValidatorField; + } + + @UrlValidator(message="must be a valid url if supplied") + public void setUrlValidatorField(String urlValidatorField) { + this.urlValidatorField = urlValidatorField; + } +} + +/** + * + */ + + diff --git a/apps/showcase/src/main/java/org/apache/struts2/showcase/validation/AjaxFormSubmitSuccessAction.java b/apps/showcase/src/main/java/org/apache/struts2/showcase/validation/AjaxFormSubmitSuccessAction.java new file mode 100644 index 000000000..6eca3fc68 --- /dev/null +++ b/apps/showcase/src/main/java/org/apache/struts2/showcase/validation/AjaxFormSubmitSuccessAction.java @@ -0,0 +1,7 @@ +package org.apache.struts2.showcase.validation; + +public class AjaxFormSubmitSuccessAction { + public String execute() { + return "success"; + } +} diff --git a/apps/showcase/src/main/resources/struts-validation.xml b/apps/showcase/src/main/resources/struts-validation.xml index ee40dc920..fc5921042 100755 --- a/apps/showcase/src/main/resources/struts-validation.xml +++ b/apps/showcase/src/main/resources/struts-validation.xml @@ -29,6 +29,19 @@ quiz-ajax.jsp quiz-success.jsp + + + + + + + /WEB-INF/validation/ajaxFormSubmit.jsp + ajaxFormSubmitSuccess + + + /WEB-INF/validation/ajaxFormSubmitSuccess.jsp + + diff --git a/apps/showcase/src/main/resources/template/ajaxErrorContainers/actionerror.ftl b/apps/showcase/src/main/resources/template/ajaxErrorContainers/actionerror.ftl new file mode 100644 index 000000000..3f7d8ab52 --- /dev/null +++ b/apps/showcase/src/main/resources/template/ajaxErrorContainers/actionerror.ftl @@ -0,0 +1,46 @@ +<#-- +/* + * $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. + */ +--> +<#-- + Make sure element is always present. To be filled later via JS. +--> + +<#if parameters.id??> + id="${parameters.id?html}"<#rt/> + +<#if parameters.cssClass??> + class="${parameters.cssClass?html}"<#rt/> +<#else> + class="errorMessage"<#rt/> + +<#if parameters.cssStyle??> + style="${parameters.cssStyle?html}"<#rt/> + +> +<#if (actionErrors?? && actionErrors?size > 0)> + <#list actionErrors as error> + <#if error??> +
  • <#if parameters.escape>${error!?html}<#else>${error!}<#rt/>
  • <#rt/> + + + + \ No newline at end of file diff --git a/apps/showcase/src/main/resources/template/ajaxErrorContainers/controlfooter.ftl b/apps/showcase/src/main/resources/template/ajaxErrorContainers/controlfooter.ftl new file mode 100644 index 000000000..7f95111f3 --- /dev/null +++ b/apps/showcase/src/main/resources/template/ajaxErrorContainers/controlfooter.ftl @@ -0,0 +1,39 @@ +<#-- +/* + * $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. + */ +--> +${parameters.after!}<#t/> + <#lt/> + +<#if (parameters.errorposition!"top") == 'bottom'> +<#assign hasFieldErrors = parameters.name?? && fieldErrors?? && fieldErrors[parameters.name]??/> +<#if hasFieldErrors> + + <#rt/> + <#if hasFieldErrors> + <#list fieldErrors[parameters.name] as error> +
    ${error?html}
    <#t/> + + + <#lt/> + + + diff --git a/apps/showcase/src/main/resources/template/ajaxErrorContainers/controlheader-core.ftl b/apps/showcase/src/main/resources/template/ajaxErrorContainers/controlheader-core.ftl new file mode 100644 index 000000000..c07b867b0 --- /dev/null +++ b/apps/showcase/src/main/resources/template/ajaxErrorContainers/controlheader-core.ftl @@ -0,0 +1,80 @@ +<#-- +/* + * $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. + */ +--> +<#-- + Always include elements to show errors. They may be filled later via AJAX. +--> +<#assign hasFieldErrors = parameters.name?? && fieldErrors?? && fieldErrors[parameters.name]??/> +<#if (parameters.errorposition!"top") == 'top'> + + <#rt/> + <#if hasFieldErrors> + <#list fieldErrors[parameters.name] as error> +
    ${error?html}
    <#t/> + + + <#lt/> + + +<#if !parameters.labelposition?? && (parameters.form.labelposition)??> +<#assign labelpos = parameters.form.labelposition/> +<#elseif parameters.labelposition??> +<#assign labelpos = parameters.labelposition/> + +<#-- + if the label position is top, + then give the label it's own row in the table +--> + +<#if (labelpos!"") == 'top'> + <#rt/> +<#else> + <#rt/> + +<#if parameters.label??> + <#t/> + + <#lt/> +<#-- add the extra row --> +<#if (labelpos!"") == 'top'> + + + diff --git a/apps/showcase/src/main/resources/template/ajaxErrorContainers/theme.properties b/apps/showcase/src/main/resources/template/ajaxErrorContainers/theme.properties new file mode 100644 index 000000000..81346c707 --- /dev/null +++ b/apps/showcase/src/main/resources/template/ajaxErrorContainers/theme.properties @@ -0,0 +1,21 @@ +# +# $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. +# +parent = xhtml 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 1ca02cc78..684a0f1f2 100644 --- a/apps/showcase/src/main/webapp/WEB-INF/decorators/main.jsp +++ b/apps/showcase/src/main/webapp/WEB-INF/decorators/main.jsp @@ -246,7 +246,6 @@
  • Interactive Demo
  • -