I am trying to implement Excel/PDF download using org.springframework.web.servlet.view.document. AbstractXlsxView .
My application is configured using org.springframework.web.servlet.support. AbstractAnnotationConfigDispatcherServletInitializer .
I have created my AbstractXlsxView with @Component and specified name to it.
@Component(VIEW_NAME_BASE_XLSX)
public class BaseXlsxView extends AbstractXlsxView {
public static final String VIEW_NAME_BASE_XLSX = "baseXlsxView";
public static final String MODEL_ATTRIBUTE_NAME_BASE_XLSX_VIEW_BEAN = "MODEL_ATTRIBUTE_NAME_BASE_XLSX_VIEW_BEAN";
@Override
protected void buildExcelDocument(Map<String, Object> modelMap, Workbook workbook, HttpServletRequest request,
HttpServletResponse response) throws Exception {
Sheet sheet = workbook.createSheet("Base Sheet");
Row row = sheet.createRow(0);
Cell cell = row.createCell(0, CellType.STRING);
cell.setCellValue(new Date());
response.setHeader("Content-Disposition",
"attachment; filename=" + "BaseExcelDocument" + System.currentTimeMillis());
}
}
I am using the specified name as view name in ModelAndView returned by my controller.
@GetMapping(value = "/exportBaseXlsx")
public ModelAndView exportBaseXlsx(ModelMap modelMap, HttpServletRequest httpServletRequest) {
return new ModelAndView(BaseXlsxView.VIEW_NAME_BASE_XLSX, BaseXlsxView.MODEL_ATTRIBUTE_NAME_BASE_XLSX_VIEW_BEAN,
null);
}
This always throws 404 . It is trying to read a JSP file(as I have configured View resolver) instead of reading BaseXlsxView .
I have seen this way of implementing(not using views.xml with <bean id="myBeanId" class="myClassQualifiedName" >
) AbstractView is working in other applications but it is not working here.
我记得SpringMVC如果没有分配默认的view-adapter,也许默认的view-adapter返回jsp view。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.