无法在WEB-INF文件夹外显示图像

时间:2019-09-22 14:42:08

标签: jsp servlets web-inf

我有一个文件夹 结构文件夹:
structure folder

我想在img文件夹中的index.jsp上显示图像。但是不会显示

到目前为止,我尝试过

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<jsp:include page="includes/header.jsp" />

    <h2 class="page-header">Home</h2>
    <base href="${pageContext.request.contextPath}/" />


        <li><a href="${pageContext.request}/about">Home</a></li>
        <a href="../../resources/img/1.png">
   <img src="/resources/img/1.png" alt="logo">
   <img src="WEB-INF/views/img/1.png" alt="studenst">
<img src="${pageContext.request.contextPath}/resources/img/1.png" />
<img src="#{request.contextPath}/resources/img/1.png" />
<h:graphicImage value="/resources/img/1.png" />

这是我简单的控制器。

public class HomeServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public HomeServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String path = request.getServletPath();

        RequestDispatcher dispatcher = this.getServletContext().getRequestDispatcher("/WEB-INF/views/index.jsp");

        dispatcher.forward(request, response);
    }

为什么它不起作用?

如果我将index.jsp放在web-inf文件夹(位于Web内容中)之外,则它可以工作。但是我想将index.jsp放在web-inf内。

2 个答案:

答案 0 :(得分:0)

复制1.png的目录路径,然后粘贴到src中(您要在其中提供图像)

答案 1 :(得分:0)

尝试 <img src="<c:url value="/resources/img/1.png" />" />