SpringBoot项目开发(三):控制器与页面_zhuyu19911016520-CSDN博客_springboot写前端页面

沙海 2021年4月27日11:57:09Java评论31字数 1799阅读5分59秒阅读模式
摘要

SpringBoot项目开发(三):控制器与页面

文章源自JAVA秀-https://www.javaxiu.com/21099.html

SpringBoot项目开发(三):控制器与页面

文章源自JAVA秀-https://www.javaxiu.com/21099.html

SpringBoot项目开发(三):控制器与页面_zhuyu19911016520-CSDN博客_springboot写前端页面文章源自JAVA秀-https://www.javaxiu.com/21099.html

闪耀的瞬间
2018-07-21 12:14:41
SpringBoot项目开发(三):控制器与页面_zhuyu19911016520-CSDN博客_springboot写前端页面
8234

SpringBoot项目开发(三):控制器与页面_zhuyu19911016520-CSDN博客_springboot写前端页面文章源自JAVA秀-https://www.javaxiu.com/21099.html

收藏

7

文章源自JAVA秀-https://www.javaxiu.com/21099.html

分类专栏:
spring boot
java
SpringBoot 项目开发 文章源自JAVA秀-https://www.javaxiu.com/21099.html

版权 文章源自JAVA秀-https://www.javaxiu.com/21099.html


Web综合开发离不开页面与数据,本章带大家创建控制器,返回数据或html页面,以及自定义配置访问端口 文章源自JAVA秀-https://www.javaxiu.com/21099.html

在pom.xml文件中引入开发需要的依赖,上一篇是在工具上选择的,也可以手动编辑xml引入文章源自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>
12345678

这样就可以使用 @RestController和@Controller 控制器注解,分别对应返回数据与页面的控制器,也可通过@ResponseBody注解 与 返回ModelAndView 组合使用文章源自JAVA秀-https://www.javaxiu.com/21099.html

1.接着上一篇,创建完项目,在项目中添加一个DataController,并加上@RestController注解 文章源自JAVA秀-https://www.javaxiu.com/21099.html

import 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

运行项目,访问 localhost:8080/getByName?name=zy,带上name参数,可看到页面返回的数据,可以返回对象,List等,框架会自动转换为Json对象 SpringBoot项目开发(三):控制器与页面_zhuyu19911016520-CSDN博客_springboot写前端页面文章源自JAVA秀-https://www.javaxiu.com/21099.html

2.再添加一个IndexController,加上@Controller注解,并在项目resources-templates目录下创建index.html 文章源自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

SpringBoot项目开发(三):控制器与页面_zhuyu19911016520-CSDN博客_springboot写前端页面 运行项目,访问 localhost:8080/index ,可看到页面的内容 SpringBoot项目开发(三):控制器与页面_zhuyu19911016520-CSDN博客_springboot写前端页面文章源自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.html

文章源自JAVA秀-https://www.javaxiu.com/21099.html

继续阅读
速蛙云 - 极致体验,强烈推荐!!!购买套餐就免费送各大视频网站会员!快速稳定、独家福利社、流媒体稳定解锁!速度快,全球上网、视频、游戏加速、独立IP均支持!基础套餐性价比很高!这里不多说,我一直正在使用,推荐购买:https://www.javaxiu.com/59919.html
weinxin
资源分享QQ群
本站是JAVA秀团队的技术分享社区, 会经常分享资源和教程; 分享的时代, 请别再沉默!
沙海
匿名

发表评论

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定