一个关于request.setAttribute()的简单问题,请各位指点
我的用struts做了一个简单的例子,但是遇到了这样的问题,我在jsp页面中用request.setAttribute()
设置的属性在Action中取出来是空的。
下面是我的jsp页面代码:
<%@ page contentType="text/html; charset=GBK" %>
<html:html locale="true">
<head>
<title>
jsp1
</title>
<html:base/>
</head>
<body>
<%
Float sum = new Float(10);
pageContext.getRequest().setAttribute("sum", sum);
%>
<p>
<html:form action="/untitled1Action.do" method="POST">
<html:text property="userName"/>
<br>
<html:submit property="submit" value="Submit"/><br>
<html:reset value ="Reset"/>
</html:form>
</body>
</html:html>
下面是我的Action的代码:
public class Untitled1Action extends Action {
public ActionForward execute(ActionMapping actionMapping,
ActionForm actionForm,
HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse) {
/**@todo: complete the business logic here, this is just a skeleton.*/
Untitled1ActionForm untitled1ActionForm = (Untitled1ActionForm) actionForm;
Float sum = (Float) httpServletRequest.getAttribute("sum");
System.out.println(sum.toString());
return actionMapping.getInputForward();
}
}
Float sum = (Float) httpServletRequest.getAttribute("sum"); 这句话取出来的sum就是空的,不知道为什么,请各位指教!另外我如何将上一个页面传过来得request对象里得Attribute放在当前页的Request对象里呀,是这样得么:
Float sumTotal = (Float) request.getAttribute("sumTotal");
if (sumTotal == null){
sumTotal = new Float(0);
}
pageContext.getRequest().setAttribute("sumTotal", sumTotal);