首先配置BeanNameViewResolver
<!--配置BeanNameViewResolver视图解析器 使用视图的名字来解析视图--> <!-- 通过 order 属性来定义视图解析器的优先级, order 值越小优先级越高 --> <bean class="org.springframework.web.servlet.view.BeanNameViewResolver"> <property name="order" value="100"></property> </bean>
写一个实现了View接口的类 implements View
然后实现其抽象
方法:
package springmvc.views; import org.springframework.web.servlet.View; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.util.Date; import java.util.Map; public class HelloView implements View { @Override public String getContentType() { return "test/html"; } @Override public void render(Map<String, ?> map, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception { httpServletResponse.getWriter().print("hello view, time: " + new Date()); } }
记得要在类前面加上@Component
然后写调用方法 注意返回值是类名小写
@RequestMapping("/testView") public String testView(){ System.out.println("testView"); return "helloView"; }
超链接调用
<a href="HelloWorld/testView">Test View</a>