Accessing a value in Struts ActionForm from JSP

Mr Morgan picture Mr Morgan · Nov 25, 2012 · Viewed 16.4k times · Source

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.

Answer

JB Nizet picture JB Nizet · Nov 25, 2012

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.