are there requirements for Struts setters beyond variable name matching?

Posted by slk on Stack Overflow See other posts from Stack Overflow or by slk
Published on 2012-03-23T23:00:06Z Indexed on 2012/03/24 5:29 UTC
Read the original article Hit count: 196

Filed under:
|
|
|

I have a model-driven Struts Web action:

public class ModelDrivenAction<T extends Object> implements ModelDriven<T>, Preparable {

  protected Long id;
  protected T model;

  @Override
  public void prepare() {}

  public void setId(Long id) { this.id = id; }

  @Override
  public T getModel() { return model; }

  public void setModel(T model) { this.model = model; }
}

I have another action which is not currently model-driven:

public class OtherAction implements Preparable {

  private ModelObj modelObj;
  private Long modelId;

  @Override
  public void prepare() { modelObj = repoService.retrieveModelById(modelId); }

  public void setModelId(Long modelId) { this.modelId = modelId; }
}

I wish to make it so, and would like to avoid having to track down all the instances in JavaScript where the action is passed a "modelId" parameter instead of "id" if at all possible. I thought this might work, so either modelId or id could be passed in:

public class OtherAction extends ModelDrivenAction<ModelObj> {

  @Override
  public void prepare() { model = repoService.retrieveModelById(id); }

  public void setModelId(Long modelId) { this.id = modelId; }
}

However, server/path/to/other!method?modelId=123 is failing to set id. I thought so long as a setter matched a parameter name the Struts interceptor would call it on action invocation. Am I missing something here?

© Stack Overflow or respective owner

Related posts about java

Related posts about inheritance