javascript - 无法使用 Spring Boot REST API、Thymeleaf 和 JS 获取显示文件的原始内容
问题描述
我有一个Spring Boot
应用程序,它在加载主页 ( http://localhost:8080
) 时成功显示浏览器上特定目录中的所有文件名,没有任何问题。
现在,我想让这些文件名成为一个超链接,单击它会在浏览器上显示该文件的内容。
这样做时出现JS
错误。该请求甚至没有到达服务器。我调用函数并在其中传递文件名参数的方式有问题吗?
错误:
VM164:1 Uncaught ReferenceError: database.properties is not defined
at <anonymous>:1:17
家庭控制器.java
@Controller
public class HomeController {
@GetMapping({"", "/", "/home"})
public String index(Model model) {
return "home";
}
}
目录列表控制器.java
@RestController
public class DirListController {
@Autowired
private SshConnService sshConnService;
@GetMapping("/api/dir")
public List<String> showAllDirs(Model model) throws Exception {
return sshConnService.listAllFiles(); //returns [database.properties, messaging.properties, ....]
}
}
RawFileContentController.java
@RestController
public class RawFileContentController {
@Autowired
private SshConnService sshConnService;
@GetMapping("/api/rawFileContent")
public String showRawFileContent(@RequestParam(value = "filename") String filename) throws Exception {
return sshConnService.catFile(filename);
}
}
主页.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Directory List</title>
</head>
<body>
<h1>Directory List</h1>
<table id="categoryTable" class="table" style="margin-top:10px;">
<thead>
<tr>
<th>Name</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
<h2>File Contents</h2>
<table id="contentTable" class="table" style="margin-top:10px;">
<thead>
<tr>
<th>File Content</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
<script src="/webjars/jquery/3.0.0/jquery.min.js"></script>
<script src="/js/main.js"></script>
</body>
</html>
js/main.js
$(document).ready(function () {
viewAllDirs();
});
/*** Show all files within a directory **/
async function viewAllDirs() {
$('#categoryTable tbody').empty();
const dirResponse = await dirService.findAll();
const dirJson = dirResponse.json();
dirJson.then(filename => {
filename.forEach(filename => {
console.log(filename);
//make the filename clickable and display it's content onclick
let categoryRow = `$(<tr><td><a href="javascript:viewFileContent(${filename})">${filename}</a></td></tr>)`;
$('#categoryTable tbody').append(categoryRow);
});
});
}
const dirService = {
findAll: async () => {
return await fetch('/api/dir');
}
};
/*** View File Contents **/
async function viewFileContent(filename) {
$('#contentTable tbody').empty();
const rawFileContentResponse = await rawFileContentService.findByFilename(filename);
const rawFileContentResponseJson = rawFileContentResponse.json();
console.log(rawFileContentResponseJson);
let contentRow = `$(<tr><td>${rawFileContentResponseJson}</td></tr>)`;
$('#contentTable tbody').append(contentRow);
}
const rawFileContentService = {
findByFilename: async (filename) => {
return await fetch('/api/rawFileContent?filename=' + filename);
}
};
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>4.1.3</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>font-awesome</artifactId>
<version>5.8.2</version>
</dependency>
解决方案
推荐阅读
- tomcat - Tomcat Catalina 日志文件自定义
- java - 来自 REST 的 @DateTimeFormat 变量给出 null
- javascript - 如何提取 div 文本以供以后在 Cypress 中使用?
- javascript - 如果子项在垂直列表中处于活动状态,则保持导航菜单打开
- django - Django & intercooler.js : 使用 Intercooler.js 将变量传递给视图
- amazon-web-services - 无法制作 S3 对象,使用 Lambda 从另一个账户上的 S3 存储桶复制,公共
- javascript - 我正在尝试将 JSON 对象发布到 ASP.NET MVC 控制器但得到空 JSON
- python - SMTPAuthenticationError: (535, b'5.7.8 用户名和密码不被接受。了解详情\n5.7.8 https://support.google
- magento - Magento 1.9:获取产品 X 是追加销售的产品
- exception - 为什么要在异常中使用多个捕获?