我有调度图像的问题,通过数据库中的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();
}
}
}
}
my faces-config
<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 =#就可以了,我可能需要进行一些偏好更改。
答案 0 :(得分:5)
如果<h:graphicImage value>
不是以方案或/
开头,那么它与当前请求网址相关。想象一下,您正在按http://localhost:8080/ClothesJSF/faces/page.xhtml
打开该页面,并且该页面有一个
<h:graphicImage value="image?id=1" />
然后JSF将生成
<img src="image?id=1" />
实际上与http://localhost:8080/ClothesJSF/faces/
相关,web浏览器将尝试从http://localhost:8080/ClothesJSF/faces/image?id=1
下载实际图像,因此只会出错。如果您已经注意到webbrowser的内置HTTP流量监视器,那么您已经注意到了。
您需要让它以/
开头,使其相对于上下文路径。
<h:graphicImage value="/image?id=1" />
这样JSF就会生成
<img src="/ClothesJSF/image?id=1" />
哪个是对的。请注意,#{request.contextPath}
不是必需的。 <h:graphicImage>
已经透明地处理它。您只需要在“纯HTML”资源元素中使用它,例如<a>
,<img>
,<link>
,<iframe>
等。