2010-07-02 59 views
1

我有點困惑。它並不是爲我填充錯誤,而是在沒有加載頁面的情況下。<html:errors>不起作用

附上我的代碼:

<?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> 

這是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"> 
<%@ 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> 

這是Action類:

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; 
    } 

} 

這是形式:

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()); 
    } 
} 

所以在執行後,我上傳了一個xls文件,如果在validate方法中進行了驗證,那麼在最後一個文件中應該會失敗。它在裏面,但是當它返回到jsp時,它不顯示任何東西。

請幫忙。

感謝 薩欽

+0

您使用的是什麼版本的Struts? – 2010-07-08 15:52:18

回答

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

你在Struts配置標籤像上面這意味着你必須顯示的消息,它的最佳做法是使用與結尾的.properties名稱的文件;編輯該文件以匹配如下。

errors.header=<UL> 
errors.prefix=<LI> 
errors.suffix=</LI> 
errors.footer=</UL> 

error.empty.filename = File name cannot be empty, please provide valid file name.