tags:

views:

29

answers:

0

Hello,

I am little stuck with . It is not populating the errors for me, rather it is loading the page without .

Attaching my code:

 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
                               "struts-config_1_2.dtd">
<struts-config>
  <display-name>CSV File Upload</display-name>
  <description>CSV File Upload</description>

  <form-beans>
    <form-bean name="FileUploadForm"
               type="example.struts.FileUploadForm">
    </form-bean>
  </form-beans>

  <action-mappings>
    <action path="/uploadfile"
              input="/jsp/index.jsp"
              name="FileUploadForm"
              validate="true"   
              scope="request"             
              type="example.struts.FileUploadAction">
          <forward name="failure" path="/jsp/error.jsp" />
          <forward name="success" path="/jsp/uploadsuccess.jsp" />
      </action>
  </action-mappings>

 <message-resources parameter="resources.MessageResources" />

</struts-config>

This is index.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Home</title>

</head>
<body>
<h2>File upload</h2>
<html:errors />

<html:form action="uploadfile.do" enctype="multipart/form-data" method="POST">

   <html:file property="file" />
   <br/>
   <html:submit value="Upload" />
</html:form>

</body>
</html>

This is the Action class:

package example.struts;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.upload.FormFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.InputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.StringTokenizer;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

/**
 * 
 * @author Sean C. Sullivan
 *
 * 
 * 
 */
public class FileUploadAction extends Action
{
    public ActionForward execute(ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) throws Exception 
    {
        System.out.println("Hello");
        FileUploadForm uploadForm = (FileUploadForm) form;

        FormFile file = uploadForm.getFile();   

        ActionForward forward = null;       

        try
        {               
            CommonUtility utility = new Utility();
            utility.parseCSV(file);
            forward = mapping.findForward(Forward.KEY_SUCCESS);
        }
        finally
        {           
            file.destroy();
        }

        return forward;
    }

}

This is the form:

package example.struts;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.upload.FormFile;
import javax.servlet.http.HttpServletRequest;

/**
 * 
 * @author Sean C. Sullivan
 * 
 */
public class FileUploadForm extends ActionForm
{
    private FormFile file;

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

    public FormFile getFile()
    {
        return this.file;
    }

    public void reset(ActionMapping mapping, HttpServletRequest request)
    {
        this.file = null;
    }

    public ActionErrors validate(ActionMapping mapping, HttpServletRequest request)
    {
        ActionErrors errors = new ActionErrors();

        if (this.getFile() == null)
        {
            errors.add("file", new ActionMessage("error.file.missing"));
        }
        else if (this.getFile().getFileName() == null)
        {
            errors.add("file", new ActionMessage("error.empty.filename"));
        }
        else if (this.getFile().getFileName().length() < 1)
        {
            errors.add("file", new ActionMessage("error.empty.filename"));
        }
        else if (this.getFile().getFileSize() < 1)
        {
            errors.add("file", new ActionMessage("error.file.size"));
        }
        else if (!this.getFile().getFileName().endsWith(".csv"))
        {           
            errors.add("file", new ActionMessage("error.file.type"));           
        }       
        return errors;
    }

    public String toString()
    {
        return "file=" + String.valueOf(this.getFile());
    }
}

So after executing i am uploading a xls file which should fail in last else if case of validation in the validate method. it's going inside that but when it is returning to the jsp, it doesnt show anything.

Please help.

Thanks Sachin