SpringBoot(三):Web开发

1.静态资源的访问

    在实际的web开发中,需要引用大量的静态资源。而SpringBoot默认提供静态资源是放于classpath目录下,且具有如下命名规则/static,/public,/resource,/META-INF/resources。并且在我们直接访问静态资源时,不用带上面的路径,直接访问即可,如http://localhost:8080/static/csdn.jpg。

2.渲染Web页面

    SpringBoot提供了默认配置的模板引擎主要有Freemarker,Thymeleaf,Velocity,Groovy,Mustache。不推荐使用Jsp。使用模板引擎渲染页面默认放在src/main/resources/templates目录下,例如当使用Freemarker时具体操作如下

   2.1 引入依赖包

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.csdn</groupId>
	<artifactId>hello1</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<!-- SpringBoot项目 -->
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.3.3.RELEASE</version>
	</parent>
	<dependencies>
		<!-- SpringBoot web 组件 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<!-- Freemarker的依赖包  -->
		<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-freemarker</artifactId>
		</dependency>
	</dependencies>
</project>

     2.2 编写前端代码

<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8" />
<title>FreemarkerTest</title>
</head>
<body>
	  ${name}
</body> 
</html>

   2.3 编写后端代码

package com.csdn.start;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;

@ComponentScan(basePackages="com.csdn.controller")
@EnableAutoConfiguration
public class start {
	public static void main(String[] args) {
		SpringApplication.run(start.class, args);
	}
}
package com.csdn.controller;

import java.util.Map;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;

@Controller
public class threeController {

	@RequestMapping("/ftest")
	public String index(ModelMap map) {
	    map.addAttribute("name","Freemarker测试");
		return "freemarker";
	}
	
}

3.结果展示

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值