CSS和JS文件的映射

时间:2013-01-04 10:22:47

标签: java spring java-ee spring-mvc

我的项目适用于" /"图案。但是当我连接js和css时它不能正常工作,导致调度程序servlet不映射css和js。当我指定" .htm"模式css和js工作,但我的所有页面(例如" /民意调查/类别")都不起作用。 这是我的web.xml文件。

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

and 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>

1 个答案:

答案 0 :(得分:0)

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

如果这没有帮助,为什么这样做很难。下载弹簧工具套件(易于谷歌)。使用此工具,您只需创建新的弹簧项目,该工具将创建具有有效配置的适当结构。并检查这一个,非常简短和简洁:

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