文章源自JAVA秀-https://www.javaxiu.com/21099.html 闪耀的瞬间 收藏 分类专栏: 版权 文章源自JAVA秀-https://www.javaxiu.com/21099.html 在pom.xml文件中引入开发需要的依赖,上一篇是在工具上选择的,也可以手动编辑xml引入文章源自JAVA秀-https://www.javaxiu.com/21099.html 这样就可以使用 @RestController和@Controller 控制器注解,分别对应返回数据与页面的控制器,也可通过@ResponseBody注解 与 返回ModelAndView 组合使用文章源自JAVA秀-https://www.javaxiu.com/21099.html 1.接着上一篇,创建完项目,在项目中添加一个DataController,并加上@RestController注解 文章源自JAVA秀-https://www.javaxiu.com/21099.html 运行项目,访问 localhost:8080/getByName?name=zy,带上name参数,可看到页面返回的数据,可以返回对象,List等,框架会自动转换为Json对象 2.再添加一个IndexController,加上@Controller注解,并在项目resources-templates目录下创建index.html 文章源自JAVA秀-https://www.javaxiu.com/21099.html 3.自定义端口,运行项目默认8080端口,我们可以修改为8001端口进行访问,打开resources目录下的 application.properties文件,添加server.port=8001,再运行项目,现在就需要访问 localhost:8001/index 才能打开页面了 文章源自JAVA秀-https://www.javaxiu.com/21099.html 这是新建完项目的第一次配置工作,想想之前的SpringMvc项目,新建完项目每次都要配置web.xml,创建springcontext.xml等等,大量重复的工作,SpringBoot把这些工作大大简化了,让开发人员只需要快速开发业务就好文章源自JAVA秀-https://www.javaxiu.com/21099.html SpringBoot内置了Tomcat,所以新建完项目可以直接运行文章源自JAVA秀-https://www.javaxiu.com/21099.html 下一篇 Thymeleaf 模版引擎文章源自JAVA秀-https://www.javaxiu.com/21099.html 文章源自JAVA秀-https://www.javaxiu.com/21099.htmlSpringBoot项目开发(三):控制器与页面
文章源自JAVA秀-https://www.javaxiu.com/21099.html文章源自JAVA秀-https://www.javaxiu.com/21099.html
2018-07-21 12:14:41
8234
文章源自JAVA秀-https://www.javaxiu.com/21099.html
7
spring boot
java
SpringBoot 项目开发 文章源自JAVA秀-https://www.javaxiu.com/21099.html
Web综合开发离不开页面与数据,本章带大家创建控制器,返回数据或html页面,以及自定义配置访问端口 文章源自JAVA秀-https://www.javaxiu.com/21099.html<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
12345678import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class DataController {
@RequestMapping("/getByName")
public String getByName(String name){
return "hi ,welcome to " + name;
}
}
1234567891011文章源自JAVA秀-https://www.javaxiu.com/21099.html
@Controller
public class IndexController {
//返回一个String的返回值,框架会自动在 templates目录下找到与返回值对应的html,后由Thymeleaf渲染出来
@RequestMapping("/index")
public String index(){
return "index";
}
}
12345678<!--index.html内容如下-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>SpringBoot项目开发系列</title>
</head>
<body>
<div style="padding-top: 20px;text-align: center">首页</div>
<div style="padding-top: 20px;text-align: center">SpringBoot项目开发系列</div>
</body>
</html>
123456789101112 运行项目,访问 localhost:8080/index ,可看到页面的内容
文章源自JAVA秀-https://www.javaxiu.com/21099.html

评论