我试图获取bean rateCode.jsp中的所有html元素
<%@page import="com.hermes.data.RateCode_" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Rate Code</title>
</head>
<body>
<jsp:useBean id="user" class="com.hermes.data.RateCode_" scope="request" >
<jsp:setProperty name="user" property="*"/></jsp:useBean>
<form id="f_rateCode" action="/ratePromoCodes" method="post" >
<table align="center" border="1" cellspacing="0">
<tr>
<td colspan="2" align="center" class="header">Rate Code Administrations</td>
</tr>
<tr>
<td align="right" style="border-style: solid;">Rate Code:</td>
<td align="left" style="border-style: solid;">
<input type="text" id="code" name="code" value="${user.code}" size="10" maxlength="32" style="width: 100px"/>
</td>
</tr>
<tr>
<td align="right" style="border-style: solid;">Rate Description:</td>
<td align="left" style="border-style: solid;">
<input type="text" id="description" name="description" value="<%=user.getDescription()%>" maxlength="128" size="40"></td>
</tr>
<tr><td><input type="submit" value="ok" /></td> </tr>
</table>
</form>
Servlet – ratePromoCodes
protected void doPost(HttpServletRequest req, HttpServletResponse resp) {
RateCode_ rc = (RateCode_) req.getAttribute("user");
Enumeration an = req.getAttributeNames();
Enumeration pn = req.getParameterNames();
Object o = null;
while (an.hasMoreElements()) {
o = an.nextElement();
System.out.println(o);
}
while (pn.hasMoreElements()) {
o = pn.nextElement();
System.out.println(o);
}
}
RateCode.java(javaBean)
public class RateCode_ {
private String code ;
private String description;
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
您似乎误解了jsp:useBean的工作和目的.
首先,您已将bean声明为会话范围,并且您正在使用当前请求的所有参数填充它.
<jsp:useBean id="user" class="com.hermes.data.RateCode_" scope="session">
<jsp:setProperty name="user" property="*"/>
</jsp:useBean>
因此,此bean存储为具有名称user的会话属性.您需要在servlet中将其作为会话属性检索,而不是作为请求属性.
RateCode_ user = (RateCode_) request.getSession().getAttribute("user");
(顺便说一下,用户是一个可怕且令人困惑的属性名称,我将它重命名为rateCode或者什么,最后没有这个奇怪的_)
但是,它什么都不包含. getCode()和getDescription()将返回null. < jsp:setProperty>没有用所有请求参数填充它,此时你正试图在servlet中访问它.只有将包含参数的请求转发回JSP页面时才会这样做.但是,这发生在servlet中的业务逻辑之外.
您需要自己将它们作为请求参数收集.首先摆脱整个< jsp:useBean> JSP中的东西,并在servlet的doPost()方法中执行以下操作:
RateCode_ user = new RateCode_();
user.setCode(request.getParameter("code"));
user.setDescription(request.getParameter("description"));
// ...
request.setAttribute("user", user); // Do NOT store in session unless really necessary.
然后你可以在JSP中访问它,如下所示:
<input type="text" name="code" value="${user.code}" />
<input type="text" name="description" value="${user.description}" />
(这只对XSS attacks敏感,你想安装JSTL并使用fn:escapeXml)
不,你不需要< jsp:useBean>在JSP中.保持它,当你使用真正的servlet的MVC(2级)方法时几乎没有价值. < jsp:useBean>仅适用于MV设计(MVC级别1).要保存收集请求参数的样板代码,请考虑使用MVC框架或Apache Commons BeanUtils.另请参见下面的提示链接.
也可以看看:
> Easy way of populating Javabeans based on request parameters
> Using beans in servlets
> Our Servlets wiki page
