基于Gradle的Spring boot 项目控制层访问HTML页面

Spring Boot通过Controller访问HTML页面教程
本文介绍了在Spring Boot项目中,如何从控制层跳转并访问HTML页面。涉及项目结构、配置文件、依赖管理、Controller代码以及HTML页面的设置。关键在于理解@Controller与@RestController注解的区别,并正确配置静态资源路径。

Spring boot 项目控制层访跳转到HTML,经过一系列的找寻终于跳过去了,直接上代码:项目中的static文件夹,好像是一种默认名字的结构,具体的大家去查一查,顺便也告诉我一下,谢谢。

1、项目结构

2、application.properties配置

spring.thymeleaf.prefix=classpath:/static/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=LEGACYHTML5

3、build.gradle依赖配置(最后两个是解析HTML需要的依赖这两个很重要,其他的依赖都是因为我这个项目需要,如果你的调试的时候出现依赖找不到,请百度自行解决)

dependencies {
    compile('org.springframework.boot:spring-boot-starter-web')
    compile('org.springframework.boot:spring-boot-starter-data-jpa')
    
    compile('com.alibaba:fastjson:1.2.40')
    
    compile('mysql:mysql-connector-java')
    compile('org.hibernate:hibernate-spatial:5.2.1.Final')
    
    compile('org.apache.poi:poi:3.17')
    compile('org.apache.poi:poi-ooxml:3.17')
    
    compile('org.apache.httpcomponents:httpclient:4.5.2')
    compile('org.apache.httpcomponents:httpmime:4.5.2')
    
    compile fileTree(dir: 'libs', includes: ['*.jar'])
    compile 'org.eclipse.persistence:org.eclipse.persistence.jpa:2.7.2'
    compile 'commons-lang:commons-lang:2.6'
    compile 'org.json:json:20090211'
    compile 'com.cedarsoft.commons:io:2.0.6'
    compile 'mysql:mysql-connector-java:5.1.23'
    testImplementation 'junit:junit:4.12'
    




    compile 'net.sourceforge.nekohtml:nekohtml:1.9.12'
    compile 'org.springframework.boot:spring-boot-starter-thymeleaf:1.5.9.RELEASE'
   
}

 

4、controller层

@RequestMapping("/test")
@Controller
public class MyController {
	
	@Autowired
	VideoRepository videoRepository;

	@RequestMapping("/video")
	public String getVideo(@RequestParam Integer id,Model model) {
		model.addAttribute("l", "如果你想传递值可以写这个");
		return  "video";
	}
}

controller层需要注意的是@Controller这个注解,如果你使用了@RestController那么次controller的返回的并不是你需要访问的页面名字,而是就是一个字符串,  所以要保证你返回是个页面需要用@Controller这个注解,如果有需要返回字符串,可以在单个方法上加上@ResponseBody这个注解,这样就可以返回字符串。

5、HTML页面

index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>

</head>
<body>
<h1>index.html</h1>
<a href="/test/video?id=1">跳转到vide.html</a>
</body>
</html>

video.html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
		<h1>Welcome video.html</h1>
</body>
</html>

6、启动类

@SpringBootApplication
public class Application {

    public static void main(String[] args) throws ParseException {
    	ConfigurableApplicationContext c = SpringApplication.run(Application.class, args);
}

7、启动服务器    访问url   :    

http://localhost:8081/

点击链接

至此结束,希望能帮助到大家,

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值