2011-07-15 3 views
4

Je reçois cette erreur lorsque j'utilise MarshallingView pour marshaler ma liste (java.util.List) d'objets FileManagement. Cela ne se produit pas si je viens d'ajouter un seul objet au modèle. Donc, cela fonctionne avec un objet mais pas avec la collection (Liste).MarshallingView dans Spring MVC 3

Exception:

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> 

Qu'est-ce que je fais mal ?

Répondre

5

Ceci est le même problème que 1603404. Cela ne fonctionne pas car le marshaller JAXB de Spring (Jaxb2Marshaller) attend un @XmlRootElement sur la classe pour marshaler. Ainsi, il peut être résolu en ajoutant une classe intermédiaire pour représenter la liste:

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