0

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");
         }
3
  • but where u getting error i mean just make some changes in question and tell where in point u getting error? Commented Sep 24, 2014 at 4:58
  • System.out.println("bf" + rs.getAttribute("customId")); in this line getting null value Commented Sep 24, 2014 at 5:14
  • 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. Commented Sep 24, 2014 at 5:51

1 Answer 1

0

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 !

Sign up to request clarification or add additional context in comments.

8 Comments

haseem I was trying that only previously but getting null at this line System.out.println("xxx" + session.getAttribute("customId"));
@NirrajSingh that line is not mentioned in your code !
@NirrajSingh it means you don't have session stored in the server ! that's why you got null , make sure the session is created !
@Hatem i think the problem is not about what u have given description. this could be solution.. @ModelAttribute("xyz") where xyz will be model attribute that u sending from Jsp or UI page.
Hatem you mean to saythe name of variable xyz should be same as in jsp
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.