我正在查看要导出到excel的mkyong tutorial。
它看起来很简单,也不过分复杂。我能够完成除最后一个步骤以外的所有步骤。最后一步是XML配置步骤。我使用Java批注配置进行项目设置,本教程中的示例为xml。如何在“ java config” / annotaiton(而非xml)中配置以下XmlViewResolver
?请参见下面的代码段。
这是我不知道如何配置的部分。我正在尝试以注解/ 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);
}
}
}
最佳答案
将我的评论扩展到原始帖子,这是不需要任何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;
}
}
控制器:
@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);
}
}
}