Author: niallp
Date: Tue Nov 8 21:14:23 2005
New Revision: 331955
URL: http://svn.apache.org/viewcvs?rev=331955&view=rev
Log:
Add validation to File Upload example and display request parameters to show
that they are handled correctly.
Added:
struts/apps/trunk/examples/src/webapp/WEB-INF/upload/validation.xml (with
props)
Modified:
struts/apps/trunk/examples/src/java/org/apache/struts/webapp/upload/UploadForm.java
struts/apps/trunk/examples/src/java/org/apache/struts/webapp/upload/UploadResources.properties
struts/apps/trunk/examples/src/webapp/WEB-INF/upload/struts-config.xml
struts/apps/trunk/examples/src/webapp/upload/display.jsp
struts/apps/trunk/examples/src/webapp/upload/upload.jsp
Modified:
struts/apps/trunk/examples/src/java/org/apache/struts/webapp/upload/UploadForm.java
URL:
http://svn.apache.org/viewcvs/struts/apps/trunk/examples/src/java/org/apache/struts/webapp/upload/UploadForm.java?rev=331955&r1=331954&r2=331955&view=diff
==============================================================================
---
struts/apps/trunk/examples/src/java/org/apache/struts/webapp/upload/UploadForm.java
(original)
+++
struts/apps/trunk/examples/src/java/org/apache/struts/webapp/upload/UploadForm.java
Tue Nov 8 21:14:23 2005
@@ -1,7 +1,7 @@
/*
* $Id$
*
- * Copyright 1999-2004 The Apache Software Foundation.
+ * Copyright 1999-2005 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -20,7 +20,11 @@
import javax.servlet.http.HttpServletRequest;
-import org.apache.struts.action.*;
+import org.apache.struts.action.ActionMapping;
+import org.apache.struts.action.ActionMessage;
+import org.apache.struts.action.ActionMessages;
+import org.apache.struts.action.ActionErrors;
+import org.apache.struts.validator.ValidatorForm;
import org.apache.struts.upload.FormFile;
import org.apache.struts.upload.MultipartRequestHandler;
@@ -33,7 +37,7 @@
*
* @version $Rev$ $Date$
*/
-public class UploadForm extends ActionForm {
+public class UploadForm extends ValidatorForm {
/**
* The value of the text the user has sent as form data
@@ -142,14 +146,17 @@
ActionMapping mapping,
HttpServletRequest request) {
- ActionErrors errors = null;
+ ActionErrors errors = super.validate(mapping, request);
+
//has the maximum length been exceeded?
Boolean maxLengthExceeded =
(Boolean) request.getAttribute(
MultipartRequestHandler.ATTRIBUTE_MAX_LENGTH_EXCEEDED);
if ((maxLengthExceeded != null) && (maxLengthExceeded.booleanValue()))
{
- errors = new ActionErrors();
+ if (errors == null) {
+ errors = new ActionErrors();
+ }
errors.add(
ActionMessages.GLOBAL_MESSAGE ,
new ActionMessage("maxLengthExceeded"));
Modified:
struts/apps/trunk/examples/src/java/org/apache/struts/webapp/upload/UploadResources.properties
URL:
http://svn.apache.org/viewcvs/struts/apps/trunk/examples/src/java/org/apache/struts/webapp/upload/UploadResources.properties?rev=331955&r1=331954&r2=331955&view=diff
==============================================================================
---
struts/apps/trunk/examples/src/java/org/apache/struts/webapp/upload/UploadResources.properties
(original)
+++
struts/apps/trunk/examples/src/java/org/apache/struts/webapp/upload/UploadResources.properties
Tue Nov 8 21:14:23 2005
@@ -1,2 +1,5 @@
maxLengthExceeded=The maximum upload length has been exceeded by the client.
maxLengthExplanation=Note that the maximum allowed size of an uploaded file
for this application is two megabytes. See the
"/WEB-INF/upload/struts-config.xml" file for this application to change it.
+errors.required={0} is required.
+errors.minlength={0} can not be less than {1} characters.
+
Modified: struts/apps/trunk/examples/src/webapp/WEB-INF/upload/struts-config.xml
URL:
http://svn.apache.org/viewcvs/struts/apps/trunk/examples/src/webapp/WEB-INF/upload/struts-config.xml?rev=331955&r1=331954&r2=331955&view=diff
==============================================================================
--- struts/apps/trunk/examples/src/webapp/WEB-INF/upload/struts-config.xml
(original)
+++ struts/apps/trunk/examples/src/webapp/WEB-INF/upload/struts-config.xml Tue
Nov 8 21:14:23 2005
@@ -7,16 +7,32 @@
<form-bean name="uploadForm"
type="org.apache.struts.webapp.upload.UploadForm" />
</form-beans>
<action-mappings>
+
<action path="/upload" forward="/upload.jsp" />
+
<!-- Upload Action -->
- <action path="/upload-submit"
type="org.apache.struts.webapp.upload.UploadAction" name="uploadForm"
scope="request" input="input">
+ <action path="/upload-submit"
+ type="org.apache.struts.webapp.upload.UploadAction"
+ name="uploadForm"
+ scope="request"
+ validate="true"
+ input="input">
<forward name="input" path="/upload.jsp" />
<forward name="display" path="/display.jsp" />
</action>
+
</action-mappings>
+
<!-- This is where you set the maximum file size for your file uploads.
-1 is an unlimited size. If you
don't specify this here, it defaults to -1. -->
<controller maxFileSize="2M" inputForward="true" />
+
<message-resources
parameter="org.apache.struts.webapp.upload.UploadResources"/>
+
+ <plug-in className="org.apache.struts.validator.ValidatorPlugIn">
+ <set-property property="pathnames"
value="/WEB-INF/validator-rules.xml,/WEB-INF/upload/validation.xml" />
+ <set-property property="stopOnFirstError" value="false" />
+ </plug-in>
+
</struts-config>
Added: struts/apps/trunk/examples/src/webapp/WEB-INF/upload/validation.xml
URL:
http://svn.apache.org/viewcvs/struts/apps/trunk/examples/src/webapp/WEB-INF/upload/validation.xml?rev=331955&view=auto
==============================================================================
--- struts/apps/trunk/examples/src/webapp/WEB-INF/upload/validation.xml (added)
+++ struts/apps/trunk/examples/src/webapp/WEB-INF/upload/validation.xml Tue Nov
8 21:14:23 2005
@@ -0,0 +1,32 @@
+<?xml version="1.0" encoding="iso-8859-1"?>
+<!DOCTYPE form-validation PUBLIC
+ "-//Apache Software Foundation//DTD Commons Validator Rules
Configuration 1.2.0//EN"
+ "http://jakarta.apache.org/commons/dtds/validator_1_2_0.dtd">
+<form-validation>
+
+ <formset>
+
+ <form name="uploadForm">
+
+ <field property="theText" depends="required,minlength">
+ <arg key="The Text" resource="false" position="0"/>
+ <arg name="minlength" key="${var:minlength}" resource="false"
position="1"/>
+ <var>
+ <var-name>minlength</var-name>
+ <var-value>5</var-value>
+ </var>
+ </field>
+
+ <field property="filePath" depends="validwhen">
+ <arg key="File Path" resource="false" position="0" />
+ <var>
+ <var-name>test</var-name>
+ <var-value>((writeFile == "false") or (*this* != null))</var-value>
+ </var>
+ </field>
+
+ </form>
+
+ </formset>
+
+</form-validation>
Propchange: struts/apps/trunk/examples/src/webapp/WEB-INF/upload/validation.xml
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: struts/apps/trunk/examples/src/webapp/WEB-INF/upload/validation.xml
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Modified: struts/apps/trunk/examples/src/webapp/upload/display.jsp
URL:
http://svn.apache.org/viewcvs/struts/apps/trunk/examples/src/webapp/upload/display.jsp?rev=331955&r1=331954&r2=331955&view=diff
==============================================================================
--- struts/apps/trunk/examples/src/webapp/upload/display.jsp (original)
+++ struts/apps/trunk/examples/src/webapp/upload/display.jsp Tue Nov 8
21:14:23 2005
@@ -21,3 +21,15 @@
<%= request.getAttribute("data") %>
</pre>
<hr />
+
+<hr />
+<h3>Request Parameters</h3>
+
+ <p>Display the request parameter values to show that the multipart request
+ retains them after a forward.</p>
+
+ <b>The Text:</b> <%= request.getParameter("theText") %></br>
+ <b>Write File:</b> <%= request.getParameter("writeFile") %></br>
+ <b>File Path:</b> <%= request.getParameter("filePath") %></br>
+
+ <hr />
Modified: struts/apps/trunk/examples/src/webapp/upload/upload.jsp
URL:
http://svn.apache.org/viewcvs/struts/apps/trunk/examples/src/webapp/upload/upload.jsp?rev=331955&r1=331954&r2=331955&view=diff
==============================================================================
--- struts/apps/trunk/examples/src/webapp/upload/upload.jsp (original)
+++ struts/apps/trunk/examples/src/webapp/upload/upload.jsp Tue Nov 8 21:14:23
2005
@@ -21,16 +21,29 @@
-->
<html:form action="upload-submit.do?queryParam=Successful"
enctype="multipart/form-data">
<p>Please enter some text, just to demonstrate the handling of text
elements as opposed to file elements: <br />
- <html:text property="theText" /></p>
+ <html:text property="theText" errorStyle="background-color: yellow"/></p>
<p>Please select the file that you would like to upload: <br />
- <html:file property="theFile" /></p>
+ <html:file property="theFile" errorStyle="background-color: yellow" /></p>
<p>If you would rather write this file to another file, please check here:
<br />
<html:checkbox property="writeFile" /></p>
<p>If you checked the box to write to a file, please specify the file path
here: <br />
- <html:text property="filePath" /></p>
+ <html:text property="filePath" errorStyle="background-color: yellow"/></p>
<p>
<html:submit />
</p>
</html:form>
+
+ <hr />
+ <h3>Request Parameters</h3>
+
+ <p>Display the request parameter values to show that the multipart request
+ retains them in the event of a validation error.</p>
+
+ <b>The Text:</b> <%= request.getParameter("theText") %></br>
+ <b>Write File:</b> <%= request.getParameter("writeFile") %></br>
+ <b>File Path:</b> <%= request.getParameter("filePath") %></br>
+
+ <hr />
+
</body>
</html>
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]