我想在JSP文件中使用表达式语言打印servlet请求属性。 我已经完成了以下操作,在我的Servlet中,我用
设置了所有必要的请求属性setRequestAttributes(String, Object);
在我的JSP页面中,我使用以下行来打印属性:
<c:out value="${string}"/>
在阅读其他帖子后,我检查了在Web XML中是否使用了2.4版本,它是。
When using getAttribute(String)
在Servlet本身中,它打印的值很好。 有没有人知道我需要做什么,所以我可以做到这一点。
修改
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>
答案 0 :(得分:1)
这段代码中有太多错误可以给出一个合适的答案。基本上一切都需要重写。您可能会发现以下答案中的启动示例很有用:Show JDBC ResultSet in HTML in JSP page using MVC and DAO pattern。