4

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.

Roman C
  • 49,761
  • 33
  • 66
  • 176
Mr Morgan
  • 2,215
  • 15
  • 48
  • 78
  • 1
    `_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. – Lion Nov 25 '12 at 16:25
  • 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. – Mr Morgan Nov 25 '12 at 16:34

3 Answers3

2

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.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
0

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.

Udo Held
  • 12,314
  • 11
  • 67
  • 93
0

1) Add your RegistrationForm to request

request.setAttribute("RegistrationForm",formBean);

2) Remove _ from your _userId variable

someone
  • 6,577
  • 7
  • 37
  • 60