Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

pass a bean from jsp to action handler  RSS feed

 
guenter krannich
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
I'm using a bean as data container between action handler and jsp - works fine in that direction. In case the validation comes up with an error, the action handler sends the bean back to an "edit bean", where things may be corrected within a form and then the bean, now containing the right values, is to be sent to the action handler. I wanted to have the scope "request" as there might be several insert/modify cycles within a session. I understood that within the form a new request will be created so that I have to put the bean in the request attributes explicitly, so I put
<% request.setAttribute("beanname", bean); %>
into the jsp, but the according
request.getAttribute("beanname");
isn't successful!
Any idea what I've got wrong?
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, the request will end as soon as the page is drawn (right after you've bound the object to it). If you want the bean to survive to the next request, you'll have to store it in session or context scope. Alternately, you could serialize it's data into hidden input fields and use them to populate an new instance of the the bean.
 
guenter krannich
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks ben, I'll try the session approach!
But I mean there is a new request caused by the FORM ... stuff so I should be able to set attributes there; and the bean still seems to be alive, I'm feeding the INPUT fields with its properties:

<jsp:useBean id="task" class="gk.office.TaskBean" scope="request"/>
<html>
<LINK href="OfficeStyle.css" type=text/css rel=stylesheet>
<BODY>
<FORM ACTION="/Office/controller?action=ModifyTaskCommit" METHOD=POST>
<% request.setAttribute(Const.REQ_ATT_TASK, task); %>
<table>
<th class="title">Modify Task</th>
<tr class="attribute">
<td class="att">Name: <BR>
<font size=3>
<c ut value="${task.name}"/>
</font>
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The bean isn't still alive.
It's a new bean.
Put a System.out.println message in your bean's constructor.
And watch it write every time you submit a form.

You're adding the attribute to the request, right before the request is destroyed.
By the time you see the form in the browser, that request is gone.
The new request won't begin until you enter the form.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!