Jsp Img标记无法加载资源:服务器响应状态为404(未找到)

时间:2016-12-08 09:51:22

标签: java html spring jsp

我是Jsp的新手,我正在使用Spring MVC并尝试在我的jsp页面中显示徽标,但是我收到了这样的错误

Failed to load resource: the server responded with a status of 404 (Not Found)

我的jsp看起来喜欢这个

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="s"%>
<html>
    <head>
    </head>
    <body style="background-color: wheat;">
        <div align="center">
            <s:form commandName="salarySlipCommandName">
                <img src="/images/logo.png" />
            </s:form>   
        </div>
    </body>
</html>

我尝试删除“/”,如<img src="images/logo.png" />但无法解决问题。无法做什么可以请任何人在这方面帮助我..?

1 个答案:

答案 0 :(得分:0)

在jsp中使用::

<c:forEach items="${images}" var="img">
   <img src="showimage?path=${img}" width="100%" height="auto"/>
</c:forEach>

在java code ::

    @SuppressWarnings("resource")
        @RequestMapping(value="/showimage",method=RequestMethod.GET)
        public @ResponseBody void showImage(@RequestParam String path,HttpServletResponse response,HttpServletRequest request){
            //Get project location
            String paaath = request.getSession().getServletContext().getRealPath("\\");
            paaath = paaath.substring(0, paaath.length()-1);
            paaath = paaath.substring(0, paaath.lastIndexOf("\\"));
            String loc  = paaath+File.separator+"Projectfolder-DOCS"+File.separator+"images";
             try{
             FileInputStream fis = new FileInputStream(new File(loc+File.separator+path));
             BufferedInputStream bis = new BufferedInputStream(fis);  
             response.setContentType("image/gif");
             BufferedOutputStream output = new BufferedOutputStream(response.getOutputStream());
             for (int data; (data = bis.read()) > -1;) {
               output.write(data);
             }   
             }catch(Exception e){

             }
        }

在网络应用程序中仅从项目文件夹加载图像,确保您在正确的位置拥有所有这些图像....

希望这段代码有用......