Spring Boot开发Web应用

Spring Boot快速入门中我们完成了一个简单的RESTful Service,体验了快速开发的特性。在留言中也有朋友提到如何把处理结果渲染到页面上。那么本篇就在上篇基础上介绍一下如何进行Web应用的开发。

静态资源访问

在我们开发Web应用的时候,需要引用大量的js、css、图片等静态资源。

默认配置

Spring Boot默认提供静态资源目录位置需置于classpath下,目录名需符合如下规则:

  • /static
  • /public
  • /resources
  • /META-INF/resources

举例:我们可以在src/main/resources/目录下创建static,在该位置放置一个图片文件。启动程序后,尝试访问http://localhost:8080/D.jpg。如能显示图片,配置成功。

渲染Web页面

在之前的示例中,我们都是通过@RestController来处理请求,所以返回的内容为json对象。那么如果需要渲染html页面的时候,要如何实现呢?

模板引擎

在动态HTML实现上Spring Boot依然可以完美胜任,并且提供了多种模板引擎的默认配置支持,所以在推荐的模板引擎下,我们可以很快的上手开发动态网站。

Spring Boot提供了默认配置的模板引擎主要有以下几种:

  • Thymeleaf
  • FreeMarker
  • Velocity
  • Groovy
  • Mustache

Spring Boot建议使用这些模板引擎,避免使用JSP,若一定要使用JSP将无法实现Spring Boot的多种特性,具体可见后文:支持JSP的配置

当你使用上述模板引擎中的任何一个,它们默认的模板配置路径为:src/main/resources/templates。当然也可以修改这个路径,具体如何修改,可在后续各模板引擎的配置属性中查询并修改。

Thymeleaf

hymeleaf是一个XML/XHTML/HTML5模板引擎,可用于Web与非Web环境中的应用开发。它是一个开源的Java库,基于Apache License 2.0许可,由Daniel Fernández创建,该作者还是Java加密库Jasypt的作者。

Thymeleaf提供了一个用于整合Spring MVC的可选模块,在应用开发中,你可以使用Thymeleaf来完全代替JSP或其他模板引擎,如Velocity、FreeMarker等。Thymeleaf的主要目标在于提供一种可被浏览器正确显示的、格式良好的模板创建方式,因此也可以用作静态建模。你可以使用它创建经过验证的XML与HTML模板。相对于编写逻辑或代码,开发者只需将标签属性添加到模板中即可。接下来,这些标签属性就会在DOM(文档对象模型)上执行预先制定好的逻辑。

示例模板:

<table>
  <thead>
    <tr>
      <th th:text="#{msgs.headers.name}">Name</td>
      <th th:text="#{msgs.headers.price}">Price</td>
    </tr>
  </thead>
  <tbody>
    <tr th:each="prod : ${allProducts}">
      <td th:text="${prod.name}">Oranges</td>
      <td th:text="${#numbers.formatDecimal(prod.price,1,2)}">0.99</td>
    </tr>
  </tbody>
</table>

可以看到Thymeleaf主要以属性的方式加入到html标签中,浏览器在解析html时,当检查到没有的属性时候会忽略,所以Thymeleaf的模板可以通过浏览器直接打开展现,这样非常有利于前后端的分离。

在Spring Boot中使用Thymeleaf,只需要引入下面依赖,并在默认的模板路径src/main/resources/templates下编写模板文件即可完成。

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

在完成配置之后,举一个简单的例子,在快速入门工程的基础上,举一个简单的示例来通过Thymeleaf渲染一个页面。

@Controller
public class HelloController {
    @RequestMapping("/")
    public String index(ModelMap map) {
        // 加入一个属性,用来在模板中读取
        map.addAttribute("host", "http://blog.didispace.com");
        // return模板文件的名称,对应src/main/resources/templates/index.html
        return "index";  
    }
}
<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8" />
    <title></title>
</head>
<body>
<h1 th:text="${host}">Hello World</h1>
</body>
</html>

如上页面,直接打开html页面展现Hello World,但是启动程序后,访问http://localhost:8080/,则是展示Controller中host的值:http://blog.didispace.com,做到了不破坏HTML自身内容的数据逻辑分离。

更多Thymeleaf的页面语法,还请访问Thymeleaf的官方文档查询使用。

Thymeleaf的默认参数配置

如有需要修改默认配置的时候,只需复制下面要修改的属性到application.properties中,并修改成需要的值,如修改模板文件的扩展名,修改默认的模板路径等。

# Enable template caching.
spring.thymeleaf.cache=true 
# Check that the templates location exists.
spring.thymeleaf.check-template-location=true 
# Content-Type value.
spring.thymeleaf.content-type=text/html 
# Enable MVC Thymeleaf view resolution.
spring.thymeleaf.enabled=true 
# Template encoding.
spring.thymeleaf.encoding=UTF-8 
# Comma-separated list of view names that should be excluded from resolution.
spring.thymeleaf.excluded-view-names= 
# Template mode to be applied to templates. See also StandardTemplateModeHandlers.
spring.thymeleaf.mode=HTML5 
# Prefix that gets prepended to view names when building a URL.
spring.thymeleaf.prefix=classpath:/templates/ 
# Suffix that gets appended to view names when building a URL.
spring.thymeleaf.suffix=.html  spring.thymeleaf.template-resolver-order= # Order of the template resolver in the chain. spring.thymeleaf.view-names= # Comma-separated list of view names that can be resolved.

