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/
点击链接
至此结束,希望能帮助到大家,