Usage of BeanNameViewResolver of [Spring MVC] (custom View)

BeanNameViewResolver (custom View)

Bean name view resolver is a view resolver used to return customized views. As the name implies, after configuration, the controller returns the bean name of the customized view, which can return the customized view.

Custom 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<String, ?> map,
                       HttpServletRequest httpServletRequest,
                       HttpServletResponse response)
            throws Exception {

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


}

Analysis: implement interface view (or inherit AbstractView), in which the function of method render is similar to the service method of servlet, but the render method also passes in model. @Component("myview") declares the view as a framework component, and the BeanName is set to myview, so you need to add the path of the customized view to the dispatcher servlet configuration file component scan label, such as

<context:component-scan base-package="org.spring;org.spring2;org.views"/>

The above is to configure the use of custom views through annotation. As can be seen from BeanName, you can also add beans to the configuration file to achieve the same effect,

<bean name="myview" class="org.views.MyView"/>

Finally, it must be reminded that when implementing the interface View, you must implement the getContentType method (even if you implement the getContentType method, don't think that you have set the response body type, then you are wrong). Otherwise, the View parser will not return the View. The reason is unknown, and the actual measurement is the same. Abstract class AbstractView implements the View interface. It is recommended to replace the View interface with the inherited abstract class AbstractView.

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<String, Object> map,
            HttpServletRequest httpServletRequest,
            HttpServletResponse response
    ) throws Exception {
        response.setContentType(ContentType);
        PrintWriter writer = response.getWriter();
        map.forEach((k,v)->writer.println(k+" "+v));
        writer.println("<pre>this is bottom</pre>");
    }
}
Controller
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",
                "<pre>hello huanghaifeng</pre>"
        );
        return "myview";
    }

}

Analysis: the model adds a property and returns the BeanName of the custom view

configuration file
 <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
        <property name="viewResolvers">
            <list>
                <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                    <property name="prefix" value="/"/>
                    <property name="suffix" value=".jsp"/>
                </bean>
                <bean class="org.springframework.web.servlet.view.BeanNameViewResolver">
                    <property name="order" value="10"/>
                </bean>
            </list>
        </property>
    </bean>

Here, BeanNameViewResolver needs to initialize the order property, so that InternalResourceViewResolver "is at the end of the view chain by default.

Published 52 original articles, won praise 4, visited 4553
Private letter follow

Keywords: Java Spring JSP

Added by nepeaNMedia on Tue, 11 Feb 2020 15:16:09 +0200