Kro*_*oko 5 ejb-3.0 jsf-2 jpa-2.0
我有调度图像的问题,通过数据库中的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();
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我的面孔配置
<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>
Run Code Online (Sandbox Code Playgroud)
我的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>
Run Code Online (Sandbox Code Playgroud)
我用这段代码加载我的图片:
<h:graphicImage id="zoomImage" value="image?id=#{goods.id}"
style="cursor:pointer" width="70" />
Run Code Online (Sandbox Code Playgroud)
我在非主页上显示图像时遇到问题.当我打开页面时:
http://localhost:8080/ClothesJSF/
Run Code Online (Sandbox Code Playgroud)
我的所有图像通常是从数据库加载的.如果我打开页面
http://localhost:8080/ClothesJSF/pages/protected/user/listAllGoods.xhtml
Run Code Online (Sandbox Code Playgroud)
等于我的主页我的图像将不会被加载.所有其他页面上也出现此问题.
我认为我的image-servlet设置有问题
<servlet-mapping>
<servlet-name>imageServlet</servlet-name>
<url-pattern>/image/*</url-pattern>
</servlet-mapping>
Run Code Online (Sandbox Code Playgroud)
也许我需要在url-pattern中使用其他正则表达式,但我无法弄明白.我会很高兴任何建议.谢谢.
更新如果我将在所有图像中使用这样的路径:
<h:graphicImage id="zoomImage" value="http://localhost:8080/ClothesJSF/image?id=#
{goods.id}" style="cursor:pointer" width="70" />
Run Code Online (Sandbox Code Playgroud)
它会工作,但如果我将使用#{request.contextPath} /这将无法正常工作.如果我想要#{request.contextPath}/image?id =#会工作,我可能需要做一些偏好更改.
如果<h:graphicImage value>
开头不是方案或/
,则它相对于当前请求URL。想象一下,您正在按打开页面,http://localhost:8080/ClothesJSF/faces/page.xhtml
并且相关页面上有一个
<h:graphicImage value="image?id=1" />
Run Code Online (Sandbox Code Playgroud)
然后,JSF将生成一个
<img src="image?id=1" />
Run Code Online (Sandbox Code Playgroud)
它实际上相对于http://localhost:8080/ClothesJSF/faces/
Web浏览器而言,并且Web浏览器将尝试从下载实际的图像http://localhost:8080/ClothesJSF/faces/image?id=1
,从而最终导致错误。如果您已经关注了webbrowser的内置HTTP流量监视程序,则您会注意到这一点。
您需要让它开始/
使其相对于上下文路径。
<h:graphicImage value="/image?id=1" />
Run Code Online (Sandbox Code Playgroud)
JSF将通过这种方式生成
<img src="/ClothesJSF/image?id=1" />
Run Code Online (Sandbox Code Playgroud)
哪个是对的。请注意,#{request.contextPath}
没有必要。在<h:graphicImage>
已经照顾它透明。您只需要在“纯HTML”资源元素,例如<a>
,<img>
,<link>
,<iframe>
,等。
归档时间: |
|
查看次数: |
2788 次 |
最近记录: |