Spring获取属性方法中的错误

时间:2014-09-24 04:50:54

标签: java spring

我希望在bean的帮助下在第一个modelandview方法中设置属性,并尝试在同一个控制器中获取其他modelandview方法中的属性,但是获取空值我的代码在下面

@RequestMapping(值=" /插入",方法= RequestMethod.POST)

        public ModelAndView inserData(@ModelAttribute    SavingBeansavingBean,HttpServletRequestrs,Model m) {


 System.out.println(savingBean.getFirstName());





 if (savingBean != null)
  System.out.println("abho");
 SavingBean saving =  persionalService.insertData(savingBean);

 custid = saving.getCustomerId();
System.out.println(custid);
m.addAttribute("customId",saving);


 System.out.println(saving.getDisgnProf());

 List<SavingBean> list = new ArrayList<SavingBean>();
 list.add(saving);

 return new ModelAndView("AccountInfo","list", list);

}      @RequestMapping(值=&#34; / accinsert&#34;,方法= RequestMethod.POST)      public ModelAndView inserData(@ModelAttribute AccountBean accbean,HttpServletRequest rs,Model m){

 SavingBean b = new SavingBean();

System.out.println("saas" +  b.getCustomerId());
session = rs.getSession();


System.out.println("xxx" + rs.getAttribute("customId"));
accbean.setCustid((Long) rs.getAttribute("customId"));
 AccountBean accbean1 = persionalService.insertacc(accbean);

 return new ModelAndView("welcome");
         }

1 个答案:

答案 0 :(得分:0)

从第一眼看到你的代码,我注意到你的请求方法没有指定。在这种情况下(使用@ModelAttribute时),您必须将其作为(POST)请求。

@RequestMapping(value = "/insert", method = RequestMethod.POST)
@RequestMapping(value = "/accinsert" , method = RequestMethod.POST)

为什么?因为实际上你的对象将被检索,因为Form Submission被视为POST请求。尝试那件事并检查结果。如果问题仍然存在,那么您的表示层(例如JSP页面)中有一些真正的问题需要负责提交数据。

祝你好运!