一个请求
添加springboot依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
启动类
@ComponentScan(basePackages = "com.cyh.controller")
@EnableAutoConfiguration
public class HelloWorld {
public static void main(String[] args) {
SpringApplication.run(HelloWorld.class, args);
}
}
创建两个控制器, hello1 , hello2
@RestController
public class HelloController {
@RequestMapping("/hello1")
public String hello(){
return "hello1";
}
}
@RestController
public class Hello2Controller {
@RequestMapping("/hello2")
public String hello(){
return "hello2";
}
}
分别请求localhost:8080/hello1, localhost:8080/hello2 网页成功显示hello1和hello2。
加载静态资源
访问图片css等静态资源可以将文件放在resource下
访问图片请求地址为: http://localhost:8080/image/1.png
异常扑获
修改hello1请求, /0引发一个500错误。
@RequestMapping("/hello1")
public String hello(){
int i = 12/ 0;
return "hello1";
}
创建异常扑获类。
@ControllerAdvice
public class GlobalExeceptionHandler {
@ExceptionHandler(RuntimeException.class)
@ResponseBody
public Map<String, Object> exceptionHandler(){
Map<String, Object> result = new HashMap<String, Object>();
result.put("code","500");
result.put("msg","系统错误");
return result;
}
}
使用freeMarker
freeMarker:
FreeMarker是一款模板引擎, 即一种基于模板和要改变的数据。
引入freeMarker的依赖包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
在templates下创建ftl文件
<!DOCTYPE html>
<html>
<head lang="en">
<title>Spring Boot Demo - FreeMarker</title>
</head>
<body>
<h1> ${name}</h1>
</body>
</html>
通过控制器访问index.ftl, “name”的值将会传到页面${name}中
@Controller
public class HelloController {
@RequestMapping("/index")
public String hello(ModelMap map){
map.addAttribute("name", "my");
return "index";
}
}
在index.ftl是使用freemarker标签
<!DOCTYPE html>
<html>
<head lang="en">
<title>Spring Boot Demo - FreeMarker</title>
</head>
<body>
<h1> ${name}</h1>
<#if sex==1>
男
<#elseif sex==2>
女
<#else >
其他
</#if>
<#list userlist as user>
${user}
</#list>
</body>
</html>
修改控制器如下:
@Controller
public class HelloController {
@RequestMapping("/index")
public String hello(ModelMap map){
map.addAttribute("name", "my");
map.put("sex", 1);
List<String> userlist = new ArrayList<>();
userlist.add("张三");
userlist.add("李四");
userlist.add("王五");
map.put("userlist",userlist);
return "index";
}
}
访问jsp页面
在WEB-INF下创建jsp页面, 还需要配置application.properties
myjsp.jsp
<html>
<body>
<h2>Hello ${name} </h2>
</body>
</html>
application.properties, 配置jsp页面的位置
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
添加依赖
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
控制层
@Controller
public class HelloController {
@RequestMapping("/myjsp")
public String hello(ModelMap map){
map.addAttribute("name", "my");
return "/myjsp";
}
}