2012-09-12 41 views
0

我在春天有個初學者,我想知道爲什麼我的ModelAttribute,沒有填充(所有值都爲空的)春天的portlet形式:@ModelAttribute不填充

我想創建一個多從,讓我上傳一個csv文件以及CSV文件的類型。

我的代碼是這樣:

在CSVUpload-portlet.xml中:

<bean class="org.springframework.web.portlet.mvc.annotation.DefaultAnnotationHandlerMapping"/> 
<bean class="org.springframework.web.portlet.mvc.annotation.AnnotationMethodHandlerAdapter"> 

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> 
    <property name="maxUploadSize" value="100000000"></property> 
</bean> 

<bean id="viewResolver" 
    class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
    <property name="viewClass" 
     value="org.springframework.web.servlet.view.JstlView" /> 
    <property name="prefix" value="/WEB-INF/jsp/" /> 
    <property name="suffix" value=".jsp" /> 
</bean> 

CSVUploadPortlet.java(控制器)

*All imports 

@Controller("uploadCsvToDatabase") 
@RequestMapping(value = "VIEW") 
public class CSVUploadPortlet { 
    private static final Logger log = Logger.getLogger(CSVUploadPortlet.class); 
    private static IWBCSVUploadRemote csvupload; 

    @RenderMapping 
    public String viewCSVUploadBase(Model model, RenderRequest request) { 
    try { 

    } catch (Exception e) { 
     e.printStackTrace(); 

     log.info("problem in retrieving the CSV Upload service" + e); 
    } 
    return "csvupload/csvupload_view"; 
    } 

    @ModelAttribute("csvFileUploadVO") 
    public CSVFileUploadVO getCommandObject() 
    { 
    System.out.println("SpringFileController -> getCommandObject -> Building VO"); 
    return new CSVFileUploadVO(); 
    } 

    @ActionMapping(params="action=uploadCsvToDatabase") 
    public void uploadCsvToDatabase(
     @ModelAttribute("csvFileUploadVO") CSVFileUploadVO csvFileUploadVO, BindingResult result, ActionRequest request, ActionResponse response, SessionStatus sessionStatus){ 
    try{ 
     System.out.println("FileType:"+csvFileUploadVO.getFileType()); //This returns null 
     System.out.println("CSVFile Size:"+csvFileUploadVO.getCsvFile().getSize()); //This returns a null-pointer exception 
    } catch (Exception e) { 
     log.info("Problem in retrieving the CSVUpload configuration list " + e); 
     e.printStackTrace(); 
    } 
    } 
} 

CSVFileUploadVO。 java

import org.springframework.web.multipart.commons.CommonsMultipartFile; 

public class CSVFileUploadVO { 
    private String fileType; 
    private CommonsMultipartFile csvFile; 
    private String message; 

    public CSVFileUploadVO() { 
    } 

    public String getFileType() { 
    return fileType; 
    } 

    public void setFileType(String fileType) { 
    this.fileType = fileType; 
    } 

    public String getMessage() { 
      return message; 
    } 

    public void setMessage(String message) { 
      this.message = message; 
    } 

    public CommonsMultipartFile getCsvFile() { 
      return csvFile; 
    } 

    public void setCsvFile(CommonsMultipartFile csvFile) { 
      this.csvFile = csvFile; 
    } 
} 

表JSP

All taglibs imported... 
<portlet:actionURL var="fileUploadURL"> 
    <portlet:param name="action" value="uploadCsvToDatabase" /> 
</portlet:actionURL> 

<form:form method="post" action="${fileUploadURL}" 
    commandName="csvFileUploadVO" enctype="multipart/form-data"> 
    <table> 
     <tbody> 
      <tr> 
       <td><label>Department:</label></td> 
       <td><form:select path="fileType"> 
         <form:option value="BRMAdd" label="BRM Add" /> 
         <form:option value="FOSAdmin" label="FOS Admin" /> 
         <form:option value="FOSRM" label="FOS RM" /> 
         <form:option value="FOSTeam" label="FOS Team" /> 
         <form:option value="ITRelationships" label="IT Relationships" /> 
         <form:option value="HRAttendance" label="HR Attendance" /> 
         <form:option value="iCareCallReport" label="iCare Call Report" /> 
        </form:select></td> 
      </tr> 
      <tr> 
       <td><label>Specify your File:</label></td> 
       <td><form:input path="csvFile" type="file" /></td> 
      </tr> 
      <tr> 
      <tr> 
       <td colspan="100%"><input type="submit" value="Submit" /></td> 
      </tr> 
      <tr> 
       <td colspan="100%">${csvFileUploadVO.message}</td> 
      </tr> 
     </tbody> 
    </table> 
</form:form> 

我知道這好像要你幫忙解決我的問題,但我一直停留在這8小時,每閱讀資源,計算器網站我可以谷歌。但儘管如此,我還是無法找到任何東西。

謝謝你在這方面的幫助。

回答

0

你必須定義的ModelAttribute在你的JSP的形式,這樣的事情:

<form:form method="post" action="${fileUploadURL}" 
modelAttribute="csvFileUploadVO" enctype="multipart/form-data"> 
0

的Liferay 6.0 .5和Spring 3.2.2.RELEASE:

我試過編輯spring類,但是有很多ea解決這個問題。只要檢查你的CommonsPortletMultipartResolver是否已經被初始化。它在spring.xml中聲明的事實並不意味着它被加載。

如果未初始化,請求的文件部分將永遠不會轉換爲CommonsMultipartFile,因爲您的請求不會是Commons Multipart請求。

CommonsPortletMultipartResolver將被org.springframework.web.portlet.DispatcherPortlet生命週期調用,如果它被加載並且spring使用這個類。

+0

類型有更容易的解決這個問題,沒必要覆蓋彈簧類。檢查您的CommonsPortletMultipartResolver是否確實已啓動。如果未初始化,則請求的文件部分永遠不會轉換爲CommonsMultipartFile,因爲您的請求不會是Commons Multipart請求。 – user2436114

0

你multipart解析器必須有ID portletMultipartResolverorg.springframework.web.portlet.multipart.CommonsPortletMultipartResolver

<bean id="portletMultipartResolver" class="org.springframework.web.portlet.multipart.CommonsPortletMultipartResolver"> 
</bean>