springboot访问html、jsp、ftl访问方式

1、html

情况一:走后端代码,直接资源访问html放置所在图如下:
在这里插入图片描述
方式一:放在src/main/resource/static下,注意一定是static包,不能随便取文件夹名
方式二:放在main/webapp下
启动项目后,访问路径如下:
1.html:localhost:8080/1html/1.html
2.html:localhost:8080/2.html
3.html:localhost:8080/3html/3.html
4.html:localhost:8080/4.html

情况二:走后端代码,访问html方法
第一步:pom.文件增加一个依赖:

	<!--引入thymeleaf的依赖 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-thymeleaf</artifactId>
		</dependency>

第二步:application.properties配置

##############################################
#
# thymeleaf配置
#
##############################################
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
spring.thymeleaf.cache=false

第三步:新建一个html文件,放在/resource/templates/helloRes.html下,取名即为helloRes.html
在这里插入图片描述
第四步:编写controller类,return的string为html的文件名

package com.yit1.demo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
 
  @Controller
  public class HelloWorld2{
	

	@RequestMapping("/getDemohtml")
	public String getDemo() {
		
		return "helloRes";
	}
	
}

jsp文件的访问路径为:localhost:8080/getDemohtml

第一步:pom.文件增加两个依赖:

<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>
			<scope>provided</scope>
		</dependency>

第二步:application.properties增加前缀和后缀配置

spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp

第三步:新建一个jsp文件,放在/WEB-INF/jsp/下,取名若为hello1.jsp
在这里插入图片描述
第四步:编写controller类,return的string为jsp的文件名

import java.util.Map;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HelloController {
	@RequestMapping("/getHello")
	public String getHello(Map< String, String> map){
		map.put("name", "yit6661");
		map.put("liuweiage", "25");
		return "hello1";
	}
}

jsp文件的访问路径为:localhost:8080/getHello

3、ftl

略,以后有机会加

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值