Hi, I am new to the spring framework and spring mvc. Here is my problem:
I have a jsp file that has 2 text values on the form. Their original values are coming from my dummyListController. DummyListCmd is being passed to the jsp file. Then, after I change the the text value, and hit the 'save' button, it will say the command object from the jsp is null at DummyListSaveController. (org..web.util.NestedServletExcepti on: Request processing failed; nested exception is org..beans.NullValueInNestedPathExc eption: Invalid property 'dummyList[0]' of bean class [edu.ucsd.act.ecorereport.web.form.DummyListCmd]: Cannot access indexed value in property referenced in indexed property path 'dummyList[0]': returned null
org..web.servlet.FrameworkServlet.p rocessRequest(FrameworkServlet.java:583)
org..web.servlet.FrameworkServlet.d oPost(FrameworkServlet.java:511)
javax.servlet.from.fromServlet.service(fromServlet .java:709)
javax.servlet.from.fromServlet.service(fromServlet .java:802)
)
Below is my jsp file.
lt;form:form modelAttribute=quot;dummyListCmdquot; action=quot;dummylist-save.htmquot; method=quot;postquot;gt;lt;div style=quot;text-align:right;margin-bottom:1em;quot;gt; lt;a hrcf=quot;#quot; onclick=quot;window.open('04.html','Window1','menubar= no,width=360,height=360,toolbar=no,scrollbars=yes, status=no,location=no');quot;gt;lt;ing src=quot;i/legend.gifquot; /gt;lt;/agt; lt;a hrcf=quot;#quot;gt;lt;ing src=quot;i/excel.gifquot; /gt;lt;/agt; lt;input type=quot;submitquot; id=quot;submitbuttonquot; value=quot;Savequot; /gt;
lt;/divgt;
lt;table id=quot;accountsquot;gt;
lt;tbodygt; lt;c:forEach var=quot;infoquot; varStatus=quot;vsquot; items=quot;${dummyListCmd.dummyList}quot;gt;
lt;trgt;
lt;tdgt;lt;form:input path=quot;dummyList[${vs.index}]quot; /gt;lt;/tdgt;
lt;/trgt;
lt;/c:forEachgt; lt;/tbodygt; lt;/tablegt;
lt;/divgt;
lt;/form:formgt;DummyListController.java:
@Controller
public class DummyListController {
@Autowired
private FundInfoMgr fundInfoMgr;
final Log logger = LogFactory.getLog(getClass());
// this is the controller to list all fund info
@RequestMapping(quot;/dummylist.htmquot;)
public ModelAndView DummyList(fromServletRequest req, ModelMap model)
throws Exception {
//
DummyListCmd dummyListCmd = new DummyListCmd();
Listlt;Stringgt; dummyList= new ArrayList();
dummyList.add(quot;aquot;);
dummyList.add(quot;bquot;); dummyListCmd.setDummyList(dummyList);
model.addAttribute(quot;dummyListCmdquot;, dummyListCmd);
return new ModelAndView(quot;dummylistquot;, model);
}
}
my DummyListSaveController.java:
public class DummyListSaveController {
@Autowired
private FundInfoMgr fundInfoMgr;
final Log logger = LogFactory.getLog(getClass());
// this is the controller to list all fund info
@RequestMapping(quot;/dummylist-save.htmquot;)
public ModelAndView DummyListSave(fromServletRequest req, SessionStatus status,
@ModelAttribute(quot;dummyListCmdquot;) DummyListCmd dummyListCmd,
BindingResult bindResult, ModelMap model)
throws Exception {
//
System.out.println(quot;test!!!!quot;);
Enumerationlt;Stringgt; e = req.getAttributeNames();
while(e.hasMoreElements()){
System.out.println(quot;--quot; +e.nextElement());
}
//System.out.out(req.getAttributeNames())
System.out.println(quot;dummy list isquot; +dummyListCmd.getDummyList().toString());
Listlt;Stringgt; dummyList= new ArrayList();
dummyList.add(quot;aquot;);
dummyList.add(quot;bquot;); dummyListCmd.setDummyList(dummyList);
model.addAttribute(quot;dummyListCmdquot;, dummyListCmd);
return new ModelAndView(quot;dummylistquot;, model);
}
}Why is my dummyListCmd is null??? Thanks for your help. Ping
Hi, Can somebody share Spring 2 java and jsp code to show how we can have a form with input values coming from the controller (the data can be from db, i.e.), then a user changes it and another controller saves the changes? The jsp also uses Spring form tag library.
Thanks a lot!!!
Ping
Hi Ping,
Why do you need to have 2 different controllers do perform a GET/POST sequence in a form?
Wouldn't it be easier to have the 2 @RequestMapping in the same controller?
The Exception you get comes from the fact that Spring tries to bind data coming from a List but the binding list has no elements.
If during a form GET/POST you want to keep the original object,thus the initialized list, you can use @AttributeSession at the top of the controller
Fred
Hi Fred,Thanks for your reply. You are absolutely right, I didn't have to use 2 controllers. I put them together now. Regarding the command object being null: I noticed that if my cmd object is a list of strings, like:
DummyListCmd dummyListCmd = new DummyListCmd();
Listlt;Stringgt; dummyList= new ArrayList();
dummyList.add(quot;aquot;);
dummyList.add(quot;bquot;); dummyListCmd.setDummyList(dummyList);
the null problem appears. If my cmd object has two strings like: DummyCmd dummyCmd = new DummyCmd(); dummyCmd.setA(quot;pquot;); dummyCmd.setB(quot;qquot;);
Just two plain string fields, then the null problem does NOT appear. So, I was wondering if my jsp file has a problem binding list with cmd object. Are you aware in Spring binding cmd obj with a list requires special care?
Also, I was going to use @SessionAttributes as you said, but I don't really want to save the cmd object in the session because a user can change it, and I want the cmd object to dynamically bind with whatever the user enters. Maybe I don't understand that annotation well..I will read upon it more.
Thanks....and looking forward to your response.
Ping
Hi Ping,
In your GET you set your command object with the data you want in it here
Listlt;Stringgt; dummyList= new ArrayList();
dummyList.add(quot;aquot;);
dummyList.add(quot;bquot;);
In your POST you ask for an object of type your command object using the quot;@ModelAttribute(quot;dummyListCmdquot;) DummyListCmd dummyListCmdquot; in the method signature.
I think if this object hasn't been initialized yet then a new object is initialized and used to bind the parameters from the request. This explains why you don't have any problems with objects not contained in the List. Because with the list you specifically gave an index in your JSP:
lt;form:input path=quot;dummyList[${vs.index}]quot; /gt;
This means that the name in a request parameter will look like for instance dummyList[0] and will be bound to a list at the index 0. However since a new object was initialized your list is empty or null so you get an exception.
One solution would be to store your command object in the session for the time of the GET/POST flow. This is the role of the @AttributeSession. It will store what you want and by using SessionStatus.setComplete() will let you clear it. Using @AttributeSession(quot;dummyListCmdquot;) means that in the POST, the object being bound will be the one from the session if you don't do anything else.
An other solution could be to declare a method with an @ModelAttribute method annotation as this method will be called before the @RequestMapping decorated methods, thus giving you the opportunity to setup your command object the way you want.
Hi Fred, Thanks for your help. I solved the problem by using 'LazyList' for my list. Here is the link I read.
node/134Best Regards,
Ping
Hi Fred, Thank you so much for your help! I did some reading and found my problem. I had to declare my list as a LazyList instead of List in my command object, and that just took care of the problem.
Here is the reference:
node/134
Ping |