Search

Using WildCards in ActionMapping Tutorial

In this tutorial you will learn see how to reduce the number of action mappings by using wildcard characters. Here is an example of struts configuration file using wildcard character.
01.<?xml version="1.0" encoding="ISO-8859-1" ?>
02. 
03.<!DOCTYPE struts-config PUBLIC
04."-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
06. 
07.<struts-config>
08. 
09.<form-beans>
10.<form-bean name="SampleReportForm"type="com.vaannila.reports.SampleReportForm"/>
11.</form-beans>
12. 
13.<action-mappings>
14.<action path="/*Action" type="com.vaannila.reports.{1}Action" name="{1}Form">
15.<forward name="success" path="/{1}.jsp" />
16.</action>
17.</action-mappings>
18. 
19.</struts-config>
Here the value of the path attribute contains a wildcard character. The value that matches the asterick(*) in the request URL will be substituted instead of '{1}' in the other attributes.
Let's see with an example. In the index.jsp page the request URL is "/SampleReportAction". So according to the configuration the "SampleReport" matches the asterick, so it will be substituted instead of {1}. For this requset URL the Action class will be SampleReportAction and the ActionFormwill be SampleReportForm and when the ActionForward value is success the user will be forwarded toSampleReport.jsp page.
The index.jsp page contains a Generate Report button, which when clicked forwards the request to the SampleReportAction URL, then invokes the SampleReportAction class and displays theSampleReport.jsp page back to the user.
01.<%@taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
02.<html>
03.<head>
04.<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
05.<title>Insert title here</title>
06.</head>
07.<body>
08.<html:form action="/SampleReportAction">
09.<html:submit value="Generate Report" />
10.</html:form>
11.</body>
12.</html>
Here is the sample report.


Struts File Upload Tutorial

In this tutorial we will see how to allow the user to upload a file using Struts. In order to allow the user to upload a file, we need to set the encoding type of html:form tag to "multipart/form-data" and specify the HTTP method as "post". The html:file tag enables the user to browse and select a file.
1.<html:form action="fileUploadAction" method="post" enctype="multipart/form-data">
2.File : <html:file property="file" />
3. 
4.<html:submit />
5.</html:form>
The property file in the FileUploadForm is of type FormFile (org.apache.struts.upload.FormFile). We will check whether the file uploaded by the user satisfies the following conditions in the FileUploadForm's validate() method. 
·         Should be an Excel File.
·         File size should not exceed 20kb.
The validate() method of the FileUploadForm contains the following code.
01.public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
02.ActionErrors errors = new ActionErrors();
03.if (file.getFileSize() == 0) {
04.errors.add("file"new ActionMessage("error.file.required"));
05.else if (!file.getContentType().equals("application/vnd.ms-excel")) {
06.errors.add("file"new ActionMessage("error.file.type"));
07.}
08./**
09.* If the file size is greater than 20kb.
10.*/
11.else if (file.getFileSize() > 20480) {
12.errors.add("file"new ActionMessage("error.file.size"));
13.}
14.return errors;
15.}
The getFileSize() method returns the size of the file. The getContentType() method returns the content type of the file selected by the user.
We save the file uploaded by the user in the server. To do this we first get the file uploaded by the user using the getFile() method. The getFile() mehtod returns a FormFile object. Using the FormFile object we can get details regarding the file like file name and file data.
The FileUploadAction contains the following code.
01.public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throwsException {
02.FileUploadForm uploadForm = (FileUploadForm) form;
03.FileOutputStream outputStream = null;
04.FormFile formFile = null;
05.try {
06.formFile = uploadForm.getFile();
07.String path = getServlet().getServletContext().getRealPath("")+"/"+
08.formFile.getFileName();
09.outputStream = new FileOutputStream(new File(path));
10.outputStream.write(formFile.getFileData());
11.}
12.finally {
13.if (outputStream != null) {
14.outputStream.close();
15.}
16.}
17.uploadForm.setMessage("The file "+formFile.getFileName()+" is uploaded successfully.");
18.return mapping.findForward(SUCCESS);
19.}
The getFileData() method returns the entire file as a byte[]. This method should be used only when the file size is small, incase of large files its better to use getInputStream() method.

On executing the example the following page will be displayed to the user. 


When the user submits the form without selecting any file the following message is dispalyed to the user.


When the user selects a file other than an Excel file then the following message is dispalyed to the user.


When the user selects a file that is greater than 20kb then the following message is dispalyed to the user.


When the user selects an excel file that is less than 20kb, then the user is forwarded to the success page.



·         Download the example and copy it inside the webapps directory of the Tomcat sever.
·         Run the example using the following url "http://localhost:8080/Example23/".
·         Select an excel file to upload.
·         The excel file will be saved inside the Example directory in the server.
·         For example, when I uploaded the temp.xls file the file got stored in the following location "C:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\Example23\temp.xls".