Access value in Struts ActionForm from JSP

I am new to Struts 1.3.10 and I have a problem when 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 user ID and password are then stored in a HashMap<String, String> with the attributes _userid and pwd .

RegistrationAction then calls the JSP if the userid and password check succeeds. But I find that in JSP, the user ID is not displayed using the following code:

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

The corresponding ActionForm RegistrationForm contains the _userid field, as shown 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 the RegistrationForm instance is _userid because I can get the entered _userid through:

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

I thought there was a reference to RegistrationForm in JSP, such as:

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

Will work.

Can anyone suggest where I'm wrong?

Thanks to the respondents. The page is working now.

+4
source share
3 answers

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

+2
source

Try

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

It doesn't matter what the internal name / coding of your form is. All that matters is what getters and setters are called. Your recipient has the name getUserid() for the javabean: userid property.

0
source

1) Add your registration form for the request

 request.setAttribute("RegistrationForm",formBean); 

2) Remove _ from _userId variable

0
source

All Articles