SpringMVC 自定义视图

2020-02-13  本文已影响0人  桑鱼nicoo
@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>
上一篇下一篇

猜你喜欢

热点阅读