Web.servlet.PageNotFound - 没有 GET 映射

Web.servlet.PageNotFound - No mapping for GET

提问人:Scripta14 提问时间:4/17/2020 最后编辑:Scripta14 更新时间:10/3/2023 访问量:8702

问:

我正在学习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/**

enter image description here

这是我的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>
spring-boot spring-mvc jsp

评论

0赞 Anish B. 4/18/2020
你能添加你的配置类吗
0赞 Scripta14 4/21/2020
对不起,你指的是哪个配置类?在我的示例中,我只有 hellocontroller 类、application.properties 和带有@SpringBootApplication注释的主类、我把它放在帖子中的 jsp 页面,没有其他内容

答:

0赞 chahir chalwati 9/16/2020 #1

使用这个

@Configuration
@EnableWebMvc
public class MvcConfig implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**")
          .addResourceLocations("/resources/"); 
    }
}