Domanda

I have an abstract class that looks like the following with appropriate getters and setters for private properties (left out to keep the post sort):

public abstract class CityDistanceAwareAction extends BaseAction implements Preparable {
  private CityDistanceRepository cityDistanceRepository;

  private CityRepository cityRepository;

  private String cityA;

  private String cityB;

  private CityDistance cityDistance;

  public void prepare() {
    if (StringUtils.isNotBlank(cityA) && StringUtils.isNotBlank(cityB)) {
      CityPair cityPair = new CityPair(getCityRepository().findByName(cityA), getCityRepository().findByName(cityB));
      setCityDistance(getCityDistanceRepository().load(cityPair));
    }
  }
}

And then I have a very simple Class that looks like the following:

public class SearchCityDistanceAjaxAction extends CityDistanceAwareAction {

  @Override
  public String perform() {
    if (getCityDistance() == null) {
      addActionError("No city distance found for that pair");
      return ERROR;
    }
    return SUCCESS;
  }
}

The idea is that I can send in a String cityA and String cityB. Do my look-ups and simply return the cityDistance object via AJAX.

When I set my struts.xml action up as follows:

<action name="searchCityDistance" class="searchCityDistanceAjaxAction">
  <result type="json">
    <param name="includeProperties">
      cityDistance.*
    </param>
  </result>
  <result name="error" type="json">
    <param name="ignoreHierarchy">false</param>
    <param name="includeProperties">
      actionErrors.*
    </param>
  </result>
</action>

I get the following return:

(CommonsLogger.java:68) - Adding include property expression: cityDistance.* (CommonsLogger.java:68) - [JSON]{}

But I expected to get the cityDistance.* object back provided by the getter in the abstract CityDistanceAwareAction.

Interestingly enough if I add a special getter on the SearchCityDistanceAjaxAction object like the following:

  public CityDistance getAjaxCityDistance() {
    return getCityDistance();
  }

and change the struts file to:

  <result type="json">
    <param name="includeProperties">
      ajaxCityDistance.*
    </param>
  </result>

It gives me what I expected from my first attempt.

(CommonsLogger.java:68) - Adding include property expression: ajaxCityDistance.* (CommonsLogger.java:68) - [JSON]{"ajaxCityDistance":{"cityPair":{"cityA":{"cityName":"American Falls","id":68},"cityB":{"cityName":"Ririe","id":119}},"distance":85}}

It would be ideal to remove the getter from my SearchCityDistanceAjaxAction as a getter is already provided in the parent. Is there something I am missing here or is this the way it should function?

Thanks in advance.

È stato utile?

Soluzione

I was able to change my struts.xml file to the following:

<action name="searchCityDistance" class="searchCityDistanceActionAjaxAction">
  <result type="json">
    <param name="ignoreHierarchy">false</param>
    <param name="excludeProperties">
      cityDistanceRepository
    </param>
    <param name="includeProperties">
      cityDistance.*
    </param>
  </result>
  <result name="error" type="json">
    <param name="ignoreHierarchy">false</param>
    <param name="includeProperties">
      actionErrors.*
    </param>
  </result>
</action>

But it still smells a little fishy to use the excludeProperties to leave out my cityDistanceRepository. If this is how has to be I guess that is what I will do but hopefully there is a better solution.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top