BeanNameViewResolver类详解 - ViewResolver系列四

BeanNameViewResolver视图解析器跟XmlViewResolver有点类似,也是通过把返回的逻辑视图名称去匹配定义好的视图bean对象。

实例一:根据模板生成Excel文件

1. 控制器配置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>

2. 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

实例二:HelloView

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. 创建一个HelloView类,实现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 HelloView 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,得到这个视图对象

版权声明:本文为JAVASCHOOL原创文章,未经本站允许不得转载。