【问题标题】:Mapping of CSS and JS filesCSS和JS文件的映射
【发布时间】:2013-01-04 10:22:47
【问题描述】:

我的项目适用于“/”模式。但是当我连接 js 和 css 时,它不能正常工作,因为调度程序 servlet 没有映射 css 和 js。当我指定“.htm”模式 css 和 js 工作时,但我的所有页面(例如“/polls/categories”)都不起作用。 这是我的 web.xml 文件。

<servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
</servlet-mapping>

和 dispatcher-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>

【问题讨论】:

  • 不清楚问题是什么。
  • 我认为您希望将 Web 资源排除在映射之外:/

标签: java spring jakarta-ee spring-mvc


【解决方案1】:

将此位添加到您的 dispatcher-servlet.xml 中:

<resources mapping="/resources/**" location="/resources/" />

你应该把静态资源放在这个位置:

PROJECT_ROOT/src/main/webapp/resources

然后像这样从您的视图中访问它们(css 示例):

<link rel="stylesheet" href="<c:url value="${webappRoot}/resources/css/style.css" />" type="text/css">

答案更新(dispatcher-servlet.xml):

<?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..
}

更新 2

如果这没有帮助,为什么要这样做。下载 spring 工具套件(易于 google)。使用此工具,您只需创建新的 spring 项目,该工具将创建具有有效配置的适当结构。看看这个,真的很简短:

https://github.com/SpringSource/spring-mvc-showcase/blob/master/MasteringSpringMVC3.pdf?raw=true

【讨论】:

  • 我把文件放到资源文件夹,但是Tomcat报错“配置问题:找不到元素[资源]的BeanDefinitionParser”
  • @incredible_titan 如果有帮助,您也可以接受答案
猜你喜欢
  • 2016-04-29
  • 2018-05-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-29
  • 2016-02-19
  • 1970-01-01
  • 2012-08-11
相关资源
最近更新 更多