< html:file /> html tag
Previous | Home | Next |
The struts file tag is a render of html input element of type file. It is defaulting to the specified property or specified value of the bean which is associated with our current form.This is nested inside a form tag body when it is only valid.
The ActionForm bean associated with this form must include a statement setting when it is correctly recognize uploaded files. the FormFile property to null in the reset() method.
Example :-<html:form method="Get" enctype="multipart/form-data"> <html:file property="thisFile" > </html:form>The struts file tag have following attributes:-
- accept :- This attributes set of content types that the server you submit to knows how to process. You can be used to limit the set of file options that is made available for selection by the client browser.
- accesskey :- This attributes is used to move focus immediately to this element.
- alt :-This is the alternate text for this element.
- dir :- The direction for weak/neutral text for this element.
- styleId :- This is Identifier to be assigned to this HTML element and renders an 'id' attribute.
- tabindex :- This is tab ascending positive integers order for this element.
- title :- This attributes is the advisory title for this element.
- titleKey :- This attributes is the advisory title for this element basically this is message resources key.
- value :- This is used when value transmitted if this checkbox is checked when the form is submitted. if This is not specified than return 'on' value.
- style :- In this element you can applied CSS style.
- styleClass :- In this element you can applied CSS stylesheet class.
The Following are many other attributes which we can use in file tag :
- disabled
- errorKey
- errorStyle
- errorStyleClass
- errorStyleId
- indexed
- name
- onblur
- onchange
- onclick
- ondblclick
- onfocus
- onkeydown
- onkeypress
- onkeyup
- onmousedown
- onmousemove
- onmouseout
- onmouseover
- onmouseup
- property
Directory Structure of FileTagExample in Struts 1.3 Using MyEclipse IDEindex.jsp<%@taglib uri="http://struts.apache.org/tags-html" prefix="html"%> <%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%> <html> <head> <title><bean:message key="label.title"/></title> </head> <body> <h3><bean:message key="label.header"/></h3> <html:form action="/fileAction" method="POST" enctype="multipart/form-data"> <bean:message key="label.file"/> <html:file property="file" name="fileForm"/> <br/> <br/> <html:submit><bean:message key="label.submit" /></html:submit> <html:reset><bean:message key="label.reset" /></html:reset> </html:form> </body> </html>web.xml<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Maven Struts Examples</display-name> <servlet> <servlet-name>action</servlet-name> <servlet-class> org.apache.struts.action.ActionServlet </servlet-class> <init-param> <param-name>config</param-name> <param-value> /WEB-INF/struts-config.xml </param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app>struts-config.xml<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd"> <struts-config> <form-beans> <form-bean name="fileForm" type="org.r4r.struts.FilesForm"> </form-bean> </form-beans> <global-exceptions /> <global-forwards /> <action-mappings > <action path="/file" type="org.apache.struts.actions.ForwardAction" parameter="/index.jsp"/> <action path="/fileAction" type="org.r4r.struts.FileAction" name="fileForm"> <forward name="success" path="/success.jsp"/> </action> </action-mappings> <message-resources parameter="org.r4r.struts.ApplicationResources" /> </struts-config>FilesForm.javapackage org.r4r.struts; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import org.apache.struts.action.ActionForm; import org.apache.struts.upload.FormFile; @SuppressWarnings("serial") public class FilesForm extends ActionForm { private FormFile file; private String fileText; public FormFile getFile() { return file; } public void setFile(FormFile file) { this.file = file; } public String getFileText() { try { ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); InputStream input = file.getInputStream(); byte[] dataBuffer = new byte[4096]; int numberBytes = 0; while ((numberBytes = input.read(dataBuffer, 0, 4096)) != -1) { byteStream.write(dataBuffer, 0, numberBytes); } fileText = new String(byteStream.toByteArray()); input.close(); } catch (IOException e) { return null; } return fileText; } public void setFileText(String fileText) { this.fileText = fileText; } }FileAction.javapackage org.r4r.struts; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; public class FileAction extends Action { public ActionForward execute(ActionMapping mapping,ActionForm form, HttpServletRequest request,HttpServletResponse response) throws Exception{ @SuppressWarnings("unused") FilesForm paramForm=(FilesForm)form; return mapping.findForward("success"); } }ApplicationResources.properties#label message label.title=Struts html:file tag example label.header=Struts html:file tag example label.file=Select File Name label.submit=Submit label.reset=Resetsuccess.jsp<%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%> <%@taglib uri="http://struts.apache.org/tags-html" prefix="html"%> <html> <head> <title><bean:message key="label.title"/></title> </head> <body> <h3><bean:message key="label.header"/></h3> <h4>OutPut:-</h4> <h5><bean:write name="fileForm" property="fileText" /></h5> </body> </html>Output
Previous | Home | Next |