javax.servlet.ServletException:无法解析名为“dispatcher”的servlet中名为“home”的视图

时间:2014-10-30 14:59:09

标签: java jsp spring-mvc tiles

运行网络应用时获取javax.servlet.ServletException: Could not resolve view with name 'home' in servlet with name 'dispatcher'。对我来说@RequestMapping,tile配置和映射看起来不错但仍然出错。

这是WebAppInitializr.java

public class WebAppInitializer implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {

        AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
        rootContext.register(RootConfig.class, MessageSourceConfig.class);

        servletContext.addListener(new ContextLoaderListener(rootContext));

        AnnotationConfigWebApplicationContext dispatcherServlet = new AnnotationConfigWebApplicationContext();
        dispatcherServlet.register(WebMvcConfig.class);

        ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", new DispatcherServlet(dispatcherServlet));
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("/");
    }

}

WebAppConfig

@EnableWebMvc
@ComponentScan(basePackages = { "com.examples" })
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {

    private static final String TILES_VIEW_RESOLVER_DEFINITION = "/WEB-INF/tiles/tiles_configuration.xml";

    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/assets/**").addResourceLocations("/assets/").setCachePeriod(31556926);
    }

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    @Bean
    public TilesViewResolver tilesViewResolver() {
        TilesViewResolver tilesViewResolver = new TilesViewResolver();
        tilesViewResolver.setOrder(2);
        return tilesViewResolver;
    }

    @Bean
    public UrlBasedViewResolver viewResolver() {
        UrlBasedViewResolver viewResolver = new UrlBasedViewResolver();
        viewResolver.setViewClass(TilesView.class);
        return viewResolver;
    }

    @Bean
    public TilesConfigurer tilesConfigurer() {
        TilesConfigurer tilesConfigurer = new TilesConfigurer();
        tilesConfigurer.setDefinitions(new String[] { TILES_VIEW_RESOLVER_DEFINITION });
        tilesConfigurer.setCheckRefresh(true);
        return tilesConfigurer;
    }

}

我在控制器中的请求映射是: @RequestMapping(value = "/", method = RequestMethod.GET)

瓷砖定义:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE tiles-definitions PUBLIC
        "-//Apache Software Foundation//DTD Tiles Configuration 3.0//EN"
        "http://tiles.apache.org/dtds/tiles-config_3_0.dtd">

<tiles-definitions>

    <definition name="defaultTemplate" template="/WEB-INF/views/templates/baseLayout.jsp">
        <put-attribute name="meta" value="/WEB-INF/views/templates/meta.jsp" />
        <put-attribute name="navbar" value="/WEB-INF/views/templates/navbar.jsp" />
        <put-attribute name="body" value="" />
        <put-attribute name="footer" value="/WEB-INF/views/templates/footer.jsp" />
    </definition>

    <definition name="home" extends="defaultTemplate">
        <put-attribute name="title" value="Home" />
        <put-attribute name="body" value="/WEB-INF/views/home.jsp" />
    </definition>

</tiles-definitions>

任何有助于解决此问题的帮助都将受到高度赞赏。

1 个答案:

答案 0 :(得分:0)

在您的磁贴定义文件中,您放置了title属性,但是您没有放置它的baseLayout.jsp文件。您需要在 baseLayout.jsp 文件的<title>元素中添加以下代码行

<title><tiles:getAsString name="title"/></title>