繁体   English   中英

CSS和JS文件的映射

[英]Mapping of CSS and JS files

我的项目在使用“ /”模式时效果很好。 但是,当我连接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>

将此位添加到您的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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM