用到的formbean/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.yourcompany.struts.form;import java.util.ArrayList;import javax.servlet.http.HttpServletRequest;import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;import com.user.model.User;/**
* MyEclipse Struts
* Creation date: 06-19-2007
*
* XDoclet definition:
* @struts.form name="userForm"
*/
public class UserForm extends ActionForm {
private static final long serialVersionUID=1L;
/** newEmail property */
private String newEmail; /** newUsername property */
private String newUsername; /** newDescription property */
private String newDescription; /** newPassword property */
private String newPassword; /** newTel property */
private String newTel; /** newName property */
private String newName; /** newRole property */
private String newRole;
private ArrayList userList;
private String hid;
private User viewUser;
private User updateUser;
/*
* Generated Methods
*/ /**
* Method reset
* @param mapping
* @param request
*/
/**
* Returns the newEmail.
* @return String
*/
public String getNewEmail() {
return newEmail;
} /**
* Set the newEmail.
* @param newEmail The newEmail to set
*/
public void setNewEmail(String newEmail) {
this.newEmail = newEmail;
} /**
* Returns the newUsername.
* @return String
*/
public String getNewUsername() {
return newUsername;
} /**
* Set the newUsername.
* @param newUsername The newUsername to set
*/
public void setNewUsername(String newUsername) {
this.newUsername = newUsername;
} /**
* Returns the newDescription.
* @return String
*/
public String getNewDescription() {
return newDescription;
} /**
* Set the newDescription.
* @param newDescription The newDescription to set
*/
public void setNewDescription(String newDescription) {
this.newDescription = newDescription;
} /**
* Returns the newPassword.
* @return String
*/
public String getNewPassword() {
return newPassword;
} /**
* Set the newPassword.
* @param newPassword The newPassword to set
*/
public void setNewPassword(String newPassword) {
this.newPassword = newPassword;
} /**
* Returns the newTel.
* @return String
*/
public String getNewTel() {
return newTel;
} /**
* Set the newTel.
* @param newTel The newTel to set
*/
public void setNewTel(String newTel) {
this.newTel = newTel;
} /**
* Returns the newName.
* @return String
*/
public String getNewName() {
return newName;
} /**
* Set the newName.
* @param newName The newName to set
*/
public void setNewName(String newName) {
this.newName = newName;
} /**
* Returns the newRole.
* @return String
*/
public String getNewRole() {
return newRole;
} /**
* Set the newRole.
* @param newRole The newRole to set
*/
public void setNewRole(String newRole) {
this.newRole = newRole;
} public String getHid() {
return hid;
} public void setHid(String hid) {
this.hid = hid;
} public User getUpdateUser() {
return updateUser;
} public void setUpdateUser(User updateUser) {
this.updateUser = updateUser;
} public ArrayList getUserList() {
return userList;
} public void setUserList(ArrayList userList) {
this.userList = userList;
} public User getViewUser() {
return viewUser;
} public void setViewUser(User viewUser) {
this.viewUser = viewUser;
}
}
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.yourcompany.struts.form;import java.util.ArrayList;import javax.servlet.http.HttpServletRequest;import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;import com.user.model.User;/**
* MyEclipse Struts
* Creation date: 06-19-2007
*
* XDoclet definition:
* @struts.form name="userForm"
*/
public class UserForm extends ActionForm {
private static final long serialVersionUID=1L;
/** newEmail property */
private String newEmail; /** newUsername property */
private String newUsername; /** newDescription property */
private String newDescription; /** newPassword property */
private String newPassword; /** newTel property */
private String newTel; /** newName property */
private String newName; /** newRole property */
private String newRole;
private ArrayList userList;
private String hid;
private User viewUser;
private User updateUser;
/*
* Generated Methods
*/ /**
* Method reset
* @param mapping
* @param request
*/
/**
* Returns the newEmail.
* @return String
*/
public String getNewEmail() {
return newEmail;
} /**
* Set the newEmail.
* @param newEmail The newEmail to set
*/
public void setNewEmail(String newEmail) {
this.newEmail = newEmail;
} /**
* Returns the newUsername.
* @return String
*/
public String getNewUsername() {
return newUsername;
} /**
* Set the newUsername.
* @param newUsername The newUsername to set
*/
public void setNewUsername(String newUsername) {
this.newUsername = newUsername;
} /**
* Returns the newDescription.
* @return String
*/
public String getNewDescription() {
return newDescription;
} /**
* Set the newDescription.
* @param newDescription The newDescription to set
*/
public void setNewDescription(String newDescription) {
this.newDescription = newDescription;
} /**
* Returns the newPassword.
* @return String
*/
public String getNewPassword() {
return newPassword;
} /**
* Set the newPassword.
* @param newPassword The newPassword to set
*/
public void setNewPassword(String newPassword) {
this.newPassword = newPassword;
} /**
* Returns the newTel.
* @return String
*/
public String getNewTel() {
return newTel;
} /**
* Set the newTel.
* @param newTel The newTel to set
*/
public void setNewTel(String newTel) {
this.newTel = newTel;
} /**
* Returns the newName.
* @return String
*/
public String getNewName() {
return newName;
} /**
* Set the newName.
* @param newName The newName to set
*/
public void setNewName(String newName) {
this.newName = newName;
} /**
* Returns the newRole.
* @return String
*/
public String getNewRole() {
return newRole;
} /**
* Set the newRole.
* @param newRole The newRole to set
*/
public void setNewRole(String newRole) {
this.newRole = newRole;
} public String getHid() {
return hid;
} public void setHid(String hid) {
this.hid = hid;
} public User getUpdateUser() {
return updateUser;
} public void setUpdateUser(User updateUser) {
this.updateUser = updateUser;
} public ArrayList getUserList() {
return userList;
} public void setUserList(ArrayList userList) {
this.userList = userList;
} public User getViewUser() {
return viewUser;
} public void setViewUser(User viewUser) {
this.viewUser = viewUser;
}
}
UserForm userForm=(UserForm)form;
User updateUser=userForm.getUpdateUser();
String username=updateUser.getUsername();
Session session=HibernateSessionFactory.getSession();
Transaction tx=session.beginTransaction();
String sql="select u from User u where u.username='"+username+"'";
Query q=session.createQuery(sql);
ArrayList userList=(ArrayList)q.list();
User user=(User)userList.get(0);
user=updateUser;
session.save(user);
tx.commit();
session.close();
package com.user.model;public class User {
private int id;
private String username;
private String password;
private String role;
private String name;
private String tel;
private String email;
private String description;
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
<action-mappings >
<action
attribute="userForm"
name="userForm"
path="/manageuser"
scope="request"
type="com.yourcompany.struts.action.ManageUserAction"
validate="false" >
<forward name="update_user" path="/update_user.jsp" />
<forward name="new_user" path="/new_user.jsp" />
<forward name="view_user" path="/view_user.jsp" />
<forward name="main_user" path="/main_user.jsp" />
</action> </action-mappings>
不知道为什么这样会不行。求救啊
<struts-config>
<data-sources />
<form-beans >
<form-bean name="userForm" type="com.yourcompany.struts.form.UserForm" /> </form-beans> <global-exceptions />
<global-forwards />
<action-mappings >
<action
name="userForm"
path="/manageuser"
scope="request"
type="com.yourcompany.struts.action.ManageUserAction"
validate="false" >
<forward name="update_user" path="/update_user.jsp" />
<forward name="new_user" path="/new_user.jsp" />
<forward name="view_user" path="/view_user.jsp" />
<forward name="main_user" path="/main_user.jsp" />
</action> </action-mappings> <message-resources parameter="com.yourcompany.struts.ApplicationResources" />
</struts-config>
中的name是text的名字,自己取个
property改为property="password"其它的都照着这个改下
property是text名字。所以才设置为updateUser.xxxx
updateUser是userForm(ActionForm)中的属性User类
有点想法。
我的FormBean是userForm
里面有一个属性User updataUser;
我感觉报错就是因为它没有办法保存
因为我把userForm的scope设置为session.它就不会报上面的错误
但是保存的时候updateUser只是一个空对象。没有把网页中修改的值传到这个User类中。
User updateUser=new User();
public void setUpdateUser(User user){
this.updateUser=user;
}
public User getUpdateUser(){
return updateUser;
}
}ActionForm中User updateUsery=new User().以后,把它当作ActionForm的一个属性,也要set和get方法的
一你用了ActionForm还用什么javaBean?
二标签里的name是对应的表单所连接的action所对应的form所以可以不用写
三标签里只写property属性就行了是form里的元素
4你想这样的获取整个类.有必要吗?
所以他这样写updateUser.xxxx,这样做也是可以的,只是他的代码中有些问题
我有一个user的javabean是因为要用hibernate和数据库打交道。
formBean还有一些额外属性
是为了方便在request保存一些需要用的user.比如说增加一个新,
更新一个。察看一个。
能指教一下具体什么问题吗。
我QQ123501875。希望得到大家的帮助。