Accessing a value in Struts ActionForm from JSP

16,501

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.

Share:
16,501
Mr Morgan
Author by

Mr Morgan

Updated on June 04, 2022

Comments

  • Mr Morgan
    Mr Morgan almost 2 years

    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.

    • Lion
      Lion over 11 years
      _userid is terribly a hateful name of a property that doesn't confirm the naming conventions of Java. You shouldn't be using it anymore.
    • Mr Morgan
      Mr Morgan over 11 years
      Agreed. _userid is simply a name used in the Struts example I was following which never worked properly anyway until this question was posted, and another one yesterday. Now that I have the example working, I will be changing the name to userName or some such.