5

I am migrating an Struts 1 app to Struts2 and trying to minimize the code changes required.

I need to know how to access the ActionForm in a Struts2 Action class. Below is my current code and I am getting NPE when trying to access ActionForm.

Public class DeptBuildingNewAction extends ActionSupport 
implements ServletRequestAware, ServletResponseAware,  ModelDriven<DeptBuidingFormBean> {

private HttpServletRequest request;
private HttpServletResponse response;
private DeptBuidingFormBean form;

public void setServletRequest(HttpServletRequest httpServletRequest) {
    this.request = httpServletRequest;
}

public void setServletResponse(HttpServletResponse httpServletResponse) {
    log.debug("Inside setServletResponse");
    this.response = httpServletResponse;
}

public DeptBuidingFormBean getModel() {
    log.debug("Inside getForm");
    return form;
}

public void setModel(DeptBuidingFormBean form) {
    log.debug("Inside setForm");
    this.form = form;
}

What is the best way to get the ActionForm over here?

Roman C
  • 49,761
  • 33
  • 66
  • 176
Ravi Gupta
  • 4,468
  • 12
  • 54
  • 85

1 Answers1

1

The form (model in Struts2) should be initialized to prevent NPE.

private DeptBuidingFormBean form = new DeptBuidingFormBean();

The ModelDriven action allows to access a model on the view layer and in action directly from the valueStack, i.e. without model or form prefix.

The modelDriven interceptor should be on the interceptors stack of the action. The default stack contains this interceptor.

From the docs:

Note: The ModelDrivenInterceptor will only push the model into the stack when the model is not null, else it will be ignored.

In the action class you have a field that you can use inside.

Roman C
  • 49,761
  • 33
  • 66
  • 176
  • i don't thing so, if it is then why ModelDriven required. in your code it will create new form instead of populated data from request. – Manoj Sharma Dec 16 '14 at 10:38
  • I guess the problem with this approach is that I will always end up getting a new empty ActionForm. – Ravi Gupta Dec 16 '14 at 10:42
  • @user2482616 @RaviGupta To populate data a `params` interceptor is required, and it won't do it if the model is not on `top` of the value stack. Without model driven the action bean is on top (optional). Then binding input fields required to use prefix in name to access bean's properties. – Roman C Dec 16 '14 at 11:06
  • @Andrea Ligios i will check. Well thanks for your explanation. – Manoj Sharma Dec 16 '14 at 11:06
  • RomanC already explained this in the second revision of his answer :) BTW to better understand how ModelDriven works, taking a look at [this answer](http://stackoverflow.com/a/21021131/1654265) might help. – Andrea Ligios Dec 16 '14 at 11:20
  • @AndreaLigios What do you mean by *bypassing the instantiation operation*. Without instantiation and initialization it will not work, I don't think OP is able to convert a whole model to the action property. – Roman C Dec 16 '14 at 11:33
  • @AndreaLigios If you only *guess* means that you are not sure about it. You may ask a question to clarify details. Or ask OP to clarify which way he/she is getting a form instantiated. – Roman C Dec 16 '14 at 12:51
  • Thinking about it, you're right, it's always needed, I've removed the misleading comments – Andrea Ligios Dec 16 '14 at 13:07
  • @RaviGupta There's no `ActionForm` in Struts2, the model driven approach have been used to provide this feature using interceptors, but the way it's implemented doesn't work in some cases where the model driven is used. Also you can develop without model driven, so it makes this concept ambiguous. – Roman C Dec 22 '14 at 10:49