gpt4 book ai didi

java - Spring MVC 无法加载 CSS

转载 作者:太空宇宙 更新时间:2023-11-04 10:58:48 27 4
gpt4 key购买 nike

我尝试创建简单的 spring mvc 元素,但我的index.jsp 不想加载CSS。我用谷歌搜索了这个问题并尝试了很多方法,但没有任何改变。

我的WebAppConfig:

@Configuration
@ComponentScan(basePackages = {"org.training.ytaranau"})
public class WebAppConfig extends WebMvcConfigurerAdapter {

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

@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}

@Bean
public Order order() {
return new Order();
}

@Bean
public InternalResourceViewResolver internalResourceViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/jsp/");
resolver.setSuffix(".jsp");
return resolver;
}
}

index.jsp 中的链接:

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

Webapp structure

最佳答案

问题出在 WebAppConfig.java

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>homework15</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<error-page>
<error-code>404</error-code>
<location>/jsp/errorPages/error404.jsp</location>
</error-page>
<error-page>
<error-code>401</error-code>
<location>/jsp/errorPages/error401.jsp</location>
</error-page>
<servlet>
<servlet-name>hello-dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-web-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>hello-dispatcher</servlet-name>
<url-pattern>/resources</url-pattern>
</servlet-mapping>
</web-app>

我用以下文件替换了WebAppConfig.java

/src/main/webapp/WEB-INF/spring-web-config.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">

<context:component-scan base-package="org.training.ytaranau" />

<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>

<mvc:resources mapping="/resources/**" location="/resources/" cache-period="31556926"/>


<mvc:annotation-driven />

</beans>

style.csshibernate.cfg.xmlerrorPageStyles.css 位于 /webapp/resources/ 目录中。

我在 error.jsp 中做了一些更改,以返回到 index.jsp 而不是 ../index.jsp

WebAppConfig.java 中存在一些问题,我不知道什么问题,但已通过 spring-web-config.xml 解决。

此外,我还注释了 WebInitializer.java 类,因为不需要它。

这是更新后的元素:

https://drive.google.com/open?id=1Keg3tyu1abVb90GLe9tlnFW-1N3xCszn

关于java - Spring MVC 无法加载 CSS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47120995/

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