SpringMVC 11. view, view resolution, view resolver

view, view resolver, view resolver


1. How Spring MVC resolves views

How to parse the view

2. Views and view resolvers

  • After the request processing method is executed, a ModelAndView object is finally returned. For those handlers that return String, View or ModeMap, Spring MVC also internally assembles them into a ModelAndView object, which contains the logical name and view of the model object.
  • Spring MVC obtains the final view object (View) with the help of the view resolver (ViewResolver), and the final view can be JSP, or it may be a view of various expressions such as Excel and JFreeChart.
  • The processor does not care about which view object is finally used to render the model data, and the processor focuses on the work of producing model data, so as to realize the full decoupling of MVC.

3. View

  • The role of the view is to render the model data and present the data in the model to
    the .
  • In order to realize the decoupling of the view model and the specific implementation technology, Spring defines a highly abstract View interface in the org.springframework.web.servlet package.

  • View objects are instantiated by the view resolver. Since views are stateless, they don't have thread-safety issues.

4. Commonly used view implementation classes

Common view implementation classes

5. View resolvers

  • SpringMVC provides different strategies for the resolution of logical view names. You can configure one or more resolution strategies in the Spring WEB context, and specify the order between them. Each mapping strategy corresponds to a specific view resolver instance class.
  • The role of the view resolver is relatively simple: it resolves the logical view into a specific view object.
  • All view resolvers must implement the ViewResolver interface.

6. Commonly used view parser implementation classes

View resolver implementation class

Programmers can choose one view resolver or mix multiple view resolvers

  • Each view resolver implements the Ordered interface and exposes an order property. You can specify the parser's priority order through the order property. The smaller the order, the higher the priority.
  • SpringMVC will parse the logical view name in the priority order of the view parser until the parsing is successful and the view object is returned, otherwise a ServletException will be thrown

7.InternalResourceViewResolver

  • JSP is the most common view technology, you can use InternalResourceViewResolver as a view resolver:
    InternalResourceViewResolver as view resolver

  • If JSTL is used in the project, SpringMVC will automatically convert the view from =InternalResourceView to JstlView

  • If you use the fmt tag of JSTL, you need to configure the internationalized resource file in the SpringMVC configuration file
<!--配置国际化资源文件-->
    <bean  id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basename" value="i18n"></property>
    </bean>
  • If you want to directly respond to the page rendered by SpringMVC, you can use the mvc:viewcontroller
    tag to achieve
    <!--配置直接转发的页面-->
    <!--可以直接转发相应的页面,无需再经过Handler方法-->
    <mvc:view-controller path="success" view-name="success"></mvc:view-controller>

Implementation example 1:

  • Custom view resolver (by view name):
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.View;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.Map;


@Component
public class HelloView implements View {
    @Nullable
    @Override
    public String getContentType() {
        return "text/html";
    }

    @Override
    public void render(@Nullable Map<String, ?> map, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
        httpServletResponse.getWriter().print("Hello Time : "+ new Date()) ;
    }
}

jsp page:

 <a href="/springmvc/testView">test View</a>

Controller class:

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

@RequestMapping("/springmvc")
@Controller
public class TestMyView {
    @RequestMapping("/testView")
    public String testView(){
        return "helloView" ;
    }
}

springmvc.xml placement


    <!--配置视图解析器BeanNameViewResolver:使用视图的名字解析视图-->
    <!--通过order设置优先级-->
    <bean class="org.springframework.web.servlet.view.BeanNameViewResolver">

        <property name="order" value="100"></property>
    </bean>

Implementation example 2

springmvc.xml placement

<!--配置国际化资源文件-->
    <bean  id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basename" value="i18n"></property>
    </bean>
    <!--配置直接转发的页面-->
    <!--可以直接转发相应的页面,无需再经过Handler方法-->
    <mvc:view-controller path="success" view-name="success"></mvc:view-controller>

    <!--在实际开发中通常需要配置mvc:annotation-driven标签-->
    <mvc:annotation-driven></mvc:annotation-driven>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

..............

 <fmt:message key="i18n.username"></fmt:message>
    <br><br>
 <fmt:message key="i18n.password"></fmt:message>

Rewrite the address to success access through the direct address bar to achieve internationalization.

8. Excel View

  • If you want to use Excel to display the data list, you only need to extend the AbstractExcelView or AbstractJExcel View provided by SpringMVC. Implement the buildExcelDocument() method and use the model data object to build the Excel document in the method.
  • AbstractExcelView is based on POI API, while AbstractJExcelView is based on JExcelAPI.
  • The view object needs to configure a bean in the IOC container, and use BeanNameViewResolver as the view resolver.
  • If you want to download the Excel document directly in the browser, you can set the value of the response header Content-Disposition to attachment;filename=xxx.xls

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325410138&siteId=291194637