0

我的项目适用于“/”模式。但是当我连接 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>

和调度员-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>
4

1 回答 1

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

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

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

于 2013-01-04T10:34:52.770 回答