Abhishek Jain Abhishek Jain - 11 months ago 52
Java Question

setter method not being called in struts 2

I am doing struts2 + spring +hibernate + liferay integration. In that case the setter method is not being invoked inside struts2 Action class. The name of properties which I am sending through jsp is kept same both in Action class as well as form .Then too setter method is not executing. I need to insert the properties into database so I need those properties inside my Java class .Please tell me where I went wrong..Any help would be appreciated. The code for Action class is as follows:-

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;
}

}


The code of JSP is as follows:-

<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>


I want to clarify one thing that there is no problem in the hibernate section as the values inserted are null. But the problem is I want to actually store the values passed through the jsp. So help me please..thanx in advance.

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;
}

}

Answer Source

Simply, following changes in your view and action should work, as suggested in given reference:

view:

<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>

action:

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;
    }
}

Reference