簡體   English   中英

轉發后未更新Jsp頁面

[英]Jsp page doesn't updated after forward

我有以下問題。 首先,我將請求從jsp發送到servlet,然后進行一些操作,並向請求中放入一些數據,然后轉發到同一頁面以呈現新數據。 但是轉發后,我的jsp頁面沒有更新。 誰能說我,我做錯了嗎?

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>File Viewer</title>
<link href="${pageContext.request.contextPath}/resources/css/bootstrap.min.css"      rel="stylesheet">
<script src="${pageContext.request.contextPath}/resources/js/bootstrap.min.js">    </script>
<script src="${pageContext.request.contextPath}/resources/js/jquery-1.8.0.min.js">    </script>
<script type="text/javascript">
    function showFolderRequest(fileName) {
        $.post( "ftp?fileName="+fileName, function( data ) {
        });
    }
</script>
</head>
<body>

<div class="container-fluid">
<div class="col-md-9  list-group" style="float: none; margin: 20px auto;">
<div class="list-group-item active" style="background-color: darkcyan;">
 Ftp server: /
</div>

<c:forEach items="${requestScope.files}" var="fileEntity">
<p class="list-group-item" onclick="showFolderRequest('${fileEntity.name}')">
            ${fileEntity.name}
        </p>
</c:forEach>
</div>
</div>
</body>
</html>

這是我的servlet

@WebServlet("/ftp")
public class FileServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        FileManager fileManager = FileManager.getInstance();
        String requestedFileName = req.getParameter("fileName");
        req.setAttribute("files", fileManager.getAllFilesByPath(requestedFileName));
        getServletContext().getRequestDispatcher("/test.jsp").forward(req, resp);
    }
}

問題是您正在觸發ajax請求。 在處理ajax請求時,您需要注意以下幾點:

  • 不能轉發不從servlet重定向。 這是來自瀏覽器的異步請求,不會從服務器的轉發或重定向中獲取任何內容。
  • 在處理JSP和呈現HTML時,表達式語言( ${}東西)和JSTL之類的JSP標簽在服務器端運行。 由於此代碼不在服務器上運行,因此任何ajax請求都不會更新任何EL或JSP標簽內容。 因此,在您觸發非ajax請求之前,您在此處設置的任何新屬性都無關緊要。
  • 您可以從服務器獲取任何數據的唯一方法是編寫包含所需數據的響應。 通常,您會編寫一個JSON響應,但是您甚至可以編寫XML或純文本響應,這取決於您的設計。 然后,在瀏覽器端(Javascript)中,您處理響應中的數據並顯示一些文本,更新當前HTML中的值,等等。

從這種情況下,您可以將所需文件的列表寫入JSON字符串,並將該字符串寫入服務器響應中,然后在客戶端進行相應的管理。 這只是如何使用Jackson庫將Java代碼轉換為JSON字符串的一個簡單示例。

在servlet中:

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    FileManager fileManager = FileManager.getInstance();
    String requestedFileName = req.getParameter("fileName");
    //not really sure what retrieves, I'm assuming it is a Lis<FileEntity>
    //edit this accordingly to your case
    List<FileEntity> files = fileManager.getAllFilesByPath(requestedFileName);
    String json = new ObjectMapper().writeValueAsString(files);
    response.setContentType("application/json");
    response.setCharacterEncoding("UTF-8");
    response.getWriter().write(json);
}

在JavaScript中:

<script type="text/javascript">
    function showFolderRequest(fileName) {
        $.post( "ftp?fileName="+fileName, function( data ) {
            //logs the whole JSON string response into browser console
            //supported in Chrome and Firefox+Firebug
            console.log(data);
            //parsing the JSON response
            var files = JSON && JSON.parse(data) || $.parseJSON(data);
            //since it is an array, you need to traverse the values
            for (var fileEntity in files) {
                //just logging the files names
                console.log(fileEntity.name);
            }
        });
    }
</script>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM