我在显示图像时遇到问题,该图像是通过 serlvet 从数据库加载的。我用这个类
public class ImageServlet extends HttpServlet {
private static final int DEFAULT_BUFFER_SIZE = 10240;
@EJB
private GoodsDAO goodsDAO;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String stringImageId = request.getParameter("id");
if (stringImageId == null) {
response.sendError(HttpServletResponse.SC_NOT_FOUND); // 404.
return;
}
int imageId = Integer.parseInt(stringImageId);
Goods goods = goodsDAO.find(imageId);
if (goods == null) {
response.sendError(HttpServletResponse.SC_NOT_FOUND); // 404.
return;
}
response.reset();
response.setBufferSize(DEFAULT_BUFFER_SIZE);
response.setContentType("image/jpeg");
response.setContentLength(goods.getImage().length);
response.setHeader("Expires", "Thu, 15 Apr 2010 20:00:00 GMT");
BufferedOutputStream output = null;
try {
output = new BufferedOutputStream(response.getOutputStream(), DEFAULT_BUFFER_SIZE);
output.write(goods.getImage());
} finally {
close(output);
}
}
private static void close(Closeable resource) {
if (resource != null) {
try {
resource.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
我的面孔配置
<faces-config xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
version="2.0">
<navigation-rule>
<navigation-case>
<from-outcome>listAllGoods</from-outcome>
<to-view-id>/pages/protected/user/listAllGoods.xhtml</to-view-id>
</navigation-case>
</navigation-rule>
<navigation-rule>
<navigation-case>
<from-outcome>createGoods</from-outcome>
<to-view-id>/pages/protected/admin/createGoods.xhtml</to-view-id>
</navigation-case>
</navigation-rule>
<navigation-rule>
<navigation-case>
<from-outcome>createOrder</from-outcome>
<to-view-id>/pages/protected/user/createOrder.xhtml</to-view-id>
</navigation-case>
</navigation-rule>
<application>
<resource-bundle>
<base-name>messages</base-name>
<var>msgs</var>
</resource-bundle>
</application>
我的 web.xml
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>ClothesJSF</display-name>
<welcome-file-list>
<welcome-file>pages/protected/user/listAllGoods.xhtml</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
<url-pattern>*.jsf</url-pattern>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>imageServlet</servlet-name>
<servlet-class>com.servlet.ImageServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>imageServlet</servlet-name>
<url-pattern>/image/*</url-pattern>
</servlet-mapping>
<filter>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
<init-param>
<param-name>thresholdSize</param-name>
<param-value>51200</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<servlet-name>Faces Servlet</servlet-name>
</filter-mapping>
我用这段代码加载我的图像:
<h:graphicImage id="zoomImage" value="image?id=#{goods.id}"
style="cursor:pointer" width="70" />
我在非主页上显示图像时遇到问题。当我打开页面时:
http://localhost:8080/ClothesJSF/
我所有的图像通常都是从数据库加载的。如果我打开页面
http://localhost:8080/ClothesJSF/pages/protected/user/listAllGoods.xhtml
等于我的主页我的图像不会被加载。此问题也出现在所有其他页面上。
我认为我的 image-servlet 设置有问题
<servlet-mapping>
<servlet-name>imageServlet</servlet-name>
<url-pattern>/image/*</url-pattern>
</servlet-mapping>
也许我需要 url-pattern 中的其他正则表达式,但我想不通。我很乐意接受任何建议。谢谢。
更新如果我将在所有图像中使用这样的路径:
<h:graphicImage id="zoomImage" value="http://localhost:8080/ClothesJSF/image?id=#
{goods.id}" style="cursor:pointer" width="70" />
它会起作用,但如果我使用 #{request.contextPath}/ 这将不起作用。如果我想要 #{request.contextPath}/image?id=# 可以工作,也许我需要进行一些偏好更改。