gpt4 book ai didi

java - 尝试获取 CSS 文件时出现 "Request method GET not supported"错误

转载 作者:太空宇宙 更新时间:2023-11-04 13:35:14 25 4
gpt4 key购买 nike

我有一个 JSP 文件,其中包含指向 <head> 中的 CSS 的链接。但是,我得到了 HTTP ERROR 405: Request method 'GET' not supported当我点击该链接时。

index.jsp:

<head>
<title>The Ultimate Address App</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link href="${pageContext.servletContext.contextPath}/resources/css/bootstrap.css" rel="stylesheet" type="text/css">
</head>

这是我的目录结构。

webapp
resources
css
bootstrap.css
WEB-INF
views
jsp
index.jsp

配置.java:

@EnableWebMvc
@Configuration
@ComponentScan({ "com.bpd.addressapp.controllers" })
public class Config extends WebMvcConfigurerAdapter {

private static final Logger LOGGER = Logger.getGlobal();

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}

@Bean
public InternalResourceViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setViewClass(JstlView.class);
viewResolver.setPrefix("/WEB-INF/views/jsp/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}

}

我没有使用任何 XML 配置文件,所以这里是 web.xml 的 Java 版本文件。

public class Initializer extends AbstractAnnotationConfigDispatcherServletInitializer {

@Override
protected Class<?>[] getRootConfigClasses() {
return Arrays.asList(Config.class).toArray(new Class[1]);
}

@Override
protected Class<?>[] getServletConfigClasses() {
return null;
}

@Override
protected String[] getServletMappings() {
return Arrays.asList("/").toArray(new String[1]);
}

}

在我的 Controller 中,我有这个方法:

@Controller
public class MainController {

@RequestMapping(value = "/", method = RequestMethod.GET)
public String index(Map<String, Object> model) {
...
return "index";
}

...

}

我怀疑 Controller 导致了这个问题。当我删除value = "/"时然后单击 HTML 源中 CSS 文件的链接,我被重定向回 index.jsp页。

如何解决此问题,以便 index.jsp页面能正确渲染吗?

最佳答案

尝试使用:

<link href="<c:url value="/resources/css/bootstrap.css"/>" rel="stylesheet" type="text/css">

并且不要忘记在 jsp 文件顶部包含 taglib:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

关于java - 尝试获取 CSS 文件时出现 "Request method GET not supported"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31767606/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com