Struts HTML File Tag ( < html:file > )

SHARE & COMMENT :

Struts HTML Tag Library

Struts HTML tag library provides tags which are used to create input forms and HTML user interfaces. The tags in the Struts HTML library form a bridge between a JSP view and the other components of a Web application. Since a dynamic Web application often depends on gathering data from a user, input forms play an important role in the Struts framework. Consequently, the majority of the HTML tags involve HTML forms.

Syntax to use Struts HTML tag library

<%@ taglib prefix="html" uri="http://struts.apache.org/tags-html" %>

< html:file >

< html:file > -renders a file input field.Property should be the corresponding FormFile type.As with the corresponding HTML < input> element, the enclosing form element must specify “POST” for the method attribute, and “multipart/form-data” for the enctype attribute.

Example Code for < html:file >

1.Create an Jsp page and name it as file.jsp.It is the Welcome page for a user.

file.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="html" uri="http://jakarta.apache.org/struts/tags-html" %>
<%@taglib prefix="bean" uri="http://jakarta.apache.org/struts/tags-bean" %>


< html>
    < head>

        < title> HTML File example </title>
    < head>
    < body bgcolor="#DDDDDD">
        < h1 > Struts html:file example </h1>

        < html:form action="/filepath" method="POST" enctype="multipart/form-data">

           < h3> Please Enter File Name < /h3>
		   < h3> < bean:write name="fileform" filter="false" property="error"/> </h3>
		   File Name: < html:file property="file"/> < br> < br>
		   < html:submit value="Submit"/>
            < html:reset value="Reset"/>
        </html:form>
    </body>
</html>

2.Create a Form bean.Form bean is used to hold the properties of the submitted form which is a sub class of ActionForm.Here we have one property to hold in tha form bean “file” of type FormFile which belongs to “org.apache.struts.upload” package to hold the file specified in the file input field.
fileform.java

package com.myapp.struts;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

public class fileform extends org.apache.struts.action.ActionForm {
    private FormFile file;
    private String contenttype;
    private int size;
    private String fname;
    private String error;

    public String getContenttype() {
        return contenttype;
    }

    public void setContenttype(String contenttype) {
        this.contenttype = contenttype;
    }

    public String getError() {
        return error;
    }

    public void setError(String error) {
        this.error = error;
    }

     public FormFile getFile() {
        return file;
    }

    public void setFile(FormFile file) {
        this.file = file;
    }

    public String getFname() {
        return fname;
    }

    public void setFname(String fname) {
        this.fname = fname;
    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }

    public fileform() {
        super();

    }

}

3.Simple Action class formaction.java which is a sub class of Action class used to process the user’s request.In this class we check for null values entered in the file input field and set their respective error message.
fileaction.java

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.ActionMapping;
import org.apache.struts.action.ActionForward;

public class formaction extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private final static String SUCCESS = "success";
     private final static String FAILURE = "failure";

    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        fileform formBean=(fileform)form;
        FormFile myFile=formBean.getFile();
        String fname=myFile.getFileName();
        String ctype=myFile.getContentType();
        int size=myFile.getFileSize();
        if(fname.equals("") &amp;&amp; size==0)
        {
            formBean.setError("&lt; span style='color:red'&gt; Enter any File Name&lt;/span&gt;");
            return mapping.findForward(FAILURE);
        }
        formBean.setContenttype(ctype);
        formBean.setFname(fname);
        formBean.setSize(size);
        return mapping.findForward(SUCCESS);
    }
}

4.Create or modify struts config file struts-config.xml with action mappings.Struts-config file contains the information about the configuration of the struts framework to the application.It contains the action mappings which helps to select Action,ActionForm and other information for specific user’s request’s.

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
          "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">

< struts-config>
    < form-beans>
        < form-bean name="fileform" type="com.myapp.struts.fileform"/>
    </form-beans>

< action-mappings>
        < action name="fileform" path="/filepath" scope="request" type="com.myapp.struts.formaction" validate="false">
        < forward name="success" path="/uploadsuccess.jsp"/>
		< forward name="failure" path="/file.jsp"/>
		</action>
</action-mappings>

	</struts-config>

5.Create another simple Jsp page uploadsuccess.jspwhich is for displaying the output Message.If file input field is empty and by checking size and file name error message is displayed on the same page itself.

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean" %>

< html>
    < head>
        < meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        < title> Struts File Tag Upload success File </title>
    </head>
    < body bgcolor="#DDDDDD">
	 < h1> File Uploaded Successfully...	</h1>
        < h3> Uploaded File Details are:	</h3>
		 < h4> File Name:< span style="color:#0055BB"> < bean:write name="fileform" property="fname" filter="false"/> < /span>< /h4>
        < h4> File Content Type:< span style="color:#0055BB"> < bean:write name="fileform" property="contenttype" filter="false"/> < /span>< /h4>
        < h4> File Size: < span style="color:#0055BB"> < bean:write name="fileform" property="size" filter="false"/> bytes< /span>< /h4>
    </body>
</html>

6.Building and running the application
Output

Access page:http://localhost:8084/file/

Struts Framework Articles

also read:

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Speak Your Mind

*

Close
Please support the site
By clicking any of these buttons you help our site to get better