关于Action获取JSP页面的List<formBean> 本帖最后由 andymu077 于 2011-08-07 16:53:56 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 这个不是和参数的提交是一样的道理吗,你一个或多个参数提交的时候,在action中不也是用相对应的参数接收,而一个list你也可以把它看做是一个参数,在action中声明一个list接收它就好了呀…… 可是从form的list变量。边取出来的却是null,不知道为什么呀! 在action中声明一个list 得到所有lstTblId文本框的值 在进行循环把每一个值添加到list中 1.你遍历的部分是否在<html:form action.../>中2.你返回的值不会是以list的形式返回的而可能是String[] strTblId ,String[] strTblName lz你为啥要把list传到action呢,你可以在action中封装哇。jsp只是显示的,不要设计太多的东西 这个用于显示的list,做成了textbox。修改之后再提交。在下一个Action中需要获取里边的修改值! 项目太多了。所以我想是不是可以一次将整个ListBean传到Action中!也就是这个list对象传到JSP中之后还可以一个List对象的方式传递到Action使用。 把前台的数据封装成list??这不是自找麻烦吗?!formbean对应不好用吗? 问题是真的不好用。提交到Action里边Form里边的list对象是null 表单里边的list取得的是null,你可以试一下 <body><%String id= request.getParameter("id");String action = id!=null?"emp.do?method=update":"emp.do?method=create";%><html:form action="<%= action%>"><html:hidden name="empForm" property="id"/>NAME:<html:text name="empForm" property="name" />BIRTHDAY: <html:text name="empForm" property="birthday" />ADDRESS: <html:text name="empForm" property="address" />EMAIL:<html:text name="empForm" property="email" /><html:submit/><html:reset/></html:form></body>public class EmpAction extends DispatchAction{ private final static String SUCCESS = "success"; //EmpService service = new EmpServiceImpl(); @Resource EmpService empService; public ActionForward find(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { Collection emps= empService.find(); request.setAttribute("emps", emps); return mapping.findForward(SUCCESS); } public ActionForward input(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { String id = request.getParameter("id"); if(id!=null){ Emp emp = empService.load(id); EmpForm empForm = (EmpForm) form; empForm.setId(emp.getId()); empForm.setName(emp.getName()); empForm.setBirthday(emp.getBirthday()); empForm.setAddress(emp.getAddress()); empForm.setEmail(emp.getEmail()); } return mapping.findForward("input"); } public ActionForward create(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { EmpForm empForm = (EmpForm)form; Emp emp = new Emp(); emp.setId(empForm.getId()); emp.setName(empForm.getName()); emp.setBirthday(empForm.getBirthday()); emp.setAddress(empForm.getAddress()); emp.setEmail(empForm.getEmail()); empService.create(emp); Collection emps= empService.find(); request.setAttribute("emps", emps); return mapping.findForward(SUCCESS); } public ActionForward update(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { EmpForm empForm = (EmpForm)form; Emp emp = new Emp(); emp.setId(empForm.getId()); emp.setName(empForm.getName()); emp.setBirthday(empForm.getBirthday()); emp.setAddress(empForm.getAddress()); emp.setEmail(empForm.getEmail()); empService.update(emp); Collection emps= empService.find(); request.setAttribute("emps", emps); return mapping.findForward(SUCCESS); } public void setService(EmpService empService) { this.empService = empService; } }public class EmpForm extends org.apache.struts.action.ActionForm{ String id; String name; String birthday; String address; String email; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getBirthday() { return birthday; } public void setBirthday(String birthday) { this.birthday = birthday; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; }} 程序变慢 如何在<% %>中调用外部的EL表达式的值? 如何通过HTML提取网页中的图片和其相关的文字? 请问这种图片是怎么生成的??? 菜鸟问题! 求救~上传下载的问题。 有点难度的SQL语句 怎么通过jspSmartUpload把一个word文件上传到 ms sql数据库里? 大哥,大姐!怎么在jbuilder9.0中把jsp中的图片包含进去?谢谢! 关于汉字的问题~请赐教 weblogic 8.1 axis 1.4 发布webservice 出错问题 Map request=(Map)ActionContext.getContext().get("request");
2.你返回的值不会是以list的形式返回的而可能是String[] strTblId ,String[] strTblName
项目太多了。所以我想是不是可以一次将整个ListBean传到Action中!也就是这个list对象传到JSP中之后还可以一个List对象的方式传递到Action使用。
问题是真的不好用。提交到Action里边Form里边的list对象是null
表单里边的list取得的是null,你可以试一下
<body><%String id= request.getParameter("id");
String action = id!=null?"emp.do?method=update":"emp.do?method=create";
%><html:form action="<%= action%>">
<html:hidden name="empForm" property="id"/>
NAME:<html:text name="empForm" property="name" />
BIRTHDAY: <html:text name="empForm" property="birthday" />
ADDRESS: <html:text name="empForm" property="address" />
EMAIL:<html:text name="empForm" property="email" />
<html:submit/>
<html:reset/>
</html:form>
</body>public class EmpAction extends DispatchAction{
private final static String SUCCESS = "success";
//EmpService service = new EmpServiceImpl();
@Resource EmpService empService;
public ActionForward find(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
Collection emps= empService.find();
request.setAttribute("emps", emps);
return mapping.findForward(SUCCESS);
}
public ActionForward input(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
String id = request.getParameter("id");
if(id!=null){
Emp emp = empService.load(id);
EmpForm empForm = (EmpForm) form;
empForm.setId(emp.getId());
empForm.setName(emp.getName());
empForm.setBirthday(emp.getBirthday());
empForm.setAddress(emp.getAddress());
empForm.setEmail(emp.getEmail());
}
return mapping.findForward("input");
}
public ActionForward create(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
EmpForm empForm = (EmpForm)form;
Emp emp = new Emp();
emp.setId(empForm.getId());
emp.setName(empForm.getName());
emp.setBirthday(empForm.getBirthday());
emp.setAddress(empForm.getAddress());
emp.setEmail(empForm.getEmail());
empService.create(emp);
Collection emps= empService.find();
request.setAttribute("emps", emps);
return mapping.findForward(SUCCESS);
}
public ActionForward update(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
EmpForm empForm = (EmpForm)form;
Emp emp = new Emp();
emp.setId(empForm.getId());
emp.setName(empForm.getName());
emp.setBirthday(empForm.getBirthday());
emp.setAddress(empForm.getAddress());
emp.setEmail(empForm.getEmail());
empService.update(emp);
Collection emps= empService.find();
request.setAttribute("emps", emps);
return mapping.findForward(SUCCESS);
}
public void setService(EmpService empService) {
this.empService = empService;
}
}
public class EmpForm extends org.apache.struts.action.ActionForm{
String id;
String name;
String birthday;
String address;
String email;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}}