0

我环顾四周,但无法弄清楚我在尝试获取结果的 xml 视图时错过了什么。

以下是我得到的例外:

javax.servlet.ServletException: Unable to locate object to be marshalled in model: {movies=[com.wickedlynotsmart.imdb.model.Movie@1450f1f, com.wickedlynotsmart.imdb.model.Movie@ac622a, com.wickedlynotsmart.imdb.model.Movie@160c21a, com.wickedlynotsmart.imdb.model.Movie@1677737, com.wickedlynotsmart.imdb.model.Movie@1c3dc66]}
    at org.springframework.web.servlet.view.xml.MarshallingView.renderMergedOutputModel(MarshallingView.java:100)
    at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:250)
    ...
    ...

以下是处理请求时包含的文件:

servlet 应用程序上下文文件

<bean id="jaxbMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
    <property name="classesToBeBound">
        <list>
            <value>com.wickedlynotsmart.imdb.model.Movie</value>
        </list>
    </property>
</bean>

<bean id="movies" class="org.springframework.web.servlet.view.xml.MarshallingView">
    <constructor-arg ref="jaxbMarshaller" />
</bean>

领域对象

@Entity
@XmlRootElement
public class Movie implements Serializable {
    public Movie() {}
    //interesting stuff
}

控制器

@RequestMapping("/movies")
public class MoviesController {
    private static final Log logger = LogFactory.getLog(MoviesController.class);

    @Autowired
    private MovieManagementService movieManagementService;

    @RequestMapping(method=RequestMethod.GET)
    public String findAllMovies(Model model) {
        List<Movie> movies = movieManagementService.getAllMovies();
        model.addAttribute("movies", movies);
        return "movies";
    }   
        //interesting stuff
}

有人可以帮我解决我在这里可能缺少的东西吗?

谢谢。

编辑:我基本上是想查看 BeanNameViewResolver 的运行情况,我已经在配置文件中配置了 BeanNameViewResolver,如下所示:

<bean id="beanNameViewResolver" class="org.springframework.web.servlet.view.BeanNameViewResolver">
    <property name="order" value="2" />
    </bean> 
4

1 回答 1

2

以下更改使事情正常进行:

Movie 类的包装器,以使 JAXB 满意

@XmlRootElement(name="movies")
public class MovieList {

    private List<Movie> movieList;

    public MovieList() {}
    public MovieList(List<Movie> movieList) {
        this.movieList = movieList;
    }

    @XmlElement(name="movie")
    public List<Movie> getMovieList() {
        return movieList;
    }
    public void setMovieList(List<Movie> movieList) {
        this.movieList = movieList;
    }

}

控制器

@RequestMapping(method=RequestMethod.GET)
public String findAllMovies(Model model) throws MovieNotFoundException {
    List<Movie> movieList = movieManagementService.getAllMovies();
    MovieList movies = new MovieList(movieList);
    model.addAttribute("movies", movies);
    return "movies";
}

sevlet 应用程序上下文

<bean id="jaxbMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
    <property name="classesToBeBound">
        <list>
            <value>com.wickedlynotsmart.imdb.model.Movie</value>
            <value>com.wickedlynotsmart.imdb.model.MovieList</value>
        </list>
    </property>
</bean>
于 2013-03-07T16:14:01.693 回答