如何使用viewResolvers显示html和jsp文件

时间:2015-05-27 10:28:23

标签: java html jsp

我正在尝试这样做,所以我的项目可以显示百里香html页面和jsp,但是使用下面的代码似乎只显示我的jsp的 WebMvcConfig:

@Bean
public TemplateResolver templateResolver() {
    TemplateResolver templateResolver = new ServletContextTemplateResolver();
    templateResolver.setOrder(0);
    templateResolver.setPrefix(VIEWS);
    templateResolver.setSuffix(".html");
    templateResolver.setTemplateMode("HTML5");
    templateResolver.setCacheable(false);
    return templateResolver;
}

@Bean
public SpringTemplateEngine templateEngine() {
    SpringTemplateEngine templateEngine = new SpringTemplateEngine();
    templateEngine.setTemplateResolver(templateResolver());
    templateEngine.addDialect(new SpringSecurityDialect());
    return templateEngine;
}

@Bean
public ThymeleafViewResolver viewResolver() {
    ThymeleafViewResolver thymeleafViewResolver = new ThymeleafViewResolver();
    thymeleafViewResolver.setTemplateEngine(templateEngine());
    thymeleafViewResolver.setCharacterEncoding("UTF-8");
    return thymeleafViewResolver;
}

@Bean
public InternalResourceViewResolver getInternalResourceViewResolver() {
    InternalResourceViewResolver resolver = new InternalResourceViewResolver();
    resolver.setOrder(1);
    resolver.setPrefix("/WEB-INF/views/");
    resolver.setSuffix(".jsp");
    return resolver;
}

Page1Controller(显示jsp):

@Controller
public class Page1Controller {
    @RequestMapping("/page1")
    public String Page1() {
        return "page1/page1";
    }
}

SigninController(不显示html):

@Controller
public class SigninController {

    @RequestMapping(value = "signin")
    public String signin() {
        return "signin/signin";
    }
}

2 个答案:

答案 0 :(得分:1)

Use multiple view revolvers with order value..

/* Thymeleaf configuration */  
@Bean  
public ServletContextTemplateResolver templateResolver() {  
ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver();  
templateResolver.setPrefix("/WEB-INF/views/");  templateResolver.setSuffix(".html");  templateResolver.setTemplateMode("HTML5");  
templateResolver.setOrder(1);  
return templateResolver;  
} 

@Bean  
public SpringTemplateEngine templateEngine(ServletContextTemplateResolver templateResolver){     
SpringTemplateEngine templateEngine = new SpringTemplateEngine();  templateEngine.setTemplateResolver(templateResolver);  
return templateEngine;  
} 

@Bean  
public ThymeleafViewResolver thymeleafViewResolver(SpringTemplateEngine templateEngine){  
ThymeleafViewResolver thymeleafViewResolver = new ThymeleafViewResolver();  thymeleafViewResolver.setViewNames(new String[]{"thymeleaf/*"});  thymeleafViewResolver.setTemplateEngine(templateEngine); 
return thymeleafViewResolver;  
} 

/* JSP view resolver */ 

@Bean  
public InternalResourceViewResolver jspViewResolver() {  
InternalResourceViewResolver resolver  = new InternalResourceViewResolver(); 
resolver.setPrefix("/WEB-INF/views/");  
resolver.setSuffix(".jsp");  
resolver.setViewClass(JstlView.class);  
return resolver;

}

答案 1 :(得分:0)

我必须在两个解析器上设置命令,然后在百万富翁解析器中我必须排除包含jsps的文件夹。我做了以下事情:

 @Bean
    public TemplateResolver templateResolver() {
        TemplateResolver templateResolver = new ServletContextTemplateResolver();
        templateResolver.setPrefix(VIEWS);
        templateResolver.setSuffix(".html");
        templateResolver.setTemplateMode("HTML5");
        templateResolver.setCacheable(false);
        return templateResolver;
    }

    @Bean
    public SpringTemplateEngine templateEngine() {
        SpringTemplateEngine templateEngine = new SpringTemplateEngine();
        templateEngine.setTemplateResolver(templateResolver());
        templateEngine.addDialect(new SpringSecurityDialect());
        return templateEngine;
    }

    @Bean
    public ThymeleafViewResolver viewResolver() {
        ThymeleafViewResolver thymeleafViewResolver = new ThymeleafViewResolver();
        thymeleafViewResolver.setTemplateEngine(templateEngine());
        thymeleafViewResolver.setCharacterEncoding("UTF-8");
        String[] excludedViews = new String[] {"page1/page1"};
        thymeleafViewResolver.setExcludedViewNames(excludedViews);
        return thymeleafViewResolver;
    }

    @Bean  
    public InternalResourceViewResolver jspViewResolver() {  
    InternalResourceViewResolver resolver  = new InternalResourceViewResolver(); 
    resolver.setPrefix("/WEB-INF/views/");  
    resolver.setSuffix(".jsp");  
    resolver.setViewClass(JstlView.class);  
    return resolver;

    }

此选项不包括ThymeleafViewResolver中的jsp视图。但是我可以将所有的fiels放在一个文件夹中并排除该文件夹。感谢大家的帮助。