BeanNameViewResolver类简介说明
下文笔者讲述BeanNameViewResolver类的简介说明,如下所示:
例2: TestView
BeanNameViewResolver视图解析器同XmlViewResolver相同, 都是使用逻辑视图名称同bean对象进行匹配解析例: 使用模板生成excel文件
控制器配置control-context.xml <bean id="beanNameViewResolver" class="org.springframework.web.servlet.view.BeanNameViewResolver" /> <bean id="viewController" class="ViewController"> <property name="dataModel"> <ref bean="model-DataModel"/> </property> </bean> <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="/view/*.report">viewController</prop> </props> </property> </bean> Excel视图ExcelView.java public class ExcelView extends AbstractExcelView { protected void buildExcelDocument(Map model, HSSFWorkbook wb, HttpServletRequest request, HttpServletResponse reqponse) throws Exception { //... Object[] datas = (Object[]) model.get("data"); //... HSSFSheet sheet; //get the sheet 1 in template sheet = wb.getSheetAt(0); //handle this sheet //... } } 实现 org.springframework.web.servlet.view.document.AbstractExcelView 类中的 buildExcelDocument方法。 3. ViewController控制器 ViewController.java public class ViewController extends MultiActionController { private DataModel dataModel; public void setDataModel(DataModel dataModel){ this.dataModel = dataModel; } public ModelAndView exportToExcel(HttpServletRequest request, HttpServletResponse response) throws Exception { Map model=new HashMap(); Object[] datas; datas = dataModel.getDatas.toArray(); model.put("data", datas); ExcelView excelView = new ExcelView(); excelView.setApplicationContext(this.getWebApplicationContext()); excelView.setUrl("/WEB-INF/Template"); return new ModelAndView(excelView,model); } } 4. 配置web.xml <servlet> <servlet-name>viewController</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/config/control-context.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>viewController</servlet-name> <url-pattern>*.report</url-pattern> </servlet-mapping> 5. 运行http://hostname/view/exportToExcel.report
例2: TestView
1.在SpringMVC配置文件中添加BeanNameViewResolver <bean class="org.springframework.web.servlet.view.BeanNameViewResolver"> <property name="order" value="10" /> </bean> <!-- 配置视图解析器 --> <!--该视图解析器的order值为Integer.MAX_VALUE --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/"></property> <property name="suffix" value=".jsp"></property> </bean> 2.创建一个TestView类,实现View接口 import java.util.Date; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Component; import org.springframework.web.servlet.View; @Component public class TestView implements View{ public String getContentType() { // TODO Auto-generated method stub return "text/html"; } public void render(Map model, HttpServletRequest request, HttpServletResponse response) throws Exception { // TODO Auto-generated method stub response.getWriter().print("Welcome to View:"+new Date()); } } 3.创建控制器 import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @RequestMapping(value="/springmvc") @Controller public class MyView { @RequestMapping(value="/testMyView") public String testView(){ System.out.println("testView"); return "helloView"; } } 此时return的是视图名的第一个字母小写 springmvc会根据这个helloView名,去getBean,得到这个视图对象
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。