我正在做struts2 + spring + hibernate + liferay集成。在这种情况下,不会在struts2 Action类中调用setter方法。我通过jsp发送的属性的名称在Action类和表单中保持相同。然后,setter方法也没有执行。我需要将属性插入数据库,所以我需要在我的Java类中的那些属性。请告诉我我哪里出错了。任何帮助将不胜感激。 Action类的代码如下: -
public class AddUserAction extends ActionSupport implements ModelDriven<User> {
/**
*
*/
private static final long serialVersionUID = 4587665276501838677L;
private String name="";
private String place=null;
private String state=null;
private User user = new User();
private List<User> userList = new ArrayList<User>();
private List<String> validateErrors;
private UserDAO userDao;
public AddUserAction(UserDAO userDao) {
this.userDao = userDao;
}
@Override
public User getModel() {
return user;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
System.out.println("hello");
}
@Override
public String execute() throws Exception {
//validateBookmark();
userDao.saveUser(user);
setUserList(userDao.listUser());
System.out.println("List users"+userDao.listUser());
return SUCCESS;
}
/*public void validateBookmark() throws Exception {
Student studentObject = getNewStudent();
validateErrors = new ArrayList<String>();
StudentValidator validator = new StudentValidator();
if(validator.validateStudent(studentObject, validateErrors)) {
StudentLocalServiceUtil.addStudent(studentObject);
addActionMessage(MessageStore.STUDENT_ADDED_SUCCESSFUL);
}
else {
//handle the error massage
addActionError(MessageStore.BOOKMARK_ADDED_FAILED);
addFieldError("failed", MessageStore.BOOKMARK_ADDED_FAILED);
Iterator<String> errorIter = validateErrors.iterator();
int count = 0;
while (errorIter.hasNext()) {
String error = errorIter.next();
addFieldError("error" + count, error);
count++;
}
}
}*/
/**
* Create a new Bookmark Object from the view
* @return
*/
/*private User getNewUSer() {
User user = new User();
user.setName(getName());
user.setPlace(getPlace());
user.setState(getState());
return user;
}*/
/*
* Getters and Setters start here
*/
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
System.out.println("name is hfdj");
}
public List<String> getValidateErrors() {
return validateErrors;
}
public void setValidateErrors(List<String> validateErrors) {
this.validateErrors = validateErrors;
}
public String getPlace() {
return place;
}
public void setPlace(String place) {
this.place = place;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public void setUserList(List<User> userList) {
this.userList = userDao.listUser();
}
public List<User> getUserList() {
return userList;
}
}
JSP的代码如下: -
<s:form action="adduserrecord">
<s:textfield name="name" label="Student Name" value="abhi"/>
<s:textfield name="place" label="Student Place" value=""/>
<s:textfield name="state" label="Student State" value=""/>
<s:submit value="Add User" align="center"/>
</s:form>
我想澄清一下hibernate部分没有问题,因为插入的值为null。但问题是我想实际存储通过jsp传递的值。所以请帮助我..提前做好准备。
User.java
@Entity(name = "user")
@Table(name="USER")
public class User {
private Long id;
private String name;
private String place;
private String state;
@Id
@GeneratedValue
@Column(name="USER_ID")
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Column(name="USER_NAME")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Column(name="USER_PLACE")
public String getPlace() {
return place;
}
public void setPlace(String place) {
this.place = place;
}
@Column(name="USER_STATE")
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
}
答案 0 :(得分:0)
简单地说,您的观点和操作中的更改应该有效,如给定参考中所示:
查看:强>
<s:form action="adduserrecord">
<s:textfield name="user.name" label="Student Name" value="abhi" />
<s:textfield name="user.place" label="Student Place" value="" />
<s:textfield name="user.state" label="Student State" value="" />
<s:submit value="Add User" align="center" />
</s:form>
<强>动作:强>
public class AddUserAction extends ActionSupport implements ModelDriven<User> {
private static final long serialVersionUID = 4587665276501838677L;
private User user;
@Override
public String execute() throws Exception {
// Here you will find your user object populated with form fields
userDao.saveUser(user);
return SUCCESS;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}