不渲染Spring + Tiles 3 JSP

时间:2015-12-11 17:50:23

标签: java spring spring-mvc apache-tiles tiles-3

我的Spring Boot App在Eclipse中运行@SpringApplication时充当魅力。

但是作为WAR部署到tomcat7中,我的JSP不会被渲染。

而是显示JSP的路径 (例如:views / monitor_task.jsp)

日志文件中没有错误

Spring Java Config

@Configuration
@EnableWebMvc
@EnableTransactionManagement
@ComponentScan("fr.myapp")
public class WebAppConfig extends WebMvcConfigurerAdapter {

    @Bean
    public UrlBasedViewResolver setupViewResolver() {
        UrlBasedViewResolver resolver = new UrlBasedViewResolver();
        resolver.setPrefix("views/");
        resolver.setSuffix(".jsp");
        resolver.setViewClass(JstlView.class);
        return resolver;
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/css/**").addResourceLocations("/css/");
        registry.addResourceHandler("/js/**").addResourceLocations("/js/");
        registry.addResourceHandler("/fonts/**").addResourceLocations("/fonts/");
    }

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

    @Bean
    public TilesConfigurer tilesConfigurer() {
        TilesConfigurer tilesConfigurer = new TilesConfigurer();
        tilesConfigurer.setDefinitions(new String[] { "tiles-def.xml" });
        tilesConfigurer.setCheckRefresh(true);
        return tilesConfigurer;
    }
}

瓦片-def.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!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="templateMain" template="views/layout.jsp">
        <put-attribute name="title" value="" />
        <put-attribute name="body" value="" />
        <put-list-attribute name="importCSS">
            <add-attribute value="css/bootstrap-Main.min.css" />
            <add-attribute value="css/dataTables.bootstrap.css" />
            <add-attribute value="css/main.css"/>
        </put-list-attribute>
        <put-list-attribute name="importJS">
            <!-- TODO Move datatable imports to required pages -->
            <add-attribute value="js/jquery.js" />
            <add-attribute value="js/jquery.dataTables.js" />
            <add-attribute value="js/bootstrap.min.js" />
            <add-attribute value="js/dataTables.bootstrap.js" />
            <add-attribute value="js/dataTables.bootstrapPagination.js" />
            <add-attribute value="js/jquery.json-2.4.min.js"/>
            <add-attribute value="js/common.js"/>
        </put-list-attribute>
    </definition>

    <definition name="templateMainLogout" template="views/layout_logout.jsp">
        <put-attribute name="title" value="" />
        <put-attribute name="body" value="" />
        <put-list-attribute name="importCSS">
            <add-attribute value="css/bootstrap-Main.min.css" />
            <add-attribute value="css/dataTables.bootstrap.css" />
            <add-attribute value="css/main.css"/>
        </put-list-attribute>
        <put-list-attribute name="importJS">
            <add-attribute value="js/jquery.js" />
            <add-attribute value="js/bootstrap.min.js" />
        </put-list-attribute>
    </definition>

    <definition name="messageLogoutPage" extends="templateMainLogout">
        <put-attribute name="title" value="Main" />
        <put-attribute name="templatePath" value="views/logout_message.jsp" />
    </definition>
    <definition name="messageLogout" template="views/logout_message.jsp" />

    <definition name="monitorTaskPage" extends="templateMain">
        <put-attribute name="title" value="Main" />
        <put-attribute name="templatePath" value="views/monitor_task.jsp" />
    </definition>
    <definition name="monitorTask" template="views/monitor_task.jsp" />

    <definition name="createTaskPage" extends="templateMain">
        <put-attribute name="title" value="Main" />
        <put-attribute name="templatePath" value="views/create_task.jsp" />
    </definition>
    <definition name="createTask" template="views/create_task.jsp" />

    <definition name="generalSettingsPage" extends="templateMain">
        <put-attribute name="title" value="Main" />
        <put-attribute name="templatePath" value="views/general_settings.jsp" />
    </definition>
    <definition name="generalSettings" template="views/general_settings.jsp" />

    <definition name="remoteDataSettings" extends="templateMain">
        <put-attribute name="title" value="Main" />
        <put-attribute name="templatePath" value="views/remoteData_settings.jsp" />
    </definition>

    <definition name="typologySettingsPage" extends="templateMain">
        <put-attribute name="title" value="Main" />
        <put-attribute name="templatePath" value="views/typology_settings.jsp" />
    </definition>
    <definition name="typologySettings" template="views/typology_settings.jsp" />

    <definition name="reportPage" extends="templateMain">
        <put-attribute name="title" value="Main" />
        <put-attribute name="templatePath" value="views/reportPage.jsp" />
    </definition>

</tiles-definitions>

典型的控制器

@RequestMapping(method=RequestMethod.GET, params="action=loadTabMonitorTask")
public ModelAndView loadTabMonitorTask(HttpServletRequest request) {
    logger.debug("TaskController:loadTabMonitorTask > enter");

    Map<String, Object> model = new HashMap<String, Object>();

    //controller code

    return new ModelAndView("monitorTask", model);
}

1 个答案:

答案 0 :(得分:1)

终于找到了解决方案,见下文

Spring Java Config

删除了resolver.setPrefix(“views /”);

@Configuration
@EnableWebMvc
@EnableTransactionManagement
@ComponentScan("fr.myapp")
public class WebAppConfig extends WebMvcConfigurerAdapter {

    @Bean
    public UrlBasedViewResolver setupViewResolver() {
        UrlBasedViewResolver resolver = new UrlBasedViewResolver();
        //REMOVED THIS
        //resolver.setPrefix("views/");
        resolver.setSuffix(".jsp");
        resolver.setViewClass(JstlView.class);
        return resolver;
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/css/**").addResourceLocations("/css/");
        registry.addResourceHandler("/js/**").addResourceLocations("/js/");
        registry.addResourceHandler("/fonts/**").addResourceLocations("/fonts/");
    }

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

    @Bean
    public TilesConfigurer tilesConfigurer() {
        TilesConfigurer tilesConfigurer = new TilesConfigurer();
        tilesConfigurer.setDefinitions(new String[] { "tiles-def.xml" });
        tilesConfigurer.setCheckRefresh(true);
        return tilesConfigurer;
    }
}

瓦片-def.xml

使用“/”

为所有jsp路径添加前缀
<?xml version="1.0" encoding="UTF-8" ?>
<!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="templateMain" template="/views/layout.jsp">
        <put-attribute name="title" value="" />
        <put-attribute name="body" value="" />
        <put-list-attribute name="importCSS">
            <add-attribute value="css/bootstrap-Main.min.css" />
            <add-attribute value="css/dataTables.bootstrap.css" />
            <add-attribute value="css/main.css"/>
        </put-list-attribute>
        <put-list-attribute name="importJS">
            <!-- TODO Move datatable imports to required pages -->
            <add-attribute value="js/jquery.js" />
            <add-attribute value="js/jquery.dataTables.js" />
            <add-attribute value="js/bootstrap.min.js" />
            <add-attribute value="js/dataTables.bootstrap.js" />
            <add-attribute value="js/dataTables.bootstrapPagination.js" />
            <add-attribute value="js/jquery.json-2.4.min.js"/>
            <add-attribute value="js/common.js"/>
        </put-list-attribute>
    </definition>

    <definition name="templateMainLogout" template="/views/layout_logout.jsp">
        <put-attribute name="title" value="" />
        <put-attribute name="body" value="" />
        <put-list-attribute name="importCSS">
            <add-attribute value="css/bootstrap-Main.min.css" />
            <add-attribute value="css/dataTables.bootstrap.css" />
            <add-attribute value="css/main.css"/>
        </put-list-attribute>
        <put-list-attribute name="importJS">
            <add-attribute value="js/jquery.js" />
            <add-attribute value="js/bootstrap.min.js" />
        </put-list-attribute>
    </definition>

    <definition name="messageLogoutPage" extends="templateMainLogout">
        <put-attribute name="title" value="Main" />
        <put-attribute name="templatePath" value="/views/logout_message.jsp" />
    </definition>
    <definition name="messageLogout" template="/views/logout_message.jsp" />

    <definition name="monitorTaskPage" extends="templateMain">
        <put-attribute name="title" value="Main" />
        <put-attribute name="templatePath" value="/views/monitor_task.jsp" />
    </definition>
    <definition name="monitorTask" template="/views/monitor_task.jsp" />

    <definition name="createTaskPage" extends="templateMain">
        <put-attribute name="title" value="Main" />
        <put-attribute name="templatePath" value="/views/create_task.jsp" />
    </definition>
    <definition name="createTask" template="/views/create_task.jsp" />

    <definition name="generalSettingsPage" extends="templateMain">
        <put-attribute name="title" value="Main" />
        <put-attribute name="templatePath" value="/views/general_settings.jsp" />
    </definition>
    <definition name="generalSettings" template="/views/general_settings.jsp" />

    <definition name="remoteDataSettings" extends="templateMain">
        <put-attribute name="title" value="Main" />
        <put-attribute name="templatePath" value="/views/remoteData_settings.jsp" />
    </definition>

    <definition name="typologySettingsPage" extends="templateMain">
        <put-attribute name="title" value="Main" />
        <put-attribute name="templatePath" value="views/typology_settings.jsp" />
    </definition>
    <definition name="typologySettings" template="/views/typology_settings.jsp" />

    <definition name="reportPage" extends="templateMain">
        <put-attribute name="title" value="Main" />
        <put-attribute name="templatePath" value="/views/reportPage.jsp" />
    </definition>

</tiles-definitions>