zoukankan      html  css  js  c++  java
  • springMVC-view

    1.ViewResolver(解析成视图对象View:jsp等)
        01.配置的试图解析器是InternalResourceViewResolver,那么默认的View就是InternalResourceView
            <!-- 配置视图解析器 -->
            <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="viewResolver">
                <property name="prefix" value="/WEB-INF/view/"></property>
                <property name="suffix" value=".jsp"></property>
            </bean>
        02.InternalResourceView主要是显示jsp的;InternalResourceViewResolver主要是解析在同一个web应用下通过转发的那个结果。
    
    
    2.配置国际化资源文件,并使用jstl的fmt使用国际化资源(一旦加入了jstl的jar包,View就会变成jstlView)
        01.
            <!-- 配置国家化资源文件 -->
            <bean class="org.springframework.context.support.ResourceBundleMessageSource" id="messageSource">
                <property name="basename" value="i18n"></property>(i18n.properties等放在类路径下)
            </bean>
        02.(加入了jstl的jar包)导入fmt标签<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt"  prefix="fmt"%>
        03.使用国际化资源
            <fmt:message key="username"></fmt:message>
        
        
    3.只通过配置实现转发,不经过handler处理器(直接响应通过springMVC渲染的页面)
        <!-- 不需要经过handler,直接转。但如果只写这么一句配置,之前那些经过handler的请求就是404了 -->
        <!-- 当请求为http://localhost:8080/springMVC-view/testSuccess时转发到/WEB-INF/view/success.jsp -->
        <mvc:view-controller path="/testSuccess" view-name="success"/>
        
        <!-- 要加上下面这一句,上面的问题才能解决 -->
        <mvc:annotation-driven></mvc:annotation-driven>
    
    4.自定义视图解析器
        01.自定义一个类继承View接口,并配置到IOC容器中
        02.若想使用该视图,可以使用BeanNameViewResovler视图解析器
            <!-- 
                配置视图解析器,可以通过order属性来配置解析器的优先级,值越小优先级越高,默认都是Integer.MAX_VALUE。
                BeanNameViewResolver是从IOC容器中找以目标方法返回值为名字的bean(View对象),调用其的render方法。
                若解析不成功再用优先级比它低的解析器
                每次请求都会产生一个新的view对象
             -->
            <bean class="org.springframework.web.servlet.view.BeanNameViewResolver" id="beanNameViewResolver">
                <property name="order" value="100"></property>
            </bean>
        
    5.重定向
        只要在返回的字符串中写上"redirect:/index.jsp"即可;"forward:/index.jsp"是转发。(第一个/都代表web应用根目录)    
        注意:一旦重定向就不能访问WEB-INF下的页面了,会报404。
        
            

    MyView.java

    package views;
    
    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;
    
    //自定义视图,继承View。若要显示Excel,可以继承AbstractExcelView会比较方便
    @Component
    public class MyView implements View {
        //返回内容的类型
        @Override
        public String getContentType() {
            return "text/html";
        }
    
        //渲染视图的过程
        @Override
        public void render(Map<String, ?> model, HttpServletRequest request, HttpServletResponse response)
                throws Exception {
            response.getWriter().print(model.get("user"));
        }
    
    }
  • 相关阅读:
    [转]asp.net页面缓存技术
    UL和LI在div中的高度的IE6下兼容性
    jquery制作的横向图片滚动带横向滚动条TackerScroll
    电脑可以上网,但是qq登陆不上去?
    Introduction to discrete event system学习笔记4.6
    Introduction to Discrete event system学习笔记4.9
    Introduction to discrete event systemsstudy 4.5
    Symbolic synthesis of obserability requirements for diagnosability B.Bittner,M.Bozzano,A.Cimatti,and X.Olive笔记4.16
    Introduction to discrete event system学习笔记4.8pm
    Introduction to discrete event system学习笔记 4.8
  • 原文地址:https://www.cnblogs.com/feifeiyun/p/6606807.html
Copyright © 2011-2022 走看看