struts2中怎么获取jsp页面中用户输入的数据? 比如在一个论坛中,怎么把用户发的帖子内容写入到数据中?数据库用的MySQL?本人是一菜鸟,求高手解答@ 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 struts2在action中写一个变量,实现setter、getter方法;在jsp页面中标签的name属性直接写变量名就可以了;在action中就能获取到变量的值了,然后你再写到数据库中了; 你也可以在action中定义类的setter、getter方法,类名。变量名 在action 里一个一个request.getparameter("")这也行。 页面上弄个form 然后要提交的东西都放form里 。 action 里用 定义变量 set方法 的方式或者 request.getParement()方法得值 //这是一个注册的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; }这是插入到数据库的方法上面是Action,获取页面上的值 struts2的话,帖子内容应该是一个实体对象的属性,在action中要有这个对象的get、set方法,然后在页面中标签的name,这样写name="实体.属性名",这样就保存在这个实体里了。action里get就有了。 不知道LZ之前学没学过servlet 其实一个道理 只不是struts2 可以写变量 set get 赋值 不知道LZ之前学没学过servlet 其实一个道理 只不过是struts2可以写变量 set get 赋值 1、把页面上表单的名字在action类里声明为全局变量2、把这些变量成生对应的get和set方法OK 问的问题太大,建议学习下Struts2 请教一个页面跳转的问题! 数据库如何动态生成树形导航菜单 extjs tree的问题,希望有人可以解答~~ 向oracle中插入,超过4000个字节该如何处理?? jsp连接mysql的一个问题 高手帮忙! JSP+STRTUS+MYSQL实现的多用户多语言的的系统,如何实现注册?? javabean连接sybase数据库出错,帮忙看看! 关于从数据库取出数据后进行条件判断,满足的行则显示红色 jsp中如何实现将本地磁盘上的文件存取到数据库中,存入和读出的时候可以指定文件的位置 struts action中execute方法 上传的图片保存在我的文件夹下,而路径保存在数据库中
//这是一个注册的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;
}这是插入到数据库的方法
上面是Action,获取页面上的值
2、把这些变量成生对应的get和set方法OK