2014-05-25 2 views
1

스트럿츠 2 응용 프로그램에서 아래의 샘플을 고려하십시오. 이것은 그리드를 보여 주며 사용자가 동일한 그리드를 PDF로 내보낼 수있는 페이지입니다.스트럿츠 2 중복 작업 방지

두 가지 동작으로 클래스를 개발합니다. 하나의 동작은 JSON으로 그리드를 반환하고 다른 동작은 동일한 그리드를 PDF로 내 보냅니다.

코드의 구조는 다음과 같습니다 :

두 개의 서로 다른 액션지도 /ShowGrid하고 일반적인 구조를 많이 가지고 위의 방법을 볼 수 있듯이 /ExportGrid

@Action (name="ShowGrid") // Result will be set to JSON 
@validation (@Required (..... // Validation Rules for fromDate toDate etc 
public String showGrid(){ 
gird = serviceFacade.getGrid(fromDate,toDate); 
return SUCCESS; 
} 


@Action (name="ExportGrid") //Result will be set as stream 
@validation (@Required ..... // Validation Rules for fromDate toDate etc 
public String exportGrid(){ 
grid = serviceFacade.getGrid(fromDate,toDate); 
inputStream = convertGridToStream(grid); // By using jasper report or other tools 
return SUCCESS; 
} 

정의 된,

  • 유효성 검사가 복제됩니다 (본인의 경우 유효성 검사 규칙이 많음)
  • 서비스 메서드 호출이 중복되었습니다.

이 문제를 방지 할 수있는 방법이 있습니까?

답변

3

@Actions 주석을 사용하여 동일한 방법으로 많은 작업을 매핑 할 수 있습니다. actions 메소드 내에서 액션 컨텍스트에서 이름을 가져 와서 로직을 정의 할 수 있습니다.

@Actions({ 
    @Action ("ExportGrid"), //Result will be set as stream 
    @Action ("ShowGrid") // Result will be set to JSON 
}) 
@validation (@Required ..... // Validation Rules for fromDate toDate etc 
public String doGrid(){ 
    grid = serviceFacade.getGrid(fromDate,toDate); 
    if (ActionContext.getContext().getName().equals("ExportGrid") 
    inputStream = convertGridToStream(grid); // By using jasper report or other tools 
    return SUCCESS; 
}