Application- 13 : Creating a Struts Application for Upload document into browser by Using DynaValidatorForm
Previous Home Next

In this chapter, you create a new Struts application for the purpose of upload any kind of document( like image, PDF, Txt , doc, and etc. ). DynaValidationForm class extends DynaActionForm and provides basic field validation based on an XML( validation.xml and validator- rules.xml) files. In which, the key passed into the validator is the action element's 'name' attribute from the in struts-config.xml which should match the form element's name attribute in the validation.xml.

Validator-rules.xml file contained in struts-core.jar, and should be referenced in the struts-config.xml under the plug-in element for the ValidatorPlugIn.For each piece of the application, we show you the code and then explain the code. This application design in NetBeans IDE- 6.8 with Struts 1.3.

Requirement for your struts application-

  • At least one view component is required in every Application.
  • A subclass of ActionForm is used to submit the date as application needed.
  • A subclass of Action class which provide specific processing functionality for the application.
  • A web.xml file is required for calling ActionServlet and struts-config.xml.
  • A struts-config.xml file is required for updating and mapping the update.

Let's Start the application, this application provide a good understanding how struts framework works and also understanding the development of Struts applications.

Now, Following the some steps for develop your first application name as FileUpload. FileUpload application contain following page-

  • Three View file name as index.jsp and Dispaly.jsp
  • One DynaActionForm class name as a UploadForm, used for store data as application need.
  • One Action class name as, used for provide specific processing functionality.
  • Two XML file name as web.xml and struts-config.xml file, used for provide control, mapping and validation in application.

Step- 1 : Start your IDE ( NetBeans 6.8 with struts 1.3)

Step- 2 : Open a new project( Crtl+Shift+N), name it , add server( Apache Tomcat 6.0.20 ), then add struts framework 1.3 , and don't forget to add Struts TLDs.

Note:-When you click Finish button, your NetBeans IDE configure everything in place and project open in your IDE.

Step- 3: Open index.jsp page, from your struts application and code it as describe below.

<%-- Document    : index.jsp
Description : First Page of FileUpload_2 Application--%>
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@ taglib uri="" 
prefix="bean" %>
<%@ taglib uri="" 
prefix="html" %>
<%@ taglib uri="" 
prefix="logic" %>
<html:html lang="true">
<meta http-equiv="Content-Type"
content="text/html; charset=UTF-8">
<title><bean:message key="welcome.title"/></title>
    <body style="background-color: white">
<html:form method="post" action="/upload" 
<h1><bean:message key="welcome.heading"/></h1>
<html:errors />
    <table border="0" cellspacing="4" cellpadding="4">
<td><bean:message key="welocome.first" /></td>
<td><html:text property="name" size="30"/></td>
<td><bean:message key="welocome.second" /></td>
<td><html:file property="document" size="30" /></td>
<td align="left"><html:submit 
value=" FileUpload " /></td>

Step- 4: Open Display.jsp page, from your struts application and code it as describe below.

<%-- Document: display.jsp
 Description : Second Page of FileUpload_2 Application--%>
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@ taglib uri=""
prefix="bean" %>
<%@ taglib uri="" 
prefix="html" %>
<html:html lang="true">
<meta http-equiv="Content-Type" 
content="text/html; charset=UTF-8">
   <title>Display page</title>
<body style="background-color: white">
<h1><bean:message key="welcome.dispaly" /></h1>
Name of File: <b> <bean:write name="UploadForm"
property="name" /></b><BR><BR>
Name of Upload File: <b> <bean:write name="UploadForm" 
property="document" /></b><BR><BR>
Size of Upload File: <b>
<%= request.getAttribute("SizeofFile")%> bytes </b>
<img src="<bean:write name="UploadForm"
property="document" />" width="500" height="300"
 alt="<bean:write name="UploadForm"
 property="name" />" /> <BR><BR>
<strong>Only Image file display, but you can esily 
download any file link given below </strong><BR><BR>
<a href="<bean:write name="UploadForm" 
property="document"/>">  Click here to download that file </a>

Step- 5: Open web.xml file place in WEB-INF folder, this file is used to map the ActionServlet( work as backbone of application, every Action, Validation and ActionForm class control by this class) , struts-config.xml file, welcome file and five different TagLibrary.

<!-- Save as a web.xml-->
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
<!-- ActionServlet map here, however you can't find
this class within the project but it work as a backbone
of the project -->
<!-- Your struts-config.xml file map here-->
<!-- Action class map with URL -->
<!-- Welcome file map here: -->
<!-- All the Tag Library which used with in the project -->

Step- 6: Open struts-config.xml file place in WEB-INF folder, this file is used to map Model class, Exception class, Validation class, Action class( Action class further choose View class) and ApplicationResource class.

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation

//DTD Struts Configuration 1.3//EN"
<!-- Used DynaValidatorForm class -->
<form-bean name="UploadForm" 
type="org.apache.struts.validator.DynaValidatorForm" >
<form-property name="name" 
type="java.lang.String" />
<form-property name="document" 
type="org.apache.struts.upload.FormFile" />
<action input="/index.jsp" name="UploadForm" 
path="/upload" scope="request" 
<forward name="success" path="/display.jsp"/>
<controller processorClass="org.apache.struts
<message-resources parameter="com/myapp/struts
<!-- =========== Tiles plugin ============-->
<plug-in className="org.apache.struts.tiles
.TilesPlugin" >
<set-property property="definitions-config"
value="/WEB-INF/tiles-defs.xml" />
<set-property property="moduleAware" value="true" />
<!-- =========== Validator plugin =============== -->
<plug-in className="org.apache.struts

Step- 7: Now introduce Bean class into struts-config.xml file, Since In this application we used DynaValidationForm which extend DynaActionForm so, no need to make another Bean/Form class.

Step- 8: Introduce Action class in to struts-config.xml file, as we define in our first application, that Netbean IDE able to configure your action class in struts-config.xml file.

/* Save as a
 * Action class of FileUpload_3 application */
package com.myapp.struts;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;
import org.apache.struts.validator.DynaValidatorForm;
public class UploadAction extends 
	org.apache.struts.action.Action {
/* forward name="" path="" */
private static final String SUCCESS = "success";
private FileOutputStream outputStream = null;
FormFile formFile = null;
/* This is the action called from the Struts framework.
 * @return */
public ActionForward execute(ActionMapping mapping,
	ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
DynaValidatorForm validatorForm = (DynaValidatorForm)
//Taking property form DynaValidatorForm
String name = (String) validatorForm.get("name");
formFile = (FormFile) validatorForm.get("document");
try {
String path = getServlet().getServletContext()
	.getRealPath("") + "/" + formFile.getFileName();
outputStream = new FileOutputStream(new File(path));
} catch (FileNotFoundException e) {
throw new FileNotFoundException(e.getMessage());
} finally {
if (outputStream != null) {
validatorForm.set("document", formFile);
request.setAttribute("SizeOfFile", formFile.getFileSize());
return mapping.findForward(SUCCESS);

Run your application:

Now your application is complete, so now start run your application. Start your server( which you embedded when configure our struts application), then right click your application and click upon Run option or open your welcome( index.jsp) page and press shift+F6.

Check validation into JSP page

Previous Home Next