Java CSS和JS文件的映射
我的项目使用“/”模式运行良好。但当我连接js和css时,它并没有像我所希望的那样工作,因为DispatcherServlet并没有映射css和js。当我指定“.htm”模式时,css和js可以工作,但我的所有页面(例如“/polls/categories”)都不能工作。 这是我的web.xml文件Java CSS和JS文件的映射,java,spring,jakarta-ee,spring-mvc,Java,Spring,Jakarta Ee,Spring Mvc,我的项目使用“/”模式运行良好。但当我连接js和css时,它并没有像我所希望的那样工作,因为DispatcherServlet并没有映射css和js。当我指定“.htm”模式时,css和js可以工作,但我的所有页面(例如“/polls/categories”)都不能工作。 这是我的web.xml文件 <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
调度员
/
和调度程序servlet:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" p:order="1"/>
</beans>
将此位添加到dispatcher-servlet.xml:
<resources mapping="/resources/**" location="/resources/" />
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xsi:schemaLocation="
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/jsp/" />
<beans:property name="suffix" value=".jsp" />
<beans:property name="order" value="1" />
</beans:bean>
</beans:beans>
然后从视图中访问它们,如下所示(css示例):
更新2
如果这没有帮助,为什么要把事情做得很艰难呢。下载spring工具套件(谷歌很容易找到)。使用此工具,您可以创建新的spring项目,该工具将使用有效配置创建适当的结构。看看这个,非常简短:
不清楚问题是什么。我认为您希望将web资源排除在映射之外:/I将文件放在资源文件夹中,但Tomcat生成错误“配置问题:无法找到元素[resources]的BeanDefinitionParser”@难以置信\u titan如果有帮助,您也可以接受答案
<link rel="stylesheet" href="<c:url value="${webappRoot}/resources/css/style.css" />" type="text/css">
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xsi:schemaLocation="
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/jsp/" />
<beans:property name="suffix" value=".jsp" />
<beans:property name="order" value="1" />
</beans:bean>
</beans:beans>
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
@Controller
class ControllerClassNameHandlerMapping{
....body omitted..
}