Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
118 views
in Technique[技术] by (71.8m points)

java - Spring Tomcat and static resources and mvc:resources

I started doing a web app from scratch. 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

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. My configuration, works with:

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

in web.xml and

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

</bean>

for the servlet context (plus other of course).

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. 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:

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/" />

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

<mvc:resources> plays well with annotated controllers, but may require some extra configuration with other kinds of controller mappings.

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):

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...