为什么在Spring Boot应用程序中无法识别Velocity模板?

时间:2017-11-30 14:54:34

标签: java spring spring-boot velocity

我想用Spring Boot设置我现有的Spring应用程序。我应用 spring-boot:run 命令。

我在配置中做了一些更改,所以我可以启动应用程序,打开登录页面,但是速度模板 login.vm 的位置应该只显示 login 文本而不是 login.vm 的内容:

enter image description here

我的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:

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包含以下行:

<h2><sitemesh:write property='title'/></h2>
<sitemesh:write property='body'/>

LoginController.java有:

@RequestMapping("/login.html")
public String login(params here) {
    //some checks
    return "login";
}

我的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模板位于 src / main / resources / templates / velocity

装饰器位于 src / main / webapp / WEB-INF / decorators

我在日志中没有错误。

所以我的问题:为什么我的应用程序显示登录文本而不是 login.vm 速度模板?如何解决这个问题?

我如何解决问题:

  1. 我将@RestController更改为@Controller
  2. 添加了 VelocityAutoConfiguration ,如所描述的here
  3. 从ApplicationConfig中删除了不必要的配置 - 仅保留siteMeshFilter()方法。

1 个答案:

答案 0 :(得分:0)

我如何解决问题:

  1. 我将@RestController更改为@Controller
  2. 添加了如此处所述的VelocityAutoConfiguration
  3. 从ApplicationConfig中删除了不必要的配置 - 只保留siteMeshFilter()方法。