我需要将数据从Servlet(它从DAO获取)传递给JSP。错误的数据最终在JSP中(来自Servlet的html代码的开销)。它到底是怎么结束的?如何正确处理这种情况?
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")%>
...
浏览器:
更新:
屏幕截图显示了过多的HTML(以某种方式从servlet或jsp中拖出并放入description
文本字段,例如:</html>
)。为了清楚起见,突出显示错误的表单数据(黄色)我不需要自定义文本区域的字体。
UPDATE2:
我设法在Servlet中处理凌乱的代码,但我仍然不想在Servlet中存储HTML。我想只有逻辑。我不想将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}
...
铬:
答案 0 :(得分:0)
您无法自定义textarea
的字体。所以就这样吧..
<tr bgcolor=\"#ffffff\"><td bgcolor=\"#cccccc\" nowrap=\"\">description:</td><td> <textarea name=\"description\" cols=\"25\" rows=\"6\">"+dslr.getDescription()+"</td></tr>