2013-04-23 60 views
2

把jsp我試圖將數據發送到JSP,但它不工作如何從控制器發送數據春季3.0

public class LoginPageController extends SimpleFormController 
{ 
    public ModelAndView onSubmit(HttpServletRequest request, 
     HttpServletResponse response, Object command, BindException errors) 
     throws ServletException 
    { 
     Loginpage lcmd=(Loginpage)command; 
     System.out.println("This is LOGIN PAGE"); 
     System.out.println(lcmd.getUserName()); 
     System.out.println(lcmd.getPassWord()); 
     request.setAttribute("MSG","Thank u"); //This code not doing anything. 
     return new ModelAndView(new RedirectView("Login.jlc")); 
    } 
} 
+0

我應該怎麼寫才能發送數據給j s p,我嘗試過控制器中的setAttribute和j $ p $ {logical name},但它不工作。 – user2312546 2013-04-24 01:43:15

+0

in controller request.setAttribute(「MSG」,「THANK U」); in j s p $ {MSG} – user2312546 2013-04-24 01:46:53

回答

1

您正在改變request對象,確實不會做任何事情。

你想要的是向模型添加一個變量。因此,這裏是你如何能做到這一點:

相反的:

request.setAttribute("MSG","Thank u"); //This code not doing anything. 
return new ModelAndView(new RedirectView("Login.jlc")); 

試試這個:如果你

Map<String, Object> model = new HashMap<String, Object>(); 
model.put("MSG", "Thank u"); 
return new ModelAndView(new RedirectView("Login.jlc"), model); // <-- notice this 

這將使你通過表達${model.MSG}和其他人訪問"Thank u"值,將它們添加到model地圖。