日期:2014-05-17 浏览次数:20705 次
//这是一个注册的Example public class RegisterUserAction extends Action { public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { int rs = 0; RegisterUserForm registerUserForm = (RegisterUserForm) form; UserDAOInfo register = new UserDAOInfo(); int uid = registerUserForm.getUid(); boolean flag = register.chkUserId(uid); if (flag == true){ return mapping.findForward("ExitIderror"); } UserInfoBean userInfoBean = new UserInfoBean(); userInfoBean.setUid(registerUserForm.getUid()); userInfoBean.setUname(registerUserForm.getUname()); userInfoBean.setUnichen(registerUserForm.getUnichen()); userInfoBean.setUemail(registerUserForm.getUemail()); userInfoBean.setUbirthYear(registerUserForm.getUbirthYear()); userInfoBean.setUbirthMonth(registerUserForm.getUbirthMonth()); rs = register.registerUser(userInfoBean); if (rs != 0) { return mapping.findForward("success"); } else { return mapping.findForward("error"); } } }
------解决方案--------------------
public int registerUser(UserInfoBean userInfoBean) { int result = 0; try { conn = ConnectDB.getConn(); if(conn == null){ System.out.println("connect failed"); return result; } String strSql="insert into commonuser(uid,uname,unichen,uemail,ubirthYear,ubirthMonth)values(?,?,?,?,?,?)"; pstmt = conn.prepareStatement(strSql); if(pstmt == null){ System.out.println("prepare failed"); } pstmt.setInt(1, userInfoBean.getUid()); pstmt.setString(2, userInfoBean.getUname()); pstmt.setString(3, userInfoBean.getUnichen()); pstmt.setString(4, userInfoBean.getUemail()); pstmt.setString(5, userInfoBean.getUbirthYear()); pstmt.setString(6, userInfoBean.getUbirthMonth()); result = pstmt.executeUpdate(); close(); } catch (SQLException e) { e.printStackTrace(); } return result; }
------解决方案--------------------
struts2的话,帖子内容应该是一个实体对象的属性,在action中要有这个对象的get、set方法,然后在页面中标签的name,这样写name="实体.属性名",这样就保存在这个实体里了。action里get就有了。
------解决方案--------------------
不知道LZ之前学没学过servlet 其实一个道理 只不是struts2 可以写变量 set get 赋值
------解决方案--------------------
1、把页面上表单的名字在action类里声明为全局变量
2、把这些变量成生对应的get和set方法
OK
------解决方案--------------------