<package name="system" namespace="/system" extends="struts-default"> <action name="elecUser_*" class="elecUserAction" method="{1}"> <result name="home">/WEB-INF/page/system/userIndex.jsp</result> <result name="add">/WEB-INF/page/system/userAdd.jsp</result> <result name="save" type="redirectAction"> <param name="actionName">elecUser_home</param> <param name="namespace">/system</param> </result> </action>
</package>
public class ElecUserAction extends BaseAction implements ModelDriven<ElecUserForm>{
private IElecUserService elecUserService; private IElecSystemDDlService elecSystemDDlService;
public IElecSystemDDlService getElecSystemDDlService() {
return elecSystemDDlService;
}
@Resource
public void setElecSystemDDlService(IElecSystemDDlService elecSystemDDlService) {
this.elecSystemDDlService = elecSystemDDlService;
}
public IElecUserService getElecUserService() {
return elecUserService;
}
@Resource
public void setElecUserService(IElecUserService elecUserService) {
this.elecUserService = elecUserService;
}
ElecUserForm elecUserForm = new ElecUserForm();
@Override public ElecUserForm getModel() {
return elecUserForm;
}
public String home(){
List<ElecUserForm> list = elecUserService.findElecUserList(elecUserForm);
request.setAttribute("list", list);
return "home";
}
public String save(){
this.elecUserService.save(elecUserForm);
return "save";
}
}
这个表单是添加用户的,点击保存的时候,在一个js函数中验证手机号、email等的数据格式和是否为空等,然后通过document.Form1.action="system/elecUser_save";把表单提交给保存用户的action,在save()方法中进行保存,struts.xml中elecUser_save的result type设成redirectAction,重定向到home,也就是添加完user以后重新跳到显示user的界面。
奇怪的是,添加的时候结果很不确定,有时候能添加上,有时候不从elecUser_save中走,不知道是什么原因。后来把上面的save方法中的返回值直接改成“home”,配置文件中去掉name=“save”那行,测试时还是有时候保存不了,不经过save()方法的处理
关注中
配置文件有问题。result写在action外面了?
不好意思,粘过来调整格式的时候弄错了,程序里面没写在外面