0
votes

I want setting the attribute in first modelandview method with the help of bean and trying to get the attributes in other modelandview method in same controller but getting null value my code is below

@RequestMapping(value="/insert",method=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(value="/accinsert",method=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
but where u getting error i mean just make some changes in question and tell where in point u getting error? - Kishan Bheemajiyani
System.out.println("bf" + rs.getAttribute("customId")); in this line getting null value - Nirraj Singh
okey but u need to give model attribute name like this @ModelAttribute("xyz") where xyz will be model attribute that u sending from Jsp or UI page. - Kishan Bheemajiyani

1 Answers

0
votes

From the first look to your code , I notice that your request method not specified. At this case (When using @ModelAttribute) you have to make it as (POST) request.

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

Why ? because actually your object will be retrieved due to Form Submission which is treated as POST request. Try that thing and check the results. If the problem is still maybe you have some real problem in your Presentation Layer (e.g JSP Page) that is responsible about submitting the data.

Good Luck !