Reputation: 7647
I have a Struts 2 <s:textfield>
tag, where I just need to get a user to enter a value and send it to the action.
<s:textfield name="user.firstAnswer" size="110" cssClass="FormObjectCompulsary" autocomplete="off" />
Even when this page loads, the user object contains a value for the first answer. I don't want to display it in the text field. Instead, I want the text field to be blank.
But, without specifying the value
attribute, the value of the user object still shows in this field.
Upvotes: 0
Views: 1611
Reputation: 1
If you are adding a new object user
, then you should create this object with new
operator before you show it in the JSP. It will contain null
references that are not displayed. If the value
attribute is not specified, then name
is used to show the value.
Upvotes: 1
Reputation: 2770
By looking at name="user.firstAnswer"
I am thinking that you are implementing ModelDriven<> to your action class. What might be happening is that when you return success in your action class and come to the jsp page, and if in action your user model had some values on it.. model driven will set those fields for your on your JSP page.
I have used this approach for update form functionality while learning struts2. Just make sure that user object contains nothing before you return...
Upvotes: 0
Reputation: 780
Make your user object null after inside the execute()
. So again it will not show value inside text box.
eg. user = null;
I am showing you piece of code, may be it will help you.
See the execute()
.
package online.solution;
import com.opensymphony.xwork2.Action;
public class MyAction implements Action {
UserBean user = new UserBean();
public UserBean getUser() {
return user;
}
public void setUser(UserBean user) {
this.user = user;
}
@SuppressWarnings("finally")
@Override
public String execute() throws Exception {
String result = "";
try {
user.setGuest("Bye bye");
System.out.println(user.getUsername() + " " + user.getPassword());
if (user.getUsername().equals(user.getPassword())) {
result = SUCCESS;
}
else {
result = ERROR;
}
user = null; //Make it null when all task completed.
}
catch (Exception exception) {
System.out.println("Exception -> " + exception);
}
finally {
return result;
}
}
@Override
protected void finalize() throws Throwable {
super.finalize();
}
}
Upvotes: 0