0

それが「状態を保存する」という言葉であるかどうかはわかりませんが、コントローラーにこのメソッドがある場合:

@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) に戻るので、毎回セクシーなオプションを配置するためにハッシュマップを再作成する必要がありますか? 家からメールを保存して戻ってきたときにこれを保存する方法はありませんか?

4

2 に答える 2

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...
    }
}
于 2012-10-01T19:32:09.623 に答える
0

「春の方法」は、2 つのハッシュマップをインスタンス変数として宣言し、それらをアプリケーション コンテキスト (DI) に接続することです。マッピングをプロパティ ファイルに保存することもできます。

于 2012-10-01T20:47:11.277 に答える