[英]Why Velocity templates are not recognized in Spring Boot application?
I want to set up my existing Spring application with Spring Boot. 我想用Spring Boot设置现有的Spring应用程序。 I apply spring-boot:run command.
我应用spring-boot:run命令。
I did some changes in configrutions, so I can start the application, open login page, but the place where velocity template login.vm should be it shows only login text instead of the content of login.vm : 我对配置做了一些更改,因此我可以启动应用程序,打开登录页面,但是速度模板login.vm应该是仅显示登录文本而不是login.vm内容的位置 :
My ApplicationConfig: 我的ApplicationConfig:
@SpringBootApplication
@ImportResource({"classpath:/applicationContext*.xml", "classpath:/static/WEB-INF/dispatcher-servlet.xml"})
@ComponentScan(basePackages={"my.package"})
public class ApplicationConfig extends WebMvcConfigurerAdapter{
public static void main(String[] args) {
SpringApplication.run(ApplicationConfig.class, args);
}
@Bean
public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
return args -> {
System.out.println("Let's inspect the beans provided by Spring Boot:");
String[] beanNames = ctx.getBeanDefinitionNames();
Arrays.sort(beanNames);
for (String beanName : beanNames) {
System.out.println(beanName);
}
};
}
@Bean
public FilterRegistrationBean filterRegistrationBean() {
FilterRegistrationBean registrationBean = new FilterRegistrationBean();
CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
characterEncodingFilter.setForceEncoding(true);
characterEncodingFilter.setEncoding("UTF-8");
registrationBean.setFilter(characterEncodingFilter);
return registrationBean;
}
@Bean
RequestDumperFilter requestDumper() {
return new RequestDumperFilter();
}
@Bean
public FilterRegistrationBean siteMeshFilter(){
FilterRegistrationBean fitler = new FilterRegistrationBean();
MySiteMeshFilter siteMeshFilter = new MySiteMeshFilter();
fitler.setFilter(siteMeshFilter);
return fitler;
}
@Bean
public FilterRegistrationBean securityFilterChainRegistration() {
DelegatingFilterProxy delegatingFilterProxy = new DelegatingFilterProxy();
delegatingFilterProxy.setTargetBeanName(AbstractSecurityWebApplicationInitializer.DEFAULT_FILTER_NAME);
FilterRegistrationBean registrationBean = new FilterRegistrationBean(delegatingFilterProxy);
registrationBean.setName(AbstractSecurityWebApplicationInitializer.DEFAULT_FILTER_NAME);
registrationBean.addUrlPatterns("/*");
return registrationBean;
}
@Bean
public WebAppRootListener webAppRootListener() {
return new WebAppRootListener();
}
@Bean
public ServletRegistrationBean dispatcherServletRegistration() {
ServletRegistrationBean registration = new ServletRegistrationBean(dispatcherServlet(), "/");
Map<String,String> params = new HashMap<>();
params.put("load-on-startup","1");
registration.setInitParameters(params);
return registration;
}
@Bean
public DispatcherServlet dispatcherServlet() {
return new DispatcherServlet();
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**")
.addResourceLocations("/resources/");
}
@Bean
public ViewResolver getViewResolver() {
VelocityViewResolver resolver = new VelocityViewResolver();
resolver.setPrefix("");
resolver.setSuffix(".vm");
return resolver;
}
@Override
public void configureDefaultServletHandling(
DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
@Bean
public VelocityConfigurer velocityConfig() throws IOException {
VelocityConfigurer velocityConfigurer = new VelocityConfigurer();
velocityConfigurer.setResourceLoaderPath("/templates/velocity/");
Properties properties = new Properties();
properties.put("output.encoding","UTF-8");
properties.put("input.encoding","UTF-8");
properties.put("file.resource.loader.path","/templates/velocity/");
File file = new ClassPathResource("/templates/velocity/macroses.vm").getFile();
properties.put("velocimacro.library",file);
velocityConfigurer.setVelocityProperties(properties);
return velocityConfigurer;
}
}
MySiteMeshFilter: MySiteMeshFilter:
public class MySiteMeshFilter extends ConfigurableSiteMeshFilter {
@Override
protected void applyCustomConfiguration(SiteMeshFilterBuilder builder) {
builder.addDecoratorPath("/*", "/WEB-INF/decorators/default-decorator.jsp")
.addDecoratorPath("/login.html", "/WEB-INF/decorators/login-decorator.jsp")
.addDecoratorPaths("/admin/crowd/*", "/WEB-INF/decorators/crowd-decorator.jsp"
,"/WEB-INF/decorators/default-decorator.jsp")
.addDecoratorPaths("/admin/project/*", "/WEB-INF/decorators/project-decorator.jsp",
"/WEB-INF/decorators/default-decorator.jsp")
.addDecoratorPaths("/admin/report/*", "/WEB-INF/decorators/report-decorator.jsp"
, "/WEB-INF/decorators/default-decorator.jsp");
}
}
login-decorator.jsp has the following lines: login-decorator.jsp具有以下几行:
<h2><sitemesh:write property='title'/></h2>
<sitemesh:write property='body'/>
LoginController.java has: LoginController.java具有:
@RequestMapping("/login.html")
public String login(params here) {
//some checks
return "login";
}
My pom.xml contains the follwoing dependencies: 我的pom.xml包含以下依赖项:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.9</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-velocity</artifactId>
<version>1.4.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-tools</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
</dependency>
Velocity templates are located in src/main/resources/templates/velocity . 速度模板位于src / main / resources / templates / velocity中 。
Decorators are located in src/main/webapp/WEB-INF/decorators . 装饰器位于src / main / webapp / WEB-INF / decorators中 。
I do not have errors in logs. 我的日志中没有错误。
So my question: Why my application show login text instead of login.vm velocity template? 所以我的问题是:为什么我的应用程序显示登录文本而不是login.vm速度模板? How to fix this?
如何解决这个问题?
How I fixed the issue: 我如何解决此问题:
siteMeshFilter()
method only. siteMeshFilter()
方法。 How I fixed the issue: 我如何解决此问题:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.