0
我需要將數據從Servlet(它從DAO獲得)傳遞給JSP。錯誤的數據在JSP中結束(來自Servlet的HTML代碼開銷)。它如何在那裏結束?如何正確處理這種情況?如何正確地將數據從Servlet傳遞到JSP?
的Servlet:
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
String reqId = request.getParameter("id");
Integer reqIdInt = null;
String tempHTML = "";
try {
reqIdInt = Integer.parseInt(reqId);
} catch (NumberFormatException e) { }
/**
* findById()
*/
if (reqIdInt != null) {
// s?id=.....
DSLR dslr = dslrDAO.findById(reqIdInt);
if(dslr != null){
tempHTML ="<form action=\"s?action=save\" method=\"POST\">\n" +
"\n" +
"<input type=\"hidden\" name=\"id\" value=\""+dslr.getDslrId()+"\">\n" +
"\n" +
"\n" +
"<table bgcolor=\"#000000\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tbody><tr><td><table border=\"0\" cellpadding=\"2\" cellspacing=\"1\">\n" +
"<tbody><tr bgcolor=\"#ffffff\"><td bgcolor=\"#cccccc\" nowrap=\"\">id:</td><td>"+dslr.getDslrId()+"</td></tr>\n" +
"<tr bgcolor=\"#ffffff\"><td bgcolor=\"#cccccc\" nowrap=\"\">model:</td><td> <b><font color=\"#0000ff\"><input type=\"text\" value=\""+dslr.getModel()+"\" name=\"model\"></font></b></td></tr>\n" +
"<tr bgcolor=\"#ffffff\"><td bgcolor=\"#cccccc\" nowrap=\"\">price:</td><td> <b><font color=\"#0000ff\"><input type=\"text\" value=\""+dslr.getPrice()+"\" name=\"price\"></font></b></td></tr>\n" +
"<tr bgcolor=\"#ffffff\"><td bgcolor=\"#cccccc\" nowrap=\"\">description:</td><td> <b><font color=\"#0000ff\"><textarea name=\"description\" cols=\"25\" rows=\"6\">"+dslr.getDescription()+"</font></b></td></tr>\n" +
"</tbody></table></td></tr></tbody></table>\n" +
"<input type=\"submit\" value=\"savedata\">\n" +
"</form>" +
"<tr><td>" + dslr.getDslrId() +
"</td><td>" + dslr.getModel() +
"</td><td>" + dslr.getPrice() +
"</td><td><a href=\"javascript:get_dslr(" + dslr.getDslrId() + ") target=\"_self\"\">" + dslr.getDescription() +
"</td><td><a href=\"" + "s?id=" + dslr.getDslrId() + "\">modify</a></td></tr>";
// request.getSession().setAttribute("generatedResponse",tempHTML);
request.setAttribute("generatedResponse",tempHTML);
request.getRequestDispatcher("/dslrs.jsp").forward(request, response);
}
}
JSP
...
<%=(String)request.getAttribute("generatedResponse")%>
...
瀏覽器:
UPDATE:
截圖示出了過度的HTML(其以某種方式從servlet或JSP拖動並放入description
文本字段,例如:</html>
)。爲了清晰起見,表單數據是錯誤的(黃色),我不需要自定義文本區域的字體。
UPDATE2:
我設法在Servlet來處理凌亂的代碼,但我還是不喜歡存放在HTML的Servlet。我想只有邏輯。我不想直接將HTML生成到JSP。在這個例子中如何從HTML中分離邏輯?
新的Servlet doGet方法:
String reqId = request.getParameter("id");
Integer reqIdInt = null;
String tempHTML = "";
try {
reqIdInt = Integer.parseInt(reqId);
} catch (NumberFormatException e) { }
/**
* findById()
*/
if (reqIdInt != null) {
// s?id=.....
DSLR dslr = dslrDAO.findById(reqIdInt);
if(dslr != null){
tempHTML = "<form action=\"s?action=save\" method=\"POST\">\n" +
"<input type=\"hidden\" name=\"id\" value=\""+dslr.getDslrId()+"\">\n" +
"\t<table class=\"table\" bgcolor=\"#000000\" border=\"0\">\n" +
"\t\t<tr>\n" +
"\t\t\t<td>id:</td>\n" +
"\t\t\t<td>"+dslr.getDslrId()+"</td>\n" +
"\t\t</tr>\n" +
"\t\t<tr>\n" +
"\t\t\t<td>model:</td>\n" +
"\t\t\t<td><input type=\"text\" name=\"dslr_model\" value=\""+dslr.getModel()+"\"></td>\n" +
"\t\t</tr>\n" +
"\t\t<tr>\n" +
"\t\t\t<td>price:</td>\n" +
"\t\t\t<td><input type=\"text\" name=\"dslr_price\" value=\""+dslr.getPrice()+"\"></td>\n" +
"\t\t</tr>\n" +
"\t\t<tr>\n" +
"\t\t\t<td>description:</td>\n" +
"\t\t\t<td><textarea name=\"dslr_description\" cols=\"30\" rows=\"10\">"+dslr.getDescription()+"</textarea></td>\n" +
"\t\t</tr>\n" +
"\t</table>\n" +
"</form>\n" +
"<input type=\"submit\" value=\"saveform\">" ;
request.getSession().setAttribute("generatedResponse",tempHTML);
request.setAttribute("generatedResponse",tempHTML);
request.getRequestDispatcher("/dslrs.jsp").forward(request, response);
}else {
// SQLException
}
/**
* findAll()
*/
} else {
List<DSLR> dslrs = dslrDAO.findAll();
if (dslrs != null) {
tempHTML = "<table class=\"table\" bgcolor=\"#000000\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">\n" +
" <tr bgcolor=\"#FFFFFF\" nowrap=\"\">\n" +
" <td>Id</td>\n" +
" <td>Model</td>\n" +
" <td>Price</td>\n" +
" </tr>\n" ;
for (Iterator<DSLR> dslrIterator = dslrs.iterator(); dslrIterator.hasNext();) {
DSLR next = dslrIterator.next();
tempHTML += "<tr><td>" + next.getDslrId() +
"</td><td>" + next.getModel() +
"</td><td>" + next.getPrice() +
"</td><td><a href=\"javascript:get_dslr(" + next.getDslrId() + ") target=\"_self\"\">description " +
"</td><td><a href=\"" + "s?id=" + next.getDslrId() + "\">modify</a></td></tr>";
}
tempHTML += "</table>";
request.getSession().setAttribute("generatedResponse",tempHTML);
request.setAttribute("generatedResponse",tempHTML);
request.getRequestDispatcher("/dslrs.jsp").forward(request, response);
}else {
//SQLException
}
}
新的JSP: ...
${generatedResponse}
...
鉻:
您可以使用[EL(http://docs.oracle.com/javaee/1.4/tutorial/doc/JSPIntro7.html)在JSP中避免的Scriptlet:'{ generatedResponse}' –
又是1999年嗎?當時我認爲將HTML和CSS放入servlet中已經被抹黑了。我把HTML放入Javascript中,只返回一個標記來隱藏/顯示jQuery。 – duffymo
下次使用搜索功能:) –