1.在dispatcherServlet-servlet.xml配置自定义视图
<!-- 配置视图 BeanNameViewResolver 解析器: 使用视图的名字来解析视图 --> <!-- 通过 order 属性来定义视图解析器的优先级, order 值越小优先级越高 --> <bean class="org.springframework.web.servlet.view.BeanNameViewResolver"> <property name="order" value="100"></property> </bean>2.Java 自定义一个视图类
package com.atguigu.springmvc.views;
import java.util.Date;
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;@Component
public class HelloView 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("hello view, time: " + new Date()); }}
3在jsp写一个动作触发
<a href="springmvc/testView">Test View</a>
4 配置action ,这里return的是视图名的第一个字母小写,springmvc会根据这个helloView名,去getBean,得到这个视图对象。
@RequestMapping("/testView")
public String testView(){ System.out.println("testView"); return "helloView"; }