[Spring MVC]之BeanNameViewResolver的用法(自定义View)

BeanNameViewResolver(自定义View)

BeanNameViewResolver这个视图解析器,用于返回自定义的视图。顾名思义,通过配置后,控制器返回自定义的视图的bean名,即可返回自定义的视图。

自定义View
package org.views;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.View;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.Map;

@Component("myview")
public class MyView implements View {
    private static final String ContentType = "text/html;charset=utf-8";
	@Override
    public String getContentType() {
        return ContentType;
    }

    @Override
    public void render(Map map,
                       HttpServletRequest httpServletRequest,
                       HttpServletResponse response)
            throws Exception {

        response.setContentType(ContentType);
        PrintWriter writer = response.getWriter();
        map.forEach((k,v)->writer.println(k+" "+v));
        writer.println("
this is bottom
"); } }

分析:实现接口View(或者继承AbstractView),其中方法render实际上跟servlet的service方法功能类似,只不过render方法还传入了模型(model)。@Component("myview")将该视图,声明为框架组件,而且BeanName设置为myview,所以需要将自定义的视图的路径添加到,调度器(dispatcherServlet)配置文件组件扫描标签中,如


以上是通过注释的方式,配置自定义视图的使用。由BeanName也可以看出,也可以在配置文件中添加bean的方式实现相同效果,


最后必须提醒,实现接口View时,一定要实现getContentType方法(即使实现了getContentType方法,也别以为是设置了响应体的类型,那你就错了),否则视图解析器将不会返回该视图,原因不明,实测如此。抽象类AbstractView实现了View接口,推荐使用继承抽象类AbstractView取代View接口。

package org.views;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.view.AbstractView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.Map;

@Component("myview")
public class MyView extends AbstractView {
    private static final String ContentType = "text/html;charset=utf-8";

    protected void renderMergedOutputModel(
            Map map,
            HttpServletRequest httpServletRequest,
            HttpServletResponse response
    ) throws Exception {
        response.setContentType(ContentType);
        PrintWriter writer = response.getWriter();
        map.forEach((k,v)->writer.println(k+" "+v));
        writer.println("
this is bottom
"); } }
控制器
package org.spring;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;


@Controller
public class HelloController {

    @RequestMapping(path = "/hello")
    public String execute(ModelMap modelMap) {
        modelMap.addAttribute(
                "testMessage",
                "
hello huanghaifeng
" ); return "myview"; } }

分析:模型添加了一个属性,然后返回自定义视图的BeanName

配置文件
 
        
            
                
                    
                    
                
                
                    
                
            
        
    

这里,BeanNameViewResolver需要初始化order属性,这样InternalResourceViewResolver"就默认位于视图链的最后。

你可能感兴趣的:(Spring)