Spring教程示例 - 使用java配置的Apache excel POI导出

问题描述 投票:1回答:2

我正在看这个mkyong tutorial出口到excel。

它看起来非常直接而且不会过于复杂。我能够完成除最后一步之外的所有步骤。最后一步是XML配置步骤。我使用java annotation config进行项目设置,本教程中的示例是xml。如何在'java config'/ annotaiton中配置以下XmlViewResolver而不是xml?请参阅下面的代码段。


这是我不知道如何配置的部分。我试图以注释/ java配置格式获取它。

弹簧配置

<beans ...>

  <bean
  class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />

    <bean class="com.mkyong.common.controller.RevenueReportController" />

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

</beans>

文件:spring-excel-views.xml

<bean id="ExcelRevenueSummary"
    class="com.mkyong.common.view.ExcelRevenueReportView">
</bean>

这就是我的控制器的样子。

@Controller
public class RevenueReportController extends AbstractController{

    private Logger log = LoggerFactory.getLogger(getClass());

    @Override
    @RequestMapping(value = "/search/exportToExcel", method = RequestMethod.GET)
    protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {

        String output = ServletRequestUtils.getStringParameter(request, "output");

        //dummy data
        Map<String,String> revenueData = new HashMap<String,String>();
        revenueData.put("Jan-2010", "$100,000,000");
        revenueData.put("Feb-2010", "$110,000,000");
        revenueData.put("Mar-2010", "$130,000,000");
        revenueData.put("Apr-2010", "$140,000,000");
        revenueData.put("May-2010", "$200,000,000");

        if(output ==null || "".equals(output)){
            //return normal view
            return new ModelAndView("RevenueSummary","revenueData",revenueData);

        }else if("EXCEL".equals(output.toUpperCase())){
            //return excel view
            return new ModelAndView("ExcelRevenueSummary","revenueData",revenueData);

        }else{
            //return normal view
            return new ModelAndView("RevenueSummary","revenueData",revenueData);

        }
    }
}
java xml spring annotations spring-java-config
2个回答
2
投票

将我的评论扩展到原始帖子,这是一个不需要任何xml配置的版本。使用此解决方案,您可以完全消除xml视图。您可以将excel视图定义为另一个bean,并直接在控制器中使用它。

配置类:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@EnableWebMvc
@ComponentScan(basePackages = { "com.mkyong.common" })
@Configuration
public class AppConfig extends WebMvcConfigurerAdapter {

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    @Bean
    public ExcelRevenueReportView excelRevenueReportView() {
        return new ExcelRevenueReportView();
    }

    @Bean
    public InternalResourceViewResolver getInternalResourceViewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/");
        resolver.setSuffix(".jsp");
        return resolver;
    }

}

contorller:

@Controller
public class RevenueReportController extends AbstractController{

    private Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    ExcelRevenueReportView excelRevenueReportView;

    @Override
    @RequestMapping(value = "/search/exportToExcel", method = RequestMethod.GET)
    protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {

        String output = ServletRequestUtils.getStringParameter(request, "output");

        //dummy data
        Map<String,String> revenueData = new HashMap<String,String>();
        revenueData.put("Jan-2010", "$100,000,000");
        revenueData.put("Feb-2010", "$110,000,000");
        revenueData.put("Mar-2010", "$130,000,000");
        revenueData.put("Apr-2010", "$140,000,000");
        revenueData.put("May-2010", "$200,000,000");

        if(output ==null || "".equals(output)){
            //return normal view
            return new ModelAndView("RevenueSummary","revenueData",revenueData);

        }else if("EXCEL".equals(output.toUpperCase())){
            //return excel view
            new ModelAndView(excelRevenueReportView, "revenueData", revenueData);

        }else{
            //return normal view
            return new ModelAndView("RevenueSummary","revenueData",revenueData);

        }
    }
}

1
投票

您需要进行如下更改:

创建一个新的配置类:

@Configuration
@ComponentScan("com.mkyong.common")
public class AppConfig {
    @Autowired
    ServletContext servletContext;

    @Bean
    public ViewResolver getXmlViewResolver() {
        XmlViewResolver resolver = new XmlViewResolver();
        resolver.setLocation(new ServletContextResource(servletContext,
                    "/WEB-INF/spring-excel-views.xml"));
        return resolver;
    }    
}

使用@Controller注释您的控制器类

@Controller
public class RevenueReportController extends AbstractController {

}

spring-excel-views.xml与使用XmlViewResolver时保持一致。它期望bean定义在XML文件中。

© www.soinside.com 2019 - 2024. All rights reserved.