1

I have a Struts 2 textfield tag where I just need to get a user enter value and send to the action.

<s:textfield name="user.firstAnswer" size="110" cssClass="FormObjectCompulsary" autocomplete="off" />

Even when this page loads user object contains value for first answer, I don't want to display it in the text field instead I want the text field to be blank.

But with out specify the value attribute still the value in user object shows in this field.

Roman C
  • 49,761
  • 33
  • 66
  • 176
Harshana
  • 7,297
  • 25
  • 99
  • 173

3 Answers3

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.

Roman C
  • 49,761
  • 33
  • 66
  • 176
0

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();
    }
}
Aleksandr M
  • 24,264
  • 12
  • 69
  • 143
Bhushankumar Lilapara
  • 780
  • 1
  • 13
  • 26
0

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

DarkHorse
  • 2,740
  • 19
  • 28