支持JSP的配置

Spring Boot并不建议使用,但如果一定要使用,可以参考此工程作为脚手架:JSP支持

spring boot完整案例

原作者:程序员didi
原文出处:http://blog.didispace.com/springbootweb/

微信公众号:CodeD
微信公众号

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: Spring Boot是一种基于Java的快速应用程序开发框架,旨在简化新Spring应用程序的创建和部署。它提供了自动配置、开箱即用的功能和简单的Maven配置,使您能够使用Spring框架来开发Web应用程序。 要使用Spring Boot开发Web应用程序,首先需要在pom.xml文件中添加spring-boot-starter-web依赖: ``` <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> ``` 然后,您可以创建一个Spring控制器来处理Web请求。例如,以下是一个简单的控制器,它响应'/hello'路径的GET请求: ``` @RestController public class HelloController { @GetMapping("/hello") public String sayHello() { return "Hello, Spring Boot!"; } } ``` 最后,您可以使用Spring Boot提供的内置Tomcat服务器在本地运行应用程序。只需在应用程序的main方法中添加@SpringBootApplication注解并调用SpringApplication.run方法即可启动应用程序: ``` @SpringBootApplication public class MyApplication { public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); } } ``` 这就是使用Spring Boot开发Web应用程序的基本流程。希望这些信息对您有帮助。 ### 回答2: Spring Boot 是一个开源的Java开发框架,它可以帮助开发者快速构建基于Spring框架的应用程序。用Spring Boot开发Web应用非常方便。 首先,Spring Boot提供了自动配置功能,简化了应用程序的配置过程。通过使用注解和约定,Spring Boot能够自动配置应用程序所需的各种组件和依赖项,例如数据库连接、Web服务器等。这样开发人员不需要手动编写大量的配置代码,提高了开发效率。 其次,Spring Boot具有内嵌的Web服务器(如Tomcat、Jetty等)和Servlet容器。这意味着开发者无需部署额外的Web服务器,只需打包应用程序为可执行的JAR文件,即可直接运行。这样可以减少运维工作和服务器资源的消耗。 另外,Spring Boot还提供了丰富的开发工具和插件,如Spring Boot DevTools和Spring Boot Actuator等。这些工具可以帮助开发人员快速开发和调试应用程序,还可以监控和管理应用程序的各种指标和状态。 除此之外,Spring Boot还支持使用Spring MVC快速构建RESTful API,同时也能与其他框架如Thymeleaf、Freemarker、Velocity等进行无缝集成,方便实现前后端分离的开发模式。 总之,Spring Boot是一个强大而简化的开发框架,它使得Spring应用程序的开发更加快速、高效。无论是开发小型项目还是大型复杂项目,都可以选择使用Spring Boot开发Web应用。 ### 回答3: Spring Boot是一个用于简化Spring应用程序开发的框架。它提供了一个快速开发的环境,并且具有自动化的配置,减少了开发人员的工作量。在使用Spring Boot开发Web应用程序时,可以遵循以下步骤: 1. 配置pom.xml:在项目的pom.xml文件中,添加Spring Boot相关的依赖项。通常包括Spring WebSpring Data JPA、Spring Security等。 2. 创建Controller:使用注解方式创建Controller类,处理来自客户端的请求。通过@RestController注解,可以将Controller类中的处理方法的返回值直接作为响应体返回给客户端。 3. 创建Service:在Service类中,编写业务逻辑代码。可以使用注解@Service将类标记为Service组件,并且使用@Autowired注解注入其他的依赖。 4. 创建Repository:在Repository类中,编写数据库操作的代码。可以使用注解@Repository将类标记为Repository组件,并且使用Spring Data JPA提供的接口来实现对数据库的操作。 5. 定义实体类:根据业务需求,定义与数据库表对应的实体类。实体类中使用注解@Entity标记类为实体类,并且使用注解@Id标记主键字段。 6. 配置数据库连接:在application.properties文件中,配置数据库连接信息,包括数据库URL、用户名、密码等。 7. 运行应用程序:通过main方法启动Spring Boot应用程序。Spring Boot会自动创建并配置好相应的运行环境,并且根据配置的端口号监听客户端的请求。 8. 测试应用程序:使用工具如Postman发送HTTP请求,测试Controller中定义的接口。可以通过URL访问接口,并且根据请求的参数和响应的结果,验证应用程序的正确性。 通过上述步骤,使用Spring Boot开发Web应用程序能够快速简便地搭建一个可靠的系统。同时,Spring Boot还提供了自动化的配置和部署功能,让开发人员能够专注于业务逻辑的开发,而不需要关心复杂的配置。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值