一, 了解注解@Controller和@RestController

  @Controller:处理Http请求

  @RestController:Spring4以后新增注解,相当于@Controller和@ResponseBody

  @RequestMapping:url映射配置

二,Json接口开发

  使用@RestController即可。该注解如果返回是一个String,就直接返回String给客户端,如果是对象,会进行Json encode,返回对象json字符串

  声明一个账户信息的model

public class Account {
private Integer id;
private String username;
private String password;
private String email;
private String nicename; public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getEmail() {
return email;
} public void setEmail(String email) {
this.email = email;
} public String getNicename() {
return nicename;
} public void setNicename(String nicename) {
this.nicename = nicename;
}
}

  创建一个Controller类,通过@RestController注解,创建一个api接口

 

package com.example.demo.controllers;

import com.example.demo.domain.Account;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
public class HelloController { @RequestMapping("/getUser")
public Account getUser() {
Account account = new Account();
account.setUsername("yangzi");
account.setPassword("123456");
return account;
}
}

  调用效果:

  

三,静态网页

  SpringBoot静态网页还算简单,直接将html文件放在src.resources.static文件夹下即可,关联的其他页面和资源也可以直接放进去,就可以直接访问

  index.html:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>LiteMall</title>
</head>
<body>
Welcome to lite mall!
<br>
<a href="login.html">login</a>
</body>
</html>

  login.html:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>login</title>
</head>
<body>
login
</body>
</html>

  导入项目static文件夹下:

  

  如果名字是index,可以直接通过域名端口访问

  访问效果:

  

四,动态页面:

  动态页面需要先请求服务器,访问后台应用程序,然后再转向到页面,比如访问JSP。spring boot建议不要使用JSP,默认使用Thymeleaf来做动态页面。

  在pom.xml  中添加Thymeleaf组件

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

  创建html页面资源

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Products</title>
</head>
<body>
list </body>
</html>

  资源放在项目 templates目录下,切记。

  创建Controller,和动态页面跳转接口,接口中直接返回页面名字

package com.example.demo.controllers;

/**
* Created by zhang_guang_yang on 2018/11/17.
*/
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; @Controller
public class HtmlController { @RequestMapping(value = "/ppt", method = RequestMethod.GET)
public String productsList() {
System.out.print("productList api called");
return "products";
}
}

  访问效果:

  

  

  

最新文章

  1. 谷歌发布的首款基于HTTP/2和protobuf的RPC框架:GRPC
  2. PhpStorm Git 配置(解决文件没有变色的问题)
  3. win7下matlab2016a配置vlfeat
  4. Android监听来电和去电
  5. springmvc返回值、数据写到页面、表单提交、ajax、重定向
  6. Eclipse下导入外部jar包的3种方式 (zhuan)
  7. 【转】简单几步让App Store软件下载快如迅雷 -- 不错!!!
  8. 在vc中使用xtremetoolkit界面库-----安装及环境配置
  9. STL 之 queue、priority_queue 源代码剖析
  10. Reveal:分析iOS UI该武器
  11. Bagging和Boosting 概念及区别
  12. Angular4.0入门
  13. Android应用启动时Activity被创建两次
  14. Android 项目中的资源获取方法
  15. 这些git命令判断提交到哪个分支哪个项目上
  16. 解决Centos7不能联网且ifconfig出现command not found
  17. 20145322 Exp5 MS11_050
  18. vector interators incompatible
  19. HDU 6184 Counting Stars
  20. cocos2d-x项目101次相遇:使用触摸事件移动 精灵

热门文章

  1. c#学习笔记之Application.DoEvents应用
  2. java三种匿名的方式开启线程
  3. delphi中关于时间差的实例
  4. 2017-11-07-noip模拟题
  5. Ubuntu 16.04中的Dock的应用顺序调整
  6. Java Static Import的用法
  7. Linux下快速删除输错的密码技巧(快速删除输入的命令)
  8. 动态加载/删除css文件以及图片预加载
  9. Android 你应该注意的开发规范
  10. Git 的使用Git Bash和Git GUI