2011-07-15 9 views
4

MarshallingViewを使用してFileManagementオブジェクトのリスト(java.util.List)を整列化すると、このエラーが発生します。モデルにオブジェクトを1つだけ追加すると、これは起こりません。だからオブジェクトとは動作していますが、コレクション(List)では動作していません。Spring MVC 3のMarshallingView

例外

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; 
    } 
} 
関連する問題