【发布时间】:2020-07-30 23:54:41
【问题描述】:
我正在学习 Spring boot + MVC 的工作原理。我可以在屏幕上显示一条消息,但我不能修改样式。 js e css 文件不与 Spring 映射。
2020-04-17 14:38:29.169 WARN 9552 --- [nio-8080-exec-3] o.s.web.servlet.PageNotFound : No mapping for GET /js/main.js
2020-04-17 14:38:29.169 WARN 9552 --- [nio-8080-exec-2] o.s.web.servlet.PageNotFound : No mapping for GET /css/main.css
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class HelloController {
@GetMapping("/")
public String index() {
return "index";
}
@PostMapping("/hello")
public String sayhello(@RequestParam("name") String name, Model model) {
model.addAttribute("name", name);
return "hello";
}
@GetMapping({"/helloworld", "/helloname"})
public String hello(Model model, @RequestParam(value="name", required=false, defaultValue="WORLD") String name) {
model.addAttribute("name", name);
return "helloname";
}
}
我尝试修改 application.properties 中的路径,但没有任何改变。
spring.mvc.view.prefix = /WEB-INF/view/
spring.mvc.view.suffix = .jsp
spring.mvc.static-path-pattern = /resources/**
这是我的jsp页面。当我运行这个页面时,我收到了我之前发布过的错误消息
<!DOCTYPE html>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html lang="en">
<head>
<link rel="stylesheet" type="text/css"
href="webjars/bootstrap/3.3.7/css/bootstrap.min.css" />
<c:url value="/css/main.css" var="jstlCss" />
<link href="${jstlCss}" rel="stylesheet" />
</head>
<body>
<div class="container">
<header>
<h1>Spring MVC + JSP + JPA + Spring Boot 2</h1>
</header>
<div class="starter-template">
<h1>Users List</h1>
<table
class="table table-striped table-hover table-condensed table-bordered">
<tr>
<th>Date</th>
<th>Device</th>
<!-- <th>Amount</th>
<th>OEE</th> -->
</tr>
<c:forEach items="${OEE}" var="oee">
<tr>
<!-- <td>${oee.oeeID.date}</td>
<td>${oee.oeeID.device}</td> -->
<td>${oee.amount}</td>
<td>${oee.oee}</td>
</tr>
</c:forEach>
</table>
</div>
</div>
<script type="text/javascript"
src="webjars/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</body>
</html>
【问题讨论】:
-
你能添加你的配置类吗
-
抱歉,您指的是哪个配置类?在我的示例中,我只有 hellocontroller 类、application.properties 和带有 @SpringBootApplication 注释的主类、我把它放在我的帖子中的 jsp 页面,没有别的
标签: spring-boot spring-mvc jsp