Spring steering view

I am trying to output an Excel file as a view in Spring Roo 1.0.2 What is the fastest way to do this? (Do I need to add a new mapping, etc.?) I am currently using Roo's AjaxUrlBasedViewResolver by default.

thank

+3
source share
1 answer

I don't think there is a specific “Roo” way to do this, but Spring MVC has an AbstractExcelView class that uses the Apache POI and does not cause any problems - I think this is the best you can hope for

First, create a view class that extends AbstractExcelView and implements the buildExcelDocument method:

 import org.springframework.web.servlet.view.document.AbstractExcelView;

 public class XlsView  extends AbstractExcelView { 

   @Override
   protected void buildExcelDocument(
            Map<String, Object> model, HSSFWorkbook workbook, 
            HttpServletRequest request, HttpServletResponse response) throws Exception {
      //Apache POI code to set up the HSSFWorkbook goes here
      response.setHeader("Content-Disposition", "attachment; filename=\"file.xls\"");
    }
 }

webmvc-config.xml. , , , TilesConfigurer:

   <bean id="excelViewResolver" class="org.springframework.web.servlet.view.XmlViewResolver">
            <property name="order" value="1"/>
            <property name="location" value="/WEB-INF/views/views-excel.xml"/>
        </bean>

, views-excel.xml :

  <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/aop   http://www.springframework.org/schema/aop/spring-aop-3.0.xsd   http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd   http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd   http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
     <bean name="XlsView" class="com.your.package.XlsView"/>
  </beans>
+5

Source: https://habr.com/ru/post/1735166/


All Articles