美文网首页
view resolver

view resolver

作者: cdz620 | 来源:发表于2020-03-05 10:20 被阅读0次

    glossary

    ViewResolver

    根据controller返回的logic view name 生成View实例

    public interface ViewResolver {
      View resolveViewName(String viewName, Locale locale)
    throws Exception;
    }
    

    JSP tag libraries

    form-to-model binding

    <%@ taglib uri="http://www.springframework.org/tags/form" prefix="sf" %>

    spring’s general tag library

    <%@ taglib uri="http://www.springframework.org/tags" prefix="s" %>

    View

    将model数据渲染到response

    public interface View {
      String getContentType();
      void render(Map<String, ?> model,
                  HttpServletRequest request,
                  HttpServletResponse response) throws Exception;
    }
    

    常用的 view resolvers

    https://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#mvc-viewresolver

    InternalResourceViewResolver

    • 解析view name到对应的jsp文件, 支持JSTL
    • 可以加前缀或者加后缀来匹配文件
    • 如果使用jstl,要配置ViewClass

    configure view resolver

    注入View Resolver bean:

    • JavaConfig
    @Bean
    public ViewResolver viewResolver() {
      InternalResourceViewResolver resolver = new InternalResourceViewResolver();
      resolver.setPrefix("/WEB-INF/views/");
      resolver.setSuffix(".jsp");
      resolver.setViewClass(org.springframework.web.servlet.view.JstlView.class);
      return resolver;
    }
    
    • xml configuration
    <bean id="viewResolver"
                  class="org.springframework.web.servlet.view.
                                    InternalResourceViewResolver"
                  p:prefix="/WEB-INF/views/"
                  p:suffix=".jsp"
                  p:viewClass="org.springframework.web.servlet.view.JstlView" />
    

    相关文章

      网友评论

          本文标题:view resolver

          本文链接:https://www.haomeiwen.com/subject/xnrjchtx.html