2011-07-15 72 views
4

當我使用MarshallingView來封送FileManagement對象的列表(java.util.List)時,出現此錯誤。如果我只將一個對象添加到模型中,則不會發生這種情況。所以它正在處理一個對象而不是集合(List)。Spring MVC中的MarshallingView 3

異常

javax.servlet.ServletException: Model object [[[email protected], [email protected], [email protected], [email protected], [email protected]]] retrieved via key [fileManagements] is not supported by the Marshaller 
    at org.springframework.web.servlet.view.xml.MarshallingView.locateToBeMarshalled(MarshallingView.java:129) 
    at org.springframework.web.servlet.view.xml.MarshallingView.renderMergedOutputModel(MarshallingView.java:98) 
    at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:250) 
    at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1120) 
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:890) 
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:792) 
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:851) 
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:756) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:743) 

FileManagement.java

@XmlRootElement 
public class FileManagement { 

    private Long id; 

    private String code; 
    private String name; 

    public Long getId() { 
     return id; 
    } 

    public void setId(Long id) { 
     this.id = id; 
    } 

    public String getCode() { 
     return code; 
    } 

    public void setCode(String code) { 
     this.code = code; 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

} 

FileManagementService.java

public interface FileManagementService { 

    /** 
    * Find all FileManagements. 
    * @return 
    */ 
    public List<FileManagement> findAll(); 
} 

FileManagementController.java

@Controller 
public class FileManagementController { 

    @RequestMapping(value="/filemanagements", method=RequestMethod.GET) 
    public String list(Model model) { 
     model.addAttribute("fileManagements", fileManagementService.findAll()); 
     return LIST_VIEW; 
    } 

    private static final String LIST_VIEW = "/filemanagements/list" ; 
} 

servletContext.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
         http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
         http://www.springframework.org/schema/context 
         http://www.springframework.org/schema/context/spring-context-3.1.xsd 
         http://www.springframework.org/schema/mvc 
         http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd 
         http://www.springframework.org/schema/oxm 
         http://www.springframework.org/schema/oxm/spring-oxm-3.1.xsd"> 

    <oxm:jaxb2-marshaller id="marshaller"> 
     <oxm:class-to-be-bound 
      name="com.afirme.filemanagement.domain.FileManagement" /> 
    </oxm:jaxb2-marshaller> 

    <context:component-scan 
     base-package="com.afirme.filemanagement.controller" /> 

    <bean id="xmlViewResolver" class="org.springframework.web.servlet.view.XmlViewResolver"/> 

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

    <mvc:annotation-driven/> 

</beans> 

/WEB-INF/views.xml

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:oxm="http://www.springframework.org/schema/oxm" 
    xsi:schemaLocation="http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd 
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 

    <bean name="/filemanagement/list" class="org.springframework.web.servlet.view.xml.MarshallingView"> 
     <property name="marshaller" ref="marshaller"/> 
     <property name="modelKey" value="fileManagements"/> 
    </bean> 

</beans> 

我在做什麼錯?

回答

5

這與1603404的問題相同。它不起作用,因爲Spring的JAXB編組器(Jaxb2Marshaller)期望在該類的@XmlRootElement進行編組。因此可以通過添加一箇中間等級來代表該列表來解決:

@XmlRootElement(name = "files") 
public class FileManagementList { 

    @XmlElement(name = "file") 
    private List<FileManagement> files; 

    public FileManagementList() { 
     this(Collections.<FileManagement>emptyList()); 
    } 

    public FileManagementList(List<FileManagement> files) { 
     this.files = files; 
    } 
}