私はPRGパターンを理解しようとしており、ユーザーの名前と年齢を受け入れる簡単なフォームを作成しました。私はそれをウェルカムユーザ名を表示したいと提出した後、それを維持する必要があります。ここではすべてのヘルプはにappriciatedされますポストリダイレクトの使い方spring mvcを使用しますか?
@RequestMapping(value="prg",method={RequestMethod.GET})
public ModelAndView demoForm(){
System.out.println("in get");
ModelAndView model=new ModelAndView("demoForm");
return model;
}
@RequestMapping(value="/prg1",method={RequestMethod.GET})
public ModelAndView doGet(@ModelAttribute ("userName") String s){
System.out.println("in get1");
//String name=s;
ModelAndView model=new ModelAndView("succes");
model.addObject("name", s);
return model;
}
@RequestMapping(value="/prg1",method={RequestMethod.POST})
public ModelAndView doPost(@RequestParam(name = "uname") String s,final RedirectAttributes redirectAttribute){
System.out.println("in post1");
ModelAndView model=new ModelAndView("redirect:/prg1");
//model.addObject("uname", s);
redirectAttribute.addFlashAttribute("userName",s);
return model;
}
コード
demoForm.jsp
<form action="/Spring_Hibernate_RegistrationAndLogin/prg1" method="post">
Name: <input type="text" name="uname"><br/><br/><br/>
Age: <input type="text" name="age"><br/><br/><br/>
<input type="submit" value="Done"/>
succes.jsp
<h3>Hello ${name}</h3>
コントローラー・コードです。前もって感謝します。
suggestion.canに感謝します。コードスニペットで私を助けてください。 – jagga
コントローラクラスが定義されている場所で、@Controllerアノテーションの後の次の行に@SessionAttributes( "userName")を追加するだけです。 – loredana