struts2中怎么获取jsp页面中用户输入的数据?
比如在一个论坛中,怎么把用户发的帖子内容写入到数据中?数据库用的MySQL?
本人是一菜鸟,求高手解答@
[解决办法]
struts2在action中写一个变量,实现setter、getter方法;在jsp页面中标签的name属性直接写变量名就可以了;在action中就能获取到变量的值了,然后你再写到数据库中了;
[解决办法]
//这是一个注册的Examplepublic 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
------解决方案--------------------
问的问题太大,建议学习下Struts2