我有以下问题。首先,我将请求从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);
}
}
答案 0 :(得分:0)
问题在于您正在触发ajax请求。处理ajax请求时,您需要注意以下事项:
${}
中的那些内容)和JSP标记(如JSTL)在处理JSP和呈现HTML时在服务器端运行。任何ajax请求不会更新任何EL或任何JSP标记内容,因为此代码不在服务器上运行。因此,在您触发非ajax请求之前,您在此处设置的任何新属性都无关紧要。从这种情况下,您可以将所需文件的列表写入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>