簡體   English   中英

Spring Tomcat和靜態資源以及mvc:資源

[英]Spring Tomcat and static resources and mvc:resources

我從頭開始做一個Web應用程序。 在我一直在處理已經運行了很長時間的應用程序之前,所以我沒有必要處理完整的設置階段。 我使用的是Spring 3和Tomcat 6,我正在使用Eclipse 3.6

我在提供圖像(或其他與控制器響應不同的事情)方面存在很大問題。 事實上,我無法找到一種方法將我的圖像放在我的jsps中。 我的配置,適用於:

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

在web.xml和

<bean name="/accise" class="it.jsoftware.jacciseweb.controllers.MainController">

</bean>

對於servlet上下文(當然還有其他)。

我在這里閱讀了很多消息,其他論壇也在談論這個:

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

但如果我在我的servlet-context.xml中插入它,我將能夠提供圖像,但控制器“accise”將無法訪問。 我誤用了還是誤解了資源標簽? 什么是正確的方法?


更新解決方案!!! :)

問題是我的servlet-config.xml錯過了一個聲明:

現在它是(使用控制器上的注釋):

<?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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    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-2.5.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
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">


    <context:component-scan base-package="it.jsoftware.jacciseweb.controllers"></context:component-scan>
    <mvc:annotation-driven />

    <bean
        class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />

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

<mvc:resources>可以使用帶注釋的控制器,但可能需要對其他類型的控制器映射進行額外配置。

我想在你的情況下你需要手動聲明BeanNameUrlHandlerMapping (它通常默認注冊,但<mvc:resources>覆蓋默認值作為應用自己的配置的副作用):

<bean class = "org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />

我有同樣的問題。 我能夠通過使用像。的完整路徑來解決這個問題

CSS

<link rel="stylesheet" type="text/css" media="screen" href="/my-web-app/resources/css/smoothness/jquery-ui-1.8.21.custom.css">

並為JavaScript

<script type="text/javascript" src="/my-web-app/static/js/jquery-1.4.4.min.js"></script?

讓我知道,如果在Spring提出一些更好的方法之前解決了你的問題。

我也有同樣的問題。 我通過在實際引用資源之前添加spring宏來解決它,以便解析servlet路徑。

它在代碼中並不是很好,但是這個解決方案使您獨立於在服務器和servlet上部署應用程序的實際上下文。 通常,您不希望在URL中提到實際的servlet。

例如,參考ColdStoneJava的答案,那將是:

  <script type="text/javascript" src="<@spring.url '/static/js/jquery-1.4.4.min.js'/>"></script>

你還必須在url中引用它絕對,所以斜杠'/ ...'是必不可少的。

這不符合我的經驗:

  <script type="text/javascript" src="<@spring.url 'static/js/jquery-1.4.4.min.js'/>"></script>

干杯。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM