标签:uil 视图 htm 并且 util 接口 ref get render
1、创建一个视图类实现spring的View接口,并且把视图类用@Component注解为sprinmvc组建(重要)
package com.hy.springmvc.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; @Component public class HelloView implements View{ @Override public String getContentType() { return "text/html"; } @Override public void render(Map<String, ?> arg0, HttpServletRequest arg1, HttpServletResponse arg2) throws Exception { arg2.getWriter().println("this is a hello viesw"); } }
2、配置视图解析器BeanNameViewResolver,并且要指定解析器使用顺序Order,顺序先于常用解析器
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver"> <property name="order" value="100"></property> </bean>
3、测试代码:
@RequestMapping("/testView") public String testView() { System.out.println("testView"); return "helloView"; }
<a href="test/testView">testView</a>
这里会在页面上打印出helloview
注意:可以使用自定义视图处理多种视图,比如excel只需要继承抽像类AbstractXlsxView AbstractXlsView实现相应的方法buildExcelDocument
标签:uil 视图 htm 并且 util 接口 ref get render
原文地址:http://www.cnblogs.com/hy87/p/6209497.html