我想在使用表达式语言的 JSP 文件中打印 servlet 请求属性。我已经完成了以下操作,在我的 Servlet 中设置了所有必要的请求属性
setRequestAttributes(String, Object);
在我的 JSP 页面中,我使用以下行来打印属性:
<c:out value="${string}"/>
在阅读了其他帖子后,我检查了 Web XML 中是否使用了 2.4 版,是的。
When using getAttribute(String)
在 Servlet 本身中,它可以很好地打印该值。有谁知道我需要做什么,这样我才能完成这项工作。
编辑
小服务程序代码:
package servlets;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import classes.DBConnection;
public class MessageDetail extends HttpServlet {
private static final long serialVersionUID = 1L;
private String messageid;
private Connection connect;
public MessageDetail() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
connect = new DBConnection().returnConnection();
messageid = request.getParameter("xxxxx");
try {
handleRequest(request);
} catch (SQLException e) {
e.printStackTrace();
}
RequestDispatcher dispatcher = request.getRequestDispatcher("messageDetail.jsp");
dispatcher.forward(request, response);
}
private void handleRequest(HttpServletRequest request) throws SQLException{
ResultSet messageData = getMessageData("xxxxxx");
ResultSet attachmentData = getMessageData("xxxxxx");
Map<Integer, List<List<Object>>> messageMap = convertResultSetToMap(messageData);
Map<Integer, List<List<Object>>> attachmentMap = convertResultSetToMap(attachmentData);
Map<Integer, List<List<Object>>> totalMap = combineMaps(messageMap, attachmentMap);
setRequestAttributes(request, totalMap);
}
private ResultSet getMessageData(String tableName) throws SQLException{
String sql = "SELECT * FROM " + tableName + " WHERE xxxxxxxx= "+ xxxxxxx;
PreparedStatement prestm = connect.prepareStatement(sql, ResultSet.CONCUR_READ_ONLY, ResultSet.TYPE_SCROLL_SENSITIVE);
ResultSet rs = prestm.executeQuery();
return rs;
}
private Map<Integer, List<List<Object>>> convertResultSetToMap(ResultSet rs) throws SQLException{
Map <Integer, List<List<Object>>> resultSetMap = new HashMap<Integer, List<List<Object>>>();
ResultSetMetaData rsmd = rs.getMetaData();
int colTotal = rsmd.getColumnCount();
int rowNumber = 1;
rs.beforeFirst();
while(rs.next()){
List<List<Object>> row = new ArrayList<List<Object>>();
for(int colCounter = 1; colCounter<=colTotal; colCounter++){
List<Object> cellData = new ArrayList<Object>();
cellData.add(rsmd.getColumnName(colCounter));
cellData.add(rs.getObject(colCounter));
row.add(cellData);
}
resultSetMap.put(rowNumber, row);
}
return resultSetMap;
}
private void setRequestAttributes(HttpServletRequest request, Map<Integer, List<List<Object>>> resultSetMap){
for(Map.Entry<Integer, List<List<Object>>> entry : resultSetMap.entrySet()){
List<List<Object>> rowData = entry.getValue();
for(List<Object> listItem : rowData){
request.setAttribute((String) listItem.get(0), listItem.get(1));
}
}
}
private Map <Integer, List<List<Object>>> combineMaps(Map <Integer, List<List<Object>>> messageData, Map <Integer, List<List<Object>>> attachmentData){
Map <Integer, List<List<Object>>> totalMap = new HashMap<Integer, List<List<Object>>>();
totalMap.putAll(messageData);
totalMap.putAll(attachmentData);
return totalMap;
}
}
JSP代码:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<link href="/ArchiveSearch/resources/css/messageDetail.css" rel="stylesheet" type="text/css">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Message detail</title>
</head>
<body>
<div class="wrapper">
<h1>Message Details</h1>
<div class="messageMeta1">
<div><span class="label">Sender:</span><c:out value="${messageid}"/></div>
<div><span class="label">Receiver:</span></div>
<div><span class="label">Subject:</span></div><c:out value="${messagesender}"/>
<div><span class="label">Attachments:</span></div>
</div>
<div class="messageMeta2">
<div><span class="label">Case ID:</span></div>
<div><span class="label">Date:</span></div>
</div>
<div class="messageContent">
</div>
</div>
</body>
</html>