【问题标题】:Display images stored in Spring MVc显示存储在 Spring MVc 中的图像
【发布时间】:2015-10-06 06:18:27
【问题描述】:

我上传了一些图片,并将其保存到此路径中的目录中: C:\Users\user\Inoesis Spring Projects.metadata.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\HDTS/photos。

 I have uploaded some images and it got saved into a directory in this path:
C:\Users\user\Inoesis Spring Projects\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\HDTS/photos. Handler method in the Controller:


       @RequestMapping(value={"/", "home"})
        public ModelAndView showIndex(){
            return new ModelAndView("index", "artistList", artistService.getArtistsByLikes());
        }

view : index.jsp

    <c:if test="${not empty artistList }">
            <c:forEach  items="${artistList }" var="artistInfo">
            <div class="col-md-4">
                <div class="panel panel-default">
                        <div class="panel-heading">
                            <h3 class="panel-title"> <span class="glyphicon glyphicon-user"></span> <c:out value="${ artistInfo.stageName}"/> </h3>
                        </div>
                        <div class="panel-body">
                            <img alt="" src="<%=request.getContextPath()%>/photos/${artistInfo.photoName}">
                        </div><!-- /.panel-body -->
                        </div><!-- /.panel panel-primary -->

            </div><!-- /.col-md-4 -->
            </c:forEach>

            </c:if>


Pls note my project is HDTS.
How do i display them on a jsp page.
thanks

请注意我的项目是 HDTS。 我如何在 jsp 页面上显示它们。 谢谢

【问题讨论】:

    标签: spring jsp


    【解决方案1】:

    您可以将上传的图片名称发送到请求的页面,并使用HttpServletRequest中定义的getContextPath()获取上传图片的完整URL。

    &lt;%=request.getContextPath()%&gt; 将提供 /wtpwebapps/HDTS

    所以你的&lt;img /&gt; 应该是这样的:

    &lt;img src="&lt;%=request.getContextPath()%&gt;/photos/${image_name}"/&gt;

    希望这会有所帮助。

    编码愉快!干杯!!

    【讨论】:

    • 我已经加入了一些sn-ps。
    • 您能否调试并检查为 ${artistInfo.photoName} 填充了什么值。同时关闭 标签。
    • 我调试了,它显示了确切的图像名称,只有部分没有找到
    • 您应该明确指定请求的图像扩展名。像 jpeg、png 等 image_name.extension 之类的东西。带有扩展名的完整文件名。
    • 是的。我将文件名作为字符串保存到表中,因此不需要扩展名。挑战在于如何指定文件路径。如果它是硬编码的,一旦应用程序部署到生产环境,它可能会中断。有什么办法解决这个问题?
    猜你喜欢
    • 2011-08-28
    • 1970-01-01
    • 2016-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-20
    相关资源
    最近更新 更多