2012-10-01 28 views
0

我不知道这是否是“拯救国家”这个词,但如果我在我的控制器有这样的方法:如何在spring 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"; 
    } 

我有重新创建hashmap,以便每次都放置性感选项,因为它会再次返回到同一页面(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

“春路”将是两个包含HashMap声明为实例变量,并在你的应用程序上下文(DI)将它们连接 - 也许映射存储在一个属性文件。