有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Spring MVC 3中的java MarshallingView

当我使用MarshallingView封送文件管理对象的列表(java.util.list)时,我遇到了这个错误。如果只向模型添加一个对象,则不会发生这种情况。因此,它使用的是对象,而不是集合(列表)

异常

javax.servlet.ServletException: Model object [[com.afirme.filemanagement.domain.FileManagement@69c, com.afirme.filemanagement.domain.FileManagement@65f, com.afirme.filemanagement.domain.FileManagement@661, com.afirme.filemanagement.domain.FileManagement@69d, com.afirme.filemanagement.domain.FileManagement@662]] 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)

文件管理。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;
    }

}

文件管理服务。java

public interface FileManagementService {

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

文件管理控制器。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" ;
}

服务上下文。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>

我做错了什么


共 (1) 个答案

  1. # 1 楼答案

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