开发者

java session bean - reload data?

Here's what I want to do. It seems simple, but I can't get it to work. JSP1 - user fills out form, submits to JSP2. JSP2 populates the form values in a Bean and displays data, and offers user option to return and modify (history.back()), or submit to Servlet. I've come up with three different options, and each has problems.

OPTION 1: JSP1 - standard html form, submits to JSP2

<form name="testform" method="post" action="jsp2.jsp">
...
City: <input name="currentCity" type="text" />

JSP2 -

<jsp:useBean id="workorder" type="com.mycompany.app.WorkorderBean" class="com.mycompany.app.WorkorderBean" scope="request">
    <jsp:setProperty name="workorder" property="*" />
</jsp:useBean>
...
currentCity: ${workorder.currentCity}

Problem -when JSP2 submits to controller, and I call WorkorderBean workorder = (WorkorderBean) request.getAttribute("workorder"); it returns null. So 'scope=request' doesn't make it from JSP to servlet.

OPTION 2: Same scenario, but on JSP2 use 'scope=session'.

Problem: when the user chooses to go back to JSP1 and modify data, then re-submits to JSP2, JSP2 does not use the new values, because it already has the bean as a session bean.

Question: Is there a way I can clear out the session bean when I submit from JSP1? I don't think I can do this with Javascript.

OPTION 3: Have JSP1 submit to Servlet, which formats the session bean and sends to JSP2.

Problem: When user choose to go back from JSP2 to JSP1 to make change开发者_Go百科s, all data is lost in the form.

How can I make this work?


The best thing to do, is to have a form with hidden fields on the second page.

And then on the first form, you should have it look at the request for entered data.

So, you end up with this:

GET /jsp1

Shows initial form, empty. It then POSTs to JSP2

POST /jsp2
fname=Frank&lname=Jones&city=Atlanta

jsp2 then REDIRECTs back to itself:

GET /jsp2?fname=Frank&lname=Jones&city=Atlanta

And it displays those values.

If the user hits BACK, then they see their original work. If you provide them with a "Go Back" link, and it's a real link, it should be like JSP2, only instead it's JSP1:

GET /jsp1?fname=Frank&lname=Jones&city=Atlanta

If they choose to continue, then you POST to the Servlet:

POST /servlet
fname=Frank&lname=Jones&city=Atlanta

On JSP2 you have a hidden form:

<form action="/servlet" method="POST">
    <input type="hidden" name="fname" value="Frank"/>
    ...
    <input type="submit" ... />
</form>

Then when it's done, the Servlet will REDIRECT to whatever JSP it wants to show.

Fundamentally, you only want the browser to display things that it GETs, rather than results of POSTs. If you've ever seen when you hit a back button and you get that dialog "do you want to resubmit this query", that's because the results of the previous page are from a POST, not a GET.

You use parameters in your requests to help carry the state forward. This keeps the information out of your session and lets things like the "refresh" button work properly for the user.


Possible solution for option 1:

In JSP2 put the request parameters in hidden form fields so that they can be submitted to your servlet. In the servlet you have to read the request parameters manually. But you need no session and you can go back from JSP2 to JSP1, change some values, submit to JSP2 again. Then a submit on JSP2 transfers the changed values to the servlet.

Update:

In your second option: The reason why the bean properties remains unchanged after a resubmit is, that you placed the jsp:setProperty Tag inside of jsp:useBean. With this constellation jsp:setProperty is only called at bean creation. During the second call of JSP2 the bean workorderalready exists in the session scope and no jsp:setProperty call happens.

You can change that behaviour if you place the jsp:setProperty Tag outside of jsp:useBean:

<jsp:useBean id="workorder" type="com.mycompany.app.WorkorderBean"
   class="com.mycompany.app.WorkorderBean" scope="session" />
<jsp:setProperty name="workorder" property="*" />  

Then jsp:setProperty will be called at every JSP2 call and overwrite the bean properties with the request parameters.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