如何将 Apache Tiles 集成到 Spring 引导 Web 应用程序中?
How to integrate Apache Tiles in a Spring Boot WebApplication?
我想在我的 spring 引导应用程序中添加 apache 磁贴。
就像我们在 application.properties 文件中配置前缀和后缀一样,我想知道我们如何配置 tiles.xml。
我在 POM.xml 文件中添加了 apache tiles 的 Maven 依赖项,但是当我从我的控制器返回 tile 定义的名称时,它给出了 404 错误。
这是我的 POM.xml 文件
<!-- https://mvnrepository.com/artifact/org.apache.tiles/tiles-jsp -->
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-jsp</artifactId>
<version>3.0.8</version>
</dependency>
这是我的 tiles.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="home-page"
template="/WEB-INF/layout/layout.jsp">
<put-attribute name="body" value="/WEB-INF/pages/landing-page.jsp" />
<put-attribute name="script" value="" />
<put-attribute name="stylesheet" value="" />
</definition>
</tiles-definitions>
这是我的控制器方法
@Controller
public class LandingPage {
@RequestMapping("/")
public String landingPage() {
return "home-page";
}
}
这是我的 TilesConfig.java 文件
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.view.UrlBasedViewResolver;
import org.springframework.web.servlet.view.tiles3.TilesConfigurer;
import org.springframework.web.servlet.view.tiles3.TilesView;
@Configuration
public class TilesConfig {
@Bean
public UrlBasedViewResolver viewResolver() {
UrlBasedViewResolver tilesViewResolver = new
UrlBasedViewResolver();
tilesViewResolver.setViewClass(TilesView.class);
return tilesViewResolver;
}
@Bean
public TilesConfigurer tilesConfigurer() {
TilesConfigurer tilesConfigurer = new TilesConfigurer();
String[] tilesXml = { "WEB-INF/tiles.xml" };
tilesConfigurer.setDefinitions(tilesXml);
return tilesConfigurer;
}
}
确保您拥有静态资源并且视图位于正确的路径中。
您可以按如下方式配置图块视图解析器。
@Bean
public UrlBasedViewResolver viewResolver() {
UrlBasedViewResolver tilesViewResolver = new UrlBasedViewResolver();
tilesViewResolver.setViewClass(TilesView.class);
return tilesViewResolver;
}
@Bean
public TilesConfigurer tilesConfigurer() {
TilesConfigurer tilesConfigurer = new TilesConfigurer();
String[] tilesXml = { "WEB-INF/tiles.xml" };
tilesConfigurer.setDefinitions(tilesXml);
return tilesConfigurer;
}
您需要做的第一件事是在您的 configuration.I 中使用 @EnableWebMvc 添加到以下配置中
您可以尝试将以下配置添加到您的应用程序中吗
public class MvcWebApplicationInitializer
extends AbstractAnnotationConfigDispatcherServletInitializer {
// Load database and spring security configuration
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[] { WebAppInitializer.class};
}
// Load spring web configuration
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[] { WebMvcConfig.class };
}
@Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = { "your component scan packages" })
public class WebMvcConfig implements WebMvcConfigurer {
/**
* Initialise Tiles on application startup and identify the location of the tiles configuration file, tiles.xml.
*
* @return tiles configurer
*/
@Bean
public TilesConfigurer tilesConfigurer() {
final TilesConfigurer configurer = new TilesConfigurer();
configurer.setDefinitions(new String[] { "WEB-INF/tiles.xml" });
configurer.setCheckRefresh(true);
return configurer;
}
/**
* Introduce a Tiles view resolver, this is a convenience implementation that extends URLBasedViewResolver.
*
* @return tiles view resolver
*/
@Bean
public TilesViewResolver tilesViewResolver() {
final TilesViewResolver resolver = new TilesViewResolver();
resolver.setViewClass(TilesView.class);
return resolver;
}
@Override
public void configureDefaultServletHandling(
DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
}
我想在我的 spring 引导应用程序中添加 apache 磁贴。
就像我们在 application.properties 文件中配置前缀和后缀一样,我想知道我们如何配置 tiles.xml。
我在 POM.xml 文件中添加了 apache tiles 的 Maven 依赖项,但是当我从我的控制器返回 tile 定义的名称时,它给出了 404 错误。
这是我的 POM.xml 文件
<!-- https://mvnrepository.com/artifact/org.apache.tiles/tiles-jsp -->
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-jsp</artifactId>
<version>3.0.8</version>
</dependency>
这是我的 tiles.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="home-page"
template="/WEB-INF/layout/layout.jsp">
<put-attribute name="body" value="/WEB-INF/pages/landing-page.jsp" />
<put-attribute name="script" value="" />
<put-attribute name="stylesheet" value="" />
</definition>
</tiles-definitions>
这是我的控制器方法
@Controller
public class LandingPage {
@RequestMapping("/")
public String landingPage() {
return "home-page";
}
}
这是我的 TilesConfig.java 文件
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.view.UrlBasedViewResolver;
import org.springframework.web.servlet.view.tiles3.TilesConfigurer;
import org.springframework.web.servlet.view.tiles3.TilesView;
@Configuration
public class TilesConfig {
@Bean
public UrlBasedViewResolver viewResolver() {
UrlBasedViewResolver tilesViewResolver = new
UrlBasedViewResolver();
tilesViewResolver.setViewClass(TilesView.class);
return tilesViewResolver;
}
@Bean
public TilesConfigurer tilesConfigurer() {
TilesConfigurer tilesConfigurer = new TilesConfigurer();
String[] tilesXml = { "WEB-INF/tiles.xml" };
tilesConfigurer.setDefinitions(tilesXml);
return tilesConfigurer;
}
}
确保您拥有静态资源并且视图位于正确的路径中。 您可以按如下方式配置图块视图解析器。
@Bean
public UrlBasedViewResolver viewResolver() {
UrlBasedViewResolver tilesViewResolver = new UrlBasedViewResolver();
tilesViewResolver.setViewClass(TilesView.class);
return tilesViewResolver;
}
@Bean
public TilesConfigurer tilesConfigurer() {
TilesConfigurer tilesConfigurer = new TilesConfigurer();
String[] tilesXml = { "WEB-INF/tiles.xml" };
tilesConfigurer.setDefinitions(tilesXml);
return tilesConfigurer;
}
您需要做的第一件事是在您的 configuration.I 中使用 @EnableWebMvc 添加到以下配置中 您可以尝试将以下配置添加到您的应用程序中吗
public class MvcWebApplicationInitializer
extends AbstractAnnotationConfigDispatcherServletInitializer {
// Load database and spring security configuration
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[] { WebAppInitializer.class};
}
// Load spring web configuration
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[] { WebMvcConfig.class };
}
@Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = { "your component scan packages" })
public class WebMvcConfig implements WebMvcConfigurer {
/**
* Initialise Tiles on application startup and identify the location of the tiles configuration file, tiles.xml.
*
* @return tiles configurer
*/
@Bean
public TilesConfigurer tilesConfigurer() {
final TilesConfigurer configurer = new TilesConfigurer();
configurer.setDefinitions(new String[] { "WEB-INF/tiles.xml" });
configurer.setCheckRefresh(true);
return configurer;
}
/**
* Introduce a Tiles view resolver, this is a convenience implementation that extends URLBasedViewResolver.
*
* @return tiles view resolver
*/
@Bean
public TilesViewResolver tilesViewResolver() {
final TilesViewResolver resolver = new TilesViewResolver();
resolver.setViewClass(TilesView.class);
return resolver;
}
@Override
public void configureDefaultServletHandling(
DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
}