博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
自定义视图
阅读量:7220 次
发布时间:2019-06-29

本文共 2015 字,大约阅读时间需要 6 分钟。

一:结构

1.新建一个包

  这个包用来放自定义的view

  

 

二:程序

1.新建一个view类

  注意要写@Component 

1 package com.spring.view; 2  3 import java.util.Date; 4 import java.util.Map; 5  6 import javax.servlet.http.HttpServletRequest; 7 import javax.servlet.http.HttpServletResponse; 8  9 import org.springframework.stereotype.Component;10 import org.springframework.web.servlet.View;11 12 @Component13 public class HelloView implements View{14 15     @Override16     public String getContentType() {17         return "text/html";18     }19 20     @Override21     public void render(Map
model, HttpServletRequest request, HttpServletResponse response) throws Exception {22 response.setHeader("Content-type", "text/html;charset=UTF-8"); 23 response.setCharacterEncoding("UTF-8"); 24 response.getWriter().print("hello view,time "+new Date()); 25 }26 }

 

2.注册视图解析器BeanNameViewResolver

  InternalResourceViewResolver解析器是最后一个解析器,如果有其他的会先执行其他的,因为order越小,优先级越高。

  注意点:重新配置扫描包,这个扫描包是view的扫描包,不然view不会被加载到。

1 
2
12
13
14 15
16
17
18
19
20 21
22
23
24
25
26
27
28 29 30 31 32 33
34
35
36
37 38
39
40
41 42
43
44 45

 

3.index

<%@ page language="java" contentType="text/html; charset=utf-8"    pageEncoding="utf-8"%>
Insert title here Test BeanNameTest

 

4.concroller

1 package com.spring.it; 2  3 import org.springframework.stereotype.Controller; 4 import org.springframework.web.bind.annotation.RequestMapping; 5  6 @Controller 7 public class BeanNameTest { 8     @RequestMapping("/BeanNameTest") 9     public String hello() {10         System.out.println("ha ha");11         //刚写的view,但是第一个字母小写12         return "helloView";13     }14 }

 

5.效果、

  

 

转载地址:http://iqqym.baihongyu.com/

你可能感兴趣的文章
自动化部署必备技能之搭建YUM仓库
查看>>
20岁出头的时候,请摆正位置,一无所有却拥有一切
查看>>
eclipse中加入写好的android工程和出现的错误
查看>>
大脑如何休息
查看>>
rhel6atime,mtime,ctime
查看>>
设置RHEL6的SSH连接时间
查看>>
加快OSPF快速收敛的方法
查看>>
zabbix用户管理(9)
查看>>
MySQL数据库用户授权管理
查看>>
使用冒泡排序,排序多个字符串
查看>>
Huffman编码
查看>>
windows DHCP 服务器迁移技术文档
查看>>
Hql语句使用
查看>>
Spring2.5学习笔记2-AOP-利用通知接口
查看>>
不想转型高升?12条箴言助IT人士提高效率
查看>>
HP BL660 GEN8 SUSE 11 查询HBA信息 驱动版本 固件版本 设备名称
查看>>
幕夜萧萧,梦邀十月迷情
查看>>
Linux 系统自动化安装
查看>>
java中类的加载,及执行顺序
查看>>
apache配置虚拟主机及虚拟目录
查看>>