【问题标题】:JSF Image servlet doesn't load imageJSF Image servlet 不加载图像
【发布时间】:2013-07-28 06:39:00
【问题描述】:

我在显示图像时遇到问题,该图像是通过 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=# 可以工作,也许我需要进行一些偏好更改。

【问题讨论】:

    标签: jsf-2 jpa-2.0 ejb-3.0


    【解决方案1】:

    如果&lt;h:graphicImage value&gt; 不以方案或/ 开头,则它与当前请求 URL 相关。想象一下,您正在通过http://localhost:8080/ClothesJSF/faces/page.xhtml 打开页面,而相关页面有一个

    <h:graphicImage value="image?id=1" />
    

    然后JSF会生成一个

    <img src="image?id=1" />
    

    它实际上与http://localhost:8080/ClothesJSF/faces/ 相关,并且网络浏览器将尝试从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} 不是必需的。 &lt;h:graphicImage&gt; 已经透明地处理了它。您只需要在“纯 HTML”资源元素中使用它,例如 &lt;a&gt;&lt;img&gt;&lt;link&gt;&lt;iframe&gt; 等。

    【讨论】:

    • 谢谢你,它的工作!我在 JSF、JPA 等方面工作只有 2 周,但我已经阅读了你的几十篇文章,你节省了我很多时间。你做得很好,非常感谢你。 PS当我写这篇文章时,我想你可能会回答它:D。 PPS 对不起我的英语
    猜你喜欢
    • 2012-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-04
    • 1970-01-01
    • 1970-01-01
    • 2020-07-13
    • 1970-01-01
    相关资源
    最近更新 更多