Tôi đang sử dụng Spring 3.0.1.RELEASE cho webapp của tôi (và tôi không có cách nào để nâng cấp nó) và tôi đang cố gắng hiển thị một số hình ảnh từ cơ sở dữ liệu trên trang web.Làm cách nào để trả lại hình ảnh đúng cách trong phản hồi trong ứng dụng Mùa xuân?
Tôi đã sau configs mùa xuân đơn giản:
mùa xuân-application.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:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">
<task:annotation-driven />
<context:annotation-config />
<context:spring-configured />
<context:component-scan base-package="com.me" />
<bean id="hibernateSessionFactory" class="com.me.dbaccess.HibernateSessionFactory">
<constructor-arg ref="sessionFactory"/>
</bean>
</beans>
mùa xuân-mvc.xml:
<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<mvc:annotation-driven/>
<bean id="tilesViewResolver" class="com.me.util.TilesExposingBeansViewResolver">
<property name="viewClass" value="com.me.util.TilesExposingBeansView"/>
<property name="exposeContextBeansAsAttributes" value="true"/>
</bean>
<bean id="tilesConfigurer"
class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/config/tiles-defs.xml</value>
</list>
</property>
</bean>
</beans>
Tôi đã sau điều khiển:
@Controller
public class PhotoController {
@RequestMapping("/carPhoto.html")
@ResponseBody
public byte[] getCarPhoto(
@RequestParam(UrlParameters.PHOTO_ID) Integer photoId,
@RequestParam(UrlParameters.PHOTO_TYPE) String photoType) {
//return image's bytes array from db by photo Id and Type;
}
}
Và cuối cùng, tôi có trang jsp đơn giản:
<%@page contentType="text/html; charset=utf-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<img id="photoImage" src="<c:url value="/carPhoto.html?photoType=1&photoId=22556793"/>" />
Nếu tôi mở trang này - Tôi có thể thấy hình ảnh này mà không gặp bất kỳ sự cố nào.
Nhưng nếu tôi sao chép thuộc tính "src" hình ảnh và dán vào thanh địa chỉ của trình duyệt (Firefox 19.0.2) - thì trình duyệt cung cấp cho tôi để lưu carPhoto.html, thay vì chỉ hiển thị hình ảnh. Tôi có nên thực hiện một số thiết lập bổ sung không?
là vấn đề thie giải quyết, nếu có, xin chia sẻ như thế nào, tôi đang phải đối mặt với cùng một vấn đề – Abhilash