2012-10-01 2 views
0

는 단어 "상태를 저장"인 경우 나도 몰라,하지만 난 내 컨트롤러에서이 방법이있는 경우 :봄 mvc 3.1에서 상태를 저장하는 방법?

@RequestMapping(value = "/", method = RequestMethod.GET) 
    public String home(Locale locale, Model model, HttpServletRequest request) { 
     Date date = new Date(); 
     DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale); 
     String formattedDate = dateFormat.format(date); 
     model.addAttribute("serverTime", formattedDate); 
     model.addAttribute("email", new Email()); 
     model.addAttribute("imgBg", getRandomBg(request.getRemoteHost())); 
     Map sexoOpts = new HashMap(); 
     sexoOpts.put("M", "homem"); 
     sexoOpts.put("F", "mulher"); 

     Map sexoOpts2 = new HashMap(); 
     sexoOpts2.put("M", "Busco por homens"); 
     sexoOpts2.put("F", "Busco por mulheres"); 

     model.addAttribute("sexoList1", sexoOpts); 
     model.addAttribute("sexoList2", sexoOpts2); 
     return "index"; 
    } 

을 다른 방법으로 내가 가진 :

@RequestMapping(value = "/save-email", method = RequestMethod.POST) 
    public String doSaveEmail(@Valid @ModelAttribute("email") Email email,BindingResult result, Model model, HttpServletRequest request){ 
     model.addAttribute("imgBg", getRandomBg(request.getLocalAddr())); 
     Map sexoOpts = new HashMap(); 
     sexoOpts.put("M", "homem"); 
     sexoOpts.put("F", "mulher"); 

     Map sexoOpts2 = new HashMap(); 
     sexoOpts2.put("M", "Busco por homens"); 
     sexoOpts2.put("F", "Busco por mulheres"); 

     model.addAttribute("sexoList1", sexoOpts); 
     model.addAttribute("sexoList2", sexoOpts2); 

     if (result.hasErrors()){ 
      return "index"; 
     } 
     Date date = new Date(); 
     email.setCreationDate(date); 

     boolean saved = false; 
     try{ 
      saved = emailBo.saveEmail(email); 
     }catch(Exception e){ 
      e.printStackTrace(); 
     } 
     model.addAttribute("email", new Email()); 
     if (saved){ 
      model.addAttribute("saveStatus", "ok"); 
     }else{ 
      model.addAttribute("saveStatus", "false"); 
     } 


     return "index"; 
    } 

내가 가진 다시 한번 같은 페이지 (index.jsp)로 돌아갈 것이기 때문에 매번 섹시한 옵션을 넣기 위해 해시 맵을 재 작성 하시겠습니까? 내가 집에서 이메일을 보내고 돌아갈 때 이것을 구할 방법이 없습니까?

+0

doSaveEmail 메소드에서 home 메소드로 제공되는 모델을 다시 사용할 수 있습니까? 그렇다면 doSaveEmail 메소드의 모델에 다시 추가 할 필요가 없습니다. – tiagomac

답변

0

나는 Map을 상수로 저장합니다.이 방법은 메소드 외부에 있지만 여전히 내부에서 참조 할 수 있습니다.

public class MyController { 
    private static Map sexoOpts = new HashMap(); 
    private static Map sexoOpts2 = new HashMap(); 

    static { 
     sexoOpts.put("M", "homem"); 
     sexoOpts.put("F", "mulher"); 
     sexoOpts2.put("M", "Busco por homens"); 
     sexoOpts2.put("F", "Busco por mulheres"); 
    } 

    @RequestMapping(value = "/", method = RequestMethod.GET) 
    public String home(Locale locale, Model model, HttpServletRequest request) { 
     //I have access to sexoOpts and sexoOpts2, so there is no 
     //need to instantiate them in here anymore... 
    } 

    @RequestMapping(value = "/save-email", method = RequestMethod.POST) 
    public String doSaveEmail(@Valid @ModelAttribute("email") Email email,BindingResult result, Model model, HttpServletRequest request){ 
     //I have access to sexoOpts and sexoOpts2, so there is no 
     //need to instantiate them in here anymore... 
    } 
} 
0

은 "스프링 방식"인스턴스 변수로 두 HashMaps을 선언하고 애플리케이션 컨텍스트 (DI)에서 그 배선하는 것 - 어쩌면 특성 파일에 매핑을 저장합니다.

관련 문제