2014-11-24 2 views
1

스프링 MVC 기반 응용 프로그램이 있고 매개 변수 값에 따라 일부 컨트롤러가 동일한보기를 반환하는 기능을 추가하고 싶습니다. .봄 : 컨트롤러가 매개 변수에 따라보기를 반환하도록 xml을 구성하십시오.

@RequestMapping("/someView") 
public String returnView(Model model, HttpServletRequest request, String param){ 
    if(param.equals("condition")){ 
     return "commonView"; 
    } 

    // do stuff 

    return "methodSpecificView"; 
} 

xml에서 첫 번째 if 조건을 구성하는 방법은 있습니까? 유사한 기능이 많은 컨트롤러에서 구현되어야하므로 상용구 코드를 작성하고 싶지 않기 때문에 XML 구성을 사용하면 작업이 훨씬 간단해질 수 있습니다.

첫 번째 매개 변수가 가능하면 요청 매핑 방법 서명에서 param 매개 변수를 제거하고이를 xml에도 넣을 수 있습니까?

답변

0

아래와 같이 AOP - Around 조언을 통해이를 구현하는 것이 좋습니다.

@Around("@annotation(RequestMapping)") // modify the condition to include/exclude specific methods 
public Object aroundAdvice(ProceedingJoinPoint joinpoint) throws Throwable { 

    Object args[] = joinpoint.getArgs(); 
    String param = args[2]; // change the index as per convenience 
    if(param.equals("condition")){ 
     return "commonView"; 
    } else { 
     return joinpoint.proceed(); // this will execute the annotated method 
    } 
} 
1

당신은 @RequestMapping을 사용할 수 있습니다 주석 문제에 대해 당신에게 아이디어를 줄 것이다 아래의 코드를 기반으로 스프링 3.2에서

@RequestMapping(value = {"/someView", "/anotherView", ...}, params = "name=condition") 
public String returnCommonView(){ 
    return "commonView"; 
} 
1

:

@RequestMapping("formSubmit.htm") 
public String onformSubmit(@ModelAttribute("TestBean") TestBean testBean,BindingResult result, ModelMap model, HttpServletRequest request) { 
    String _result = null; 
    if (!result.hasErrors()) { 
      _result = performAction(request, dataStoreBean);//Method to perform action based on parameters recieved 
     } 
     if(testBean.getCondition()){ 
     _result = "commonView"; 
     }else{ 
     _result = "methodSpecificView"; 
     }  
     return _result; 

    } 
TestBean//Class to hold all the required setters and getters 

설명 : 으로 보기에서 요청을 받으면 ModelAttribute 참조는 모델 속성에서 직접 얻을 수있는 것보다 조건을 뷰에서 가져온 경우보기의 모든 값을 보유하고 해당보기를 리턴합니다. 특정 로직을 적용한 후에 조건을 얻은 경우 testBean에서 조건을 설정하고 다시 해당 뷰를 반환 할 수 있습니다.

관련 문제