Mr Morgan
Mr Morgan

Reputation: 2243

Accessing a value in Struts ActionForm from JSP

I am a Struts 1.3.10 newbie and I have an issue where I have an Action called RegistrationAction as follows:

    public final class RegistrationAction extends Action{

        @Override
        public ActionForward execute(ActionMapping mapping,
                                     ActionForm form,
                                     HttpServletRequest request,
                                     HttpServletResponse response)
        throws Exception{

           RegistrationForm formBean = (RegistrationForm) form;
           String userid = formBean.getUserid();
           String pwd = formBean.getPassword();

The userid and password are then saved to a HashMap<String, String> with attributes _userid and pwd.

RegistrationAction then calls a JSP if the validation of the userid and password are successful. But what I am finding is that in the JSP, the userid is not being displayed using the following code:

    <h1>Hello  <bean:write name="RegistrationForm" property="_userid" />!</h1>

The matching ActionForm RegistrationForm contains the _userid field as below:

    public final class RegistrationForm extends ActionForm{

        private String _userid = null;

        public String getUserid(){ return _userid; }
        public void   setUserid(String userid){ _userid = userid; }

        ...

I know that an instance of RegistrationForm is being populated because I can retrieve the entered _userid via:

    if(err == RegistrationModel.OK){
            System.out.println("Here " + model.getUserid()); 

I thought that a reference to the RegistrationForm in the JSP such as:

<h1>Hello <bean:write name="RegistrationForm" property="_userid" />!</h1>

Would work.

Can anyone suggest where I'm wrong?

Thanks to the respondents. The page works now.

Upvotes: 4

Views: 17207

Answers (3)

someone
someone

Reputation: 6572

1) Add your RegistrationForm to request

request.setAttribute("RegistrationForm",formBean);

2) Remove _ from your _userId variable

Upvotes: 0

Udo Held
Udo Held

Reputation: 12548

Try

<h1>Hello  <bean:write name="RegistrationForm" property="userid" />!</h1>

it doesn't matter what the internal name / coding of your formbean is. All what matters is how the getters and setters are named. Your getter is named getUserid() to the javabean property is userid.

Upvotes: 0

JB Nizet
JB Nizet

Reputation: 692081

The JSP tags, and the JSP EL, access bean properties, and not bean fields. So if you pass _userId, it will look for a getter method called get_userId(). Since you want to access the getter getUserId(), you need to use userId inside the tag.

Upvotes: 2

Related Questions