很多时候我们要 修改一些 实体类的信息的时候,当从主页跳转到 修改页后,还要把实体的信息一并传过去,
效果是这样的:(当然这个很简单,做法也很多)
当点击更新的后为:
这里肯定dao层得有一个方法 通过id获取实体类。
其中主页的代码:
更新页面为:
<s:form action="student!update" method="get">
<!-- action类中的属性与这里的name如果对应的话,这就直接把值赋了过去 -->
姓名:<s:textfield name="name"></s:textfield>
年龄:<s:textfield name="age"></s:textfield>
性别:<s:textfield name="sex"></s:textfield>
<s:hidden name="id"></s:hidden>
<s:submit value="提交"/>
</s:form>
主要的是 student对应的action类中的update2jsp方法,下面是关键代码:
public String update2jsp(){
student=studentService.getStduentById(id);
//这句话直接将student压到了valuestack的栈顶
ActionContext.getContext().getValueStack().push(student);
//上式等价与
//ActionContext.getContext().getValueStack().getRoot().add(0,student);
//System.out.println("stduent: "+student.getAge()+"/"+student.getName()+"/"+student.getSex());
return SUCCESS;
}
当然要 将student作为 action的一个属性。
其中这句话是关键;ActionContext.getContext().getValueStack().push(student);
观察update2jsp页面的debug:
我们封装的是实体类,到jsp页面后 全部成为属性了(这就是妙处);
我们直接可以用ognl表达式获取。
写给有用的人。。。