美文网首页
SpringMVC 自定义视图

SpringMVC 自定义视图

作者: 桑鱼nicoo | 来源:发表于2020-02-13 17:46 被阅读0次
    @Component
    public class HelloView implements View {
    
        @Override
        public String getContentType() {
            return "text/html";
        }
    
        @Override
        public void render(Map<String, ?> map, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
    
            httpServletResponse.getWriter().print("hello view ,time :" + new Date());
        }
    }
    
    @Controller
    @RequestMapping("/springmvc")
    public class TestController {
        @RequestMapping("/testView")
        public String testView(){
            System.out.println("testView");
            return "helloView";
        }
    }
    
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:mvc="http://www.springframework.org/schema/mvc"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
    
        <!-- 配置自动扫描的包-->
        <context:component-scan base-package="com.sangyu.springmvc"></context:component-scan>
        <!-- 配置视图解析器:如何把handler方法返回值解析为实际的物理视图-->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/views/"></property>
            <property name="suffix" value=".jsp"></property>
        </bean>
    <!--BeanNameViewResolver :将逻辑视图名解析为一个Bean,Bean的id等于逻辑视图名-->
    <!-- 通过order 属性来定义视图解析器的优先级,order越小优先级越高-->
        <bean class="org.springframework.web.servlet.view.BeanNameViewResolver">
            <property name="order" value="100"></property>
        </bean>
    </beans>
    
    <a href="/springmvc/testView">Test testView</a>
    

    相关文章

      网友评论

          本文标题:SpringMVC 自定义视图

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