小弟在学习struct的时候遇到一个初始化Form的问题,现在请各位指点。先贴出代码,再说问题:
<!-- 使用struct标签的表单 -->
<html:form action="/set" method="post" >
<html:text property="userName" value="" />
</html:form>
=================================================================
// 对应的ActionForm代码
public class SetForm extends ActionForm {
protected String userName="";
public void setUserName(String userName){
this.userName = userName;
} public String getUserName(){
return userName;
}
}另外,在Cookie中,存放了一个名称为userName的值
我现在的问题是,如果我想在JSP页面加载时,从Cookie中获取userName来初始化form中的userName,我应该怎样做呢?(注意,我不想将读Cookie的代码写到JSP页面中)请各位帮忙,先谢谢了
<!-- 使用struct标签的表单 -->
<html:form action="/set" method="post" >
<html:text property="userName" value="" />
</html:form>
=================================================================
// 对应的ActionForm代码
public class SetForm extends ActionForm {
protected String userName="";
public void setUserName(String userName){
this.userName = userName;
} public String getUserName(){
return userName;
}
}另外,在Cookie中,存放了一个名称为userName的值
我现在的问题是,如果我想在JSP页面加载时,从Cookie中获取userName来初始化form中的userName,我应该怎样做呢?(注意,我不想将读Cookie的代码写到JSP页面中)请各位帮忙,先谢谢了
<html:text property="userName" value="" />把读取cookie的值给userName,把value去掉
进入这个表单页面之前
先在Action里从cookie里读取userName,存在request里
再
<html:text property="userName" value="${userName}" />
动态表单不是主要完成动态生成setter和getter的吗?请问怎样取得request???
刚好想到办法,好像就是tianyao_gs所说的方法,谢谢!我现在的JSP代码如下,具体的Cookie读取和其它操作就可以完全在action中操作了,哈哈<!--
JSP页面代码
--><!-- forward到.do页面,这样就可以创建action了 -->
<c:if test="${ThisActionForm.userId==null}">
<jsp:forward page="set.do" />
</c:if><!-- 使用struct标签的表单 -->
<html:form action="/set" method="post" >
<html:text property="userName" value="" />
</html:form>
结贴吧,辛苦大家了