文章目录
Spring 框架学习(六)---- 返回页面+加载静态资源
一、返回页面
不加 @ResponseBody ,默认是返回一个网页
@RequestMapping("/getPage")
public String getPage(){
return "index.html";
}
二、返回非页面的数据
返回非页面的数据,必须在方法或者类上加 @ResponseBody,同时 我们返回的类型 springmvc会自动解析成对应的格式,不需要我们进行手动指定
1、返回 text/html
@RequestMapping("/getText")
@ResponseBody
public String getHTML(String name){
return "<h1>你好,欢迎用户:"+name+"<h1>";
}
访问接口,自动解析成 html格式
通过 Fiddler 进行抓包,查看返回响应的格式为 text/html。
2、返回 application/json
使用map存储数据,返回map
@RequestMapping("/getmap")
@ResponseBody
public Object getJson(){
HashMap<Object,Object> map = new HashMap<>();
map.put("msg","登陆成功");
map.put("status",200);
return map;
}
自动解析称为 json 格式的数据
三、加载静态资源
咱们就直接定死了写的格式
在webapp目录下创建static文件夹保存 css、js、html 资源
同时在spring-mvc.xml 文件中加入 过滤静态资源、加载静态资源的配置
<!-- 过滤静态资源, /.jsp /.html 不会经过-->