gpt4 book ai didi

java - Spring + Tiles 3 JSP 未渲染

转载 作者:行者123 更新时间:2023-12-01 10:45:02 26 4
gpt4 key购买 nike

当通过 Eclipse 中的 @SpringApplication 启动时,我的 Spring Boot 应用程序运行得非常顺利。

但是作为 WAR 部署到 tomcat7 中,我的 JSP 未呈现。

相反,显示 JSP 的路径(示例:views/monitor_task.jsp)

日志文件中没有错误

Spring Java 配置

@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;
}
}

tiles-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>

典型的 Controller

@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);
}

最佳答案

终于找到解决办法了,见下文

Spring Java 配置

删除了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;
}
}

tiles-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>

关于java - Spring + Tiles 3 JSP 未渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34229574/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com