带有Image Servlet配置的Spring DispatcherServlet

时间:2011-02-27 21:25:33

标签: java spring servlets spring-mvc

我正在尝试设置我的应用程序以使用Spring以及一个服务于servlet的图像。我查看了其他帖子并找到了类似的主题,但似乎无法使我的配置正常工作。我使用Spring 3.0并使用tomcat 7作为我的app服务器。这是我的配置:

<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring/app-context.xml</param-value>
</context-param>

<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- Processes application requests -->
<servlet>
    <servlet-name>viewer</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet>
    <servlet-name>imageServlet</servlet-name>
    <servlet-class>com.billy.viewer.servlet.ImageServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/imageServlet/imgServlet-context.xml</param-value>
    </init-param>       
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>viewer</servlet-name>
    <url-pattern>/web/*</url-pattern>
</servlet-mapping>

<servlet-mapping>
    <servlet-name>imageServlet</servlet-name>
    <url-pattern>/images/*</url-pattern>
</servlet-mapping>

<welcome-file-list>
    <welcome-file>list.jsp</welcome-file>
</welcome-file-list>

servlet的context.xml中

<!-- 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/views/" />
    <beans:property name="suffix" value=".jsp" />
</beans:bean>

<!-- Imports user-defined @Controller beans that process client requests -->
<beans:import resource="controllers.xml" />

<beans:bean id="imageService" class="com.billy.viewer.service.ImageService"></beans:bean>

displayImage.jsp在正文中包含了这个

<img alt="Test" src="/images/${image.imageId}">

ImageServlet

public class ImageServlet extends HttpServlet {

private static final int DEFAULT_BUFFER_SIZE = 10240;

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    int imageId = Integer.valueOf(request.getPathInfo().substring(1));

    System.out.println("Image id: "+imageId);
}

}

我的页面很好,我可以看到img标签上方的其他输出,但图像永远不会出现。我没有从servelt看到我的system.out.println的Image id。我的控制台中没有错误消息。

任何建议将不胜感激。感谢

2 个答案:

答案 0 :(得分:2)

一切似乎都很好。用firebug检查图像请求的实际响应是什么(或尝试用浏览器打开它)。

我觉得很奇怪你有imgServlet-context.xml

顺便说一句,没有必要使用servlet来执行此操作。您可以安全地将调度程序servlet映射到/*,并使用弹簧控制器来处理@RequestMapping("/images/{id}")

当您指定网址时,最好使用src="<c:url value="/images/${..}" />" - 这将解析相对于当前上下文的网址。另一种选择是使用相对路径 - 即src="images/${..}"

答案 1 :(得分:1)

将标准标记库中的c:url标记用于img和其他URL,这些URL应该是您的Web应用程序上下文相关的。您使用“/images/${image.imageId}”指定的URL,因为前导“/”指向根上下文或/ images上下文而不是您的Web应用程序上下文,并且仅当您将Web应用程序部署为根上下文时它才有效。使用c:url它将始终有效,当app是root并且它是非root上下文时。