我有一个应用程序,其中我在Spring Boot中使用Angular 4。它具有主页,仪表板,检查,报告页面等。当我单击导航菜单中的任何链接时,它将转到该页面。当我点击localhost:8080时,它转到主页。
之前我们没有任何View Resolver。我们要求通过单击超级链接在“报告”页面之一中下载Excel / PDF / CSV。所以我添加了一个Configuration类和Controller并进行了测试,当我点击直接URL时,它工作正常(意味着我可以下载excel / pdf / csv)。但是,当我按下localhost:8080时,它没有显示主页,而是下载Excel。我确定我缺少一些配置。请提供任何帮助...我可以根据要求共享更多代码...
直接网址:localhost:8080 / api / inspDefectHistReportDownload.xls
WebConfiguration.java
@Configuration
public class WebConfiguration extends WebMvcConfigurerAdapter {
@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
configurer
.defaultContentType(MediaType.APPLICATION_JSON)
.favorPathExtension(true);
}
/*
* Configure ContentNegotiatingViewResolver
*/
@Bean
public ViewResolver contentNegotiatingViewResolver(ContentNegotiationManager manager) {
ContentNegotiatingViewResolver resolver = new ContentNegotiatingViewResolver();
resolver.setContentNegotiationManager(manager);
// Define all possible view resolvers
List<ViewResolver> resolvers = new ArrayList<>();
//resolvers.add(csvViewResolver());
resolvers.add(excelViewResolver());
//resolvers.add(pdfViewResolver());
resolver.setViewResolvers(resolvers);
return resolver;
}
/*
* Configure View resolver to provide XLS output using Apache POI library to
* generate XLS output for an object content
*/
@Bean
public ViewResolver excelViewResolver() {
return new ExcelViewResolver();
}
/*
* Configure View resolver to provide Csv output using Super Csv library to
* generate Csv output for an object content
*/
@Bean
public ViewResolver csvViewResolver() {
return new CsvViewResolver();
}
/*
* Configure View resolver to provide Pdf output using iText library to
* generate pdf output for an object content
*/
@Bean
public ViewResolver pdfViewResolver() {
return new PdfViewResolver();
}
我当然有ReportController,ExcelView ...和ExcelViewResolver.java等...(如果需要,我也可以发布这些类)
答案 0 :(得分:0)
经过长期调查,我发现下面的代码对我有用。现在,我可以通过相应的URL转到主页和其他屏幕,并单击特定屏幕上的超级链接,就可以下载所需的excel。
WebConfiguration.java
@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
configurer
.defaultContentType(MediaType.APPLICATION_JSON)
.ignoreAcceptHeader(true);
}
@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
registry.enableContentNegotiation(
new ExcelView()
);
}