简体   繁体   English

Spring Tomcat和静态资源以及mvc:资源

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

I started doing a web app from scratch. 我从头开始做一个Web应用程序。 Before I've always been working on apps that were already running for a long time, so I didn't have to deal with the full setup phase. 在我一直在处理已经运行了很长时间的应用程序之前,所以我没有必要处理完整的设置阶段。 I am using Spring 3 and Tomcat 6, and I am using Eclipse 3.6 我使用的是Spring 3和Tomcat 6,我正在使用Eclipse 3.6

I've a big problem with serving images (or other things different from controller responses). 我在提供图像(或其他与控制器响应不同的事情)方面存在很大问题。 In fact I can't find a way to have my images in my jsps. 事实上,我无法找到一种方法将我的图像放在我的jsps中。 My configuration, works with: 我的配置,适用于:

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

in web.xml and 在web.xml和

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

</bean>

for the servlet context (plus other of course). 对于servlet上下文(当然还有其他)。

I've read many messages here and other forums talking about this: 我在这里阅读了很多消息,其他论坛也在谈论这个:

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

but if I insert that in my servlet-context.xml, I will be able to serve images, yet the controller "accise" won't be reachable. 但如果我在我的servlet-context.xml中插入它,我将能够提供图像,但控制器“accise”将无法访问。 Am I misusing or I misunderstood the resources tag? 我误用了还是误解了资源标签? What's the correct way? 什么是正确的方法?


Update solution found!!! 更新解决方案!!! :) :)

The problem was that my servlet-config.xml missed one declaration: 问题是我的servlet-config.xml错过了一个声明:

Now it is(using annotations on the controller): 现在它是(使用控制器上的注释):

<?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> plays well with annotated controllers, but may require some extra configuration with other kinds of controller mappings. <mvc:resources>可以使用带注释的控制器,但可能需要对其他类型的控制器映射进行额外配置。

I guess in your case you need to declare BeanNameUrlHandlerMapping manually (it's usually registered by default, but <mvc:resources> overrides defaults as a side-effect of applying its own configuration): 我想在你的情况下你需要手动声明BeanNameUrlHandlerMapping (它通常默认注册,但<mvc:resources>覆盖默认值作为应用自己的配置的副作用):

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

I had the same problem. 我有同样的问题。 I was able to fix this by using the full path like for 我能够通过使用像。的完整路径来解决这个问题

CSS CSS

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

and for javascript 并为JavaScript

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

Let me know if this solves your problem before Spring comes up with some better approach. 让我知道,如果在Spring提出一些更好的方法之前解决了你的问题。

I had the same problem, too. 我也有同样的问题。 I solved it by adding the spring macro before the actual reference to the resource, in order to resolve the servlet path. 我通过在实际引用资源之前添加spring宏来解决它,以便解析servlet路径。

It is not really nice in the code, but this solution makes you being independent from the actual context under which your application is deployed on the server and your servlet too. 它在代码中并不是很好,但是这个解决方案使您独立于在服务器和servlet上部署应用程序的实际上下文。 Usually, you don't want to have the actual servlet being mentioned in your url. 通常,您不希望在URL中提到实际的servlet。

For instance, refering to the answer of ColdStoneJava, that would be: 例如,参考ColdStoneJava的答案,那将是:

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

You also have to reference it absolute in the url, so the slash '/...' is essential. 你还必须在url中引用它绝对,所以斜杠'/ ...'是必不可少的。

This, won't work in my experience: 这不符合我的经验:

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

Cheers. 干杯。

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

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