use of cn.taketoday.web.view.View in project today-infrastructure by TAKETODAY.
the class XlsViewTests method testXls.
@Test
@SuppressWarnings("resource")
public void testXls() throws Exception {
View excelView = new AbstractXlsView() {
@Override
protected void buildExcelDocument(Map<String, Object> model, Workbook workbook, RequestContext request) throws Exception {
Sheet sheet = workbook.createSheet("Test Sheet");
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue("Test Value");
}
};
excelView.render(new HashMap<>(), requestContext);
Workbook wb = new HSSFWorkbook(new ByteArrayInputStream(response.getContentAsByteArray()));
assertThat(wb.getSheetName(0)).isEqualTo("Test Sheet");
Sheet sheet = wb.getSheet("Test Sheet");
Row row = sheet.getRow(0);
Cell cell = row.getCell(0);
assertThat(cell.getStringCellValue()).isEqualTo("Test Value");
}
use of cn.taketoday.web.view.View in project today-infrastructure by TAKETODAY.
the class XlsViewTests method testXlsxStreamingView.
@Test
@SuppressWarnings("resource")
public void testXlsxStreamingView() throws Exception {
View excelView = new AbstractXlsxStreamingView() {
@Override
protected void buildExcelDocument(Map<String, Object> model, Workbook workbook, RequestContext request) throws Exception {
Sheet sheet = workbook.createSheet("Test Sheet");
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue("Test Value");
}
};
excelView.render(new HashMap<>(), requestContext);
Workbook wb = new XSSFWorkbook(new ByteArrayInputStream(response.getContentAsByteArray()));
assertThat(wb.getSheetName(0)).isEqualTo("Test Sheet");
Sheet sheet = wb.getSheet("Test Sheet");
Row row = sheet.getRow(0);
Cell cell = row.getCell(0);
assertThat(cell.getStringCellValue()).isEqualTo("Test Value");
}
use of cn.taketoday.web.view.View in project today-infrastructure by TAKETODAY.
the class ViewResolverRegistry method initContentNegotiatingViewResolver.
private ContentNegotiatingViewResolver initContentNegotiatingViewResolver(View[] defaultViews) {
// ContentNegotiatingResolver in the registry: elevate its precedence!
this.order = order != null ? order : Ordered.HIGHEST_PRECEDENCE;
if (contentNegotiatingResolver != null) {
if (ObjectUtils.isNotEmpty(defaultViews) && CollectionUtils.isNotEmpty(contentNegotiatingResolver.getDefaultViews())) {
ArrayList<View> views = new ArrayList<>(contentNegotiatingResolver.getDefaultViews());
CollectionUtils.addAll(views, defaultViews);
contentNegotiatingResolver.setDefaultViews(views);
}
} else {
this.contentNegotiatingResolver = new ContentNegotiatingViewResolver();
contentNegotiatingResolver.setDefaultViews(Arrays.asList(defaultViews));
contentNegotiatingResolver.setViewResolvers(viewResolvers);
if (contentNegotiationManager != null) {
contentNegotiatingResolver.setContentNegotiationManager(contentNegotiationManager);
}
}
return contentNegotiatingResolver;
}
use of cn.taketoday.web.view.View in project today-infrastructure by TAKETODAY.
the class ModelAndViewReturnValueHandler method handleModelAndView.
/**
* Resolve {@link ModelAndView} return type
*
* @since 2.3.3
*/
public final void handleModelAndView(RequestContext context, Object handler, @Nullable ModelAndView modelAndView) throws Exception {
if (modelAndView != null) {
View view = modelAndView.getView();
String viewName = modelAndView.getViewName();
BindingContext bindingContext = context.getBindingContext();
bindingContext.addAllAttributes(modelAndView.getModel());
if (viewName != null) {
delegate.renderView(context, viewName);
} else if (view != null) {
delegate.renderView(context, view);
}
}
}
use of cn.taketoday.web.view.View in project today-framework by TAKETODAY.
the class FreeMarkerViewTests method freeMarkerViewResolver.
@Test
public void freeMarkerViewResolver() throws Exception {
MockServletContext sc = new MockServletContext();
FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
configurer.setConfiguration(new TestConfiguration());
StaticWebServletApplicationContext wac = new StaticWebServletApplicationContext();
wac.setServletContext(sc);
wac.getBeanFactory().registerSingleton("configurer", configurer);
wac.refresh();
FreeMarkerViewResolver vr = new FreeMarkerViewResolver("prefix_", "_suffix");
vr.setApplicationContext(wac);
View view = vr.resolveViewName("test", Locale.CANADA);
assertThat(view.getClass()).as("Correct view class").isEqualTo(FreeMarkerView.class);
assertThat(((FreeMarkerView) view).getUrl()).as("Correct URL").isEqualTo("prefix_test_suffix");
view = vr.resolveViewName("non-existing", Locale.CANADA);
assertThat(view).isNull();
view = vr.resolveViewName("redirect:myUrl", Locale.getDefault());
assertThat(view.getClass()).as("Correct view class").isEqualTo(RedirectView.class);
assertThat(((RedirectView) view).getUrl()).as("Correct URL").isEqualTo("myUrl");
view = vr.resolveViewName("forward:myUrl", Locale.getDefault());
assertThat(view.getClass()).as("Correct view class").isEqualTo(InternalResourceView.class);
assertThat(((InternalResourceView) view).getUrl()).as("Correct URL").isEqualTo("myUrl");
}
Aggregations