2016-04-13 135 views
1

我不知道如何將我的CSS鏈接到一個js文件在DispatcherServlet中沒有找到具有URI的HTTP請求的映射名稱

請healp。我只得到這個請求: 沒有找到映射與HTTP請求的URI [/LiblaryProject/WebContent/js/script.js在DispatcherServlet的名爲 '庫'

庫servlet.xml中:

<?xml version="1.0" encoding="UTF-8"?> 
    <beans xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns="http://www.springframework.org/schema/beans" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd 
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> 

    <context:component-scan base-package="com.myliblary.*" /> 

    <bean id="UserDAO" class="com.myliblary.dao.UserDAOImpl"> 
     <constructor-arg> 
      <ref bean="sessionFactory" /> 
     </constructor-arg> 
    </bean> 
    <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"> 
     <property name="driverClassName" value="com.mysql.jdbc.Driver" /> 
     <property name="url" value="jdbc:mysql://localhost:3306/liblarydb" /> 
     <property name="username" value="root" /> 
     <property name="password" value="" /> 
    </bean> 

    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
     <property name="dataSource" ref="dataSource" /> 
     <property name="configLocation" value="classpath:hibernate.cfg.xml" /> 
    </bean> 

    <tx:annotation-driven /> 
    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 
      <property name="sessionFactory" ref="sessionFactory" /> 
    </bean> 

    <mvc:annotation-driven/> 

    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="prefix" value="/Pages/"></property> 
     <property name="suffix" value=".jsp"></property> 
    </bean> 

</beans> 

的web.xml:

<?xml version="1.0" encoding="UTF-8"?> 

<web-app version="2.4" 
     xmlns="http://java.sun.com/xml/ns/j2ee" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
     http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" > 

    <servlet> 
    <servlet-name>library</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <load-on-startup>1</load-on-startup> 
    </servlet> 

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

</web-app> 

,在我回到Home.jsp我喜歡成才:

<script src="<c:url value="WebContent/js/script.js" />"></script> 
    <link href="/css/custom.css" rel="stylesheet" type="text/css" /> 

回答

0

通過使用url-pattern /你已經覆蓋了容器的默認servlet。這是從Web應用程序的根目錄提供靜態內容的servlet。爲了保持你的映射,你需要配置Spring默認的servlet處理程序。這是如何工作的,現在通過Spring而不是默認的servlet來請求靜態資源。 Spring現在將根據容器類型查找容器defualt servlet。爲了實現這一目標將下面一行添加到您的應用程序配置文件

<mvc:defualt-servlet-handler/> 
0

在你的控制器接受請求,如「/」,因爲在你的web.xml這種方式。嘗試改變此:

實施例:

WEB.XML:

<servlet-mapping> 
    <servlet-name>library</servlet-name> 
    <url-pattern>.*htm</url-pattern> 
    </servlet-mapping> 
相關問題