Search in sources :

Example 6 with View

use of cn.taketoday.web.view.View in project today-framework 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");
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) RequestContext(cn.taketoday.web.RequestContext) ServletRequestContext(cn.taketoday.web.servlet.ServletRequestContext) Row(org.apache.poi.ss.usermodel.Row) View(cn.taketoday.web.view.View) HashMap(java.util.HashMap) Map(java.util.Map) Sheet(org.apache.poi.ss.usermodel.Sheet) Cell(org.apache.poi.ss.usermodel.Cell) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) Test(org.junit.jupiter.api.Test)

Example 7 with View

use of cn.taketoday.web.view.View in project today-framework 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;
}
Also used : ArrayList(java.util.ArrayList) ContentNegotiatingViewResolver(cn.taketoday.web.view.ContentNegotiatingViewResolver) View(cn.taketoday.web.view.View)

Example 8 with View

use of cn.taketoday.web.view.View in project today-framework 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);
        }
    }
}
Also used : BindingContext(cn.taketoday.web.BindingContext) View(cn.taketoday.web.view.View) ModelAndView(cn.taketoday.web.view.ModelAndView)

Example 9 with View

use of cn.taketoday.web.view.View in project today-framework by TAKETODAY.

the class XlsViewTests method testXlsxView.

@Test
@SuppressWarnings("resource")
public void testXlsxView() throws Exception {
    View excelView = new AbstractXlsxView() {

        @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");
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) RequestContext(cn.taketoday.web.RequestContext) ServletRequestContext(cn.taketoday.web.servlet.ServletRequestContext) Row(org.apache.poi.ss.usermodel.Row) View(cn.taketoday.web.view.View) HashMap(java.util.HashMap) Map(java.util.Map) Sheet(org.apache.poi.ss.usermodel.Sheet) Cell(org.apache.poi.ss.usermodel.Cell) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) Test(org.junit.jupiter.api.Test)

Example 10 with View

use of cn.taketoday.web.view.View in project today-framework 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");
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) RequestContext(cn.taketoday.web.RequestContext) ServletRequestContext(cn.taketoday.web.servlet.ServletRequestContext) Row(org.apache.poi.ss.usermodel.Row) View(cn.taketoday.web.view.View) HashMap(java.util.HashMap) Map(java.util.Map) Sheet(org.apache.poi.ss.usermodel.Sheet) Cell(org.apache.poi.ss.usermodel.Cell) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) Test(org.junit.jupiter.api.Test)

Aggregations

View (cn.taketoday.web.view.View)12 Test (org.junit.jupiter.api.Test)8 RequestContext (cn.taketoday.web.RequestContext)6 ServletRequestContext (cn.taketoday.web.servlet.ServletRequestContext)6 ByteArrayInputStream (java.io.ByteArrayInputStream)6 HashMap (java.util.HashMap)6 Map (java.util.Map)6 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)6 Cell (org.apache.poi.ss.usermodel.Cell)6 Row (org.apache.poi.ss.usermodel.Row)6 Sheet (org.apache.poi.ss.usermodel.Sheet)6 Workbook (org.apache.poi.ss.usermodel.Workbook)6 XSSFWorkbook (org.apache.poi.xssf.usermodel.XSSFWorkbook)6 BindingContext (cn.taketoday.web.BindingContext)2 StaticWebServletApplicationContext (cn.taketoday.web.context.support.StaticWebServletApplicationContext)2 InternalResourceView (cn.taketoday.web.servlet.view.InternalResourceView)2 AbstractView (cn.taketoday.web.view.AbstractView)2 ContentNegotiatingViewResolver (cn.taketoday.web.view.ContentNegotiatingViewResolver)2 ModelAndView (cn.taketoday.web.view.ModelAndView)2 RedirectView (cn.taketoday.web.view.RedirectView)2