File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes struts.xml doubt (Action classes,struts-2) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "struts.xml doubt (Action classes,struts-2)" Watch "struts.xml doubt (Action classes,struts-2)" New topic
Author

struts.xml doubt (Action classes,struts-2)

Pradeep Adibatla
Ranch Hand

Joined: Oct 27, 2009
Posts: 336
I have 2 jsp's say Index.jsp and lookup.jsp. In index.jsp I have an empty table I add 3 rows and save it.Now the table has 3 rows. I have 2 buttons 'Save' an 'GoToLookUp'.
I saved the modifications by 'Save' button and I click 'GoToLookUp' button to go to lookup.jsp .This is achieved by returning 'success' from IndexAction Class and setting in

struts.xml as :

Now,in lookup.jsp , I have 2 buttons 'Save' and 'Back'. The issue is with 'Back' button...

When I click back button I am going back to Index.jsp ,but index.jsp being in same position as it was intially(empty table).When i click 'back', I have to go back to Index.jsp and it should show the modified thing i.e table with 3 rows saved.

In struts.xml for LookupAction class ,on 'Back' click I returned success and give it as



I think something should be done for the above statement as it is just taking me to index.jsp which is at it's original state...

How to go back to midified state of Index.jsp??


Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9307
    
  17

When you add 3 rows to index.jsp and click save, where are these values stored?? Are they stored in a database??


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Pradeep Adibatla
Ranch Hand

Joined: Oct 27, 2009
Posts: 336
No only in the UI... while adding the row itself I am adding the elements to DB... I just need to save them for the user to view those on that screen...
Lorand Komaromi
Ranch Hand

Joined: Oct 08, 2009
Posts: 276
Pradeep Adibatla wrote:
How to go back to midified state of Index.jsp??


A jsp's purpose is only to render the page, it has no "state". You have to persist the added rows somewhere before they are saved to the DB, e.g. the session.


OCJP 6 (93%)
Pradeep Adibatla
Ranch Hand

Joined: Oct 27, 2009
Posts: 336
how do I achieve that session? should I Implement session aware interface and do it?
How does it happen in struts-2? direct me to any documents or provide a way... I have no idea how to achieve it.It's biting me big time!!!
Lorand Komaromi
Ranch Hand

Joined: Oct 08, 2009
Posts: 276
Pradeep Adibatla wrote:how do I achieve that session? should I Implement session aware interface and do it?


Yes, that's how you could access the session object.

Pradeep Adibatla wrote:No only in the UI... while adding the row itself I am adding the elements to DB...


It isn't clear to me when those rows are saved to the DB. If they are inserted before the user goes to the lookup page, there's no need to put them into the session, when the user clicks back, you could load them from the DB...
Pradeep Adibatla
Ranch Hand

Joined: Oct 27, 2009
Posts: 336
When the user edits a page and comes back, index.jsp will read the rows from the session and display them



This means that I should write code for session in jsp to read and get rows from Action class and display?

Whew! my confusion is still tight...

1) row is added and ajax call is made and it calls the action class.
2) yes,i will store the values in a session.


case 1:--->

on GoToIndex button click
This is an example flow: login.jsp----------------------------------->index.jsp (now, at this point of index.jsp should it include scriptlet for session? If yes,it can't do anything as I have no values...adding dynamic rows,storing in DB are yet to be done!!)


case2:----> Ok,At this point I have added rows dynamicaaly,stored values in DB via Ajax server call,stored the values in session...

on 'Back' button click
index.jsp--------->lookup.jsp------------------------------->index.jsp(now this should be able to read values from session and display them in DB)


index.jsp is still unclear with respect to cases 1 and 2 !!!







Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9307
    
  17

Basically what you need to do is, when a new row is added dynamically to the index.jsp page, then make an AJAX call to the server. The AJAX call will call an action which will store the row in the session. When the user edits a page and comes back, index.jsp will read the rows from the session and display them. Reading the values from session is fairly simple using JSTL (c:forEach etc)...
Pradeep Adibatla
Ranch Hand

Joined: Oct 27, 2009
Posts: 336
When the user edits a page and comes back, index.jsp will read the rows from the session and display them



This means that I should write code for session in jsp to read and get rows from Action class and display?

Whew! my confusion is still tight...

1) row is added and ajax call is made and it calls the action class.
2) yes,i will store the values in a session.


case 1:--->

on GoToIndex button click
This is an example flow: login.jsp----------------------------------->index.jsp (now, at this point of index.jsp should it include scriptlet for session? If yes,it can't do anything as I have no values...adding dynamic rows,storing in DB are yet to be done!!)


case2:----> Ok,At this point I have added rows dynamicaaly,stored values in DB via Ajax server call,stored the values in session...

on 'Back' button click
index.jsp--------->lookup.jsp------------------------------->index.jsp(now this should be able to read values from session and display them in DB)


index.jsp is still unclear with respect to cases 1 and 2 !!!







Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9307
    
  17

I couldn't understand your confusion properly and as Lorand said, its not clear when the rows are actually added to the database. Let me explain you this very clearly. Suppose I'm the first user on your site. There's no row in the database or session. Now I add two rows to the index.jsp dynamically. The AJAX call will add these rows to the session. Now when I press 'GoToLookUp' button, it will take me to lookup.jsp. There I press the back button, I'm taken to the index.jsp. Index.jsp can directly access values from the session (added by the ajax calls). There's no need for any action to do anything right now. You can access values of the session using scriptlets or JSTL and EL (recommended).

I come to the site after 5 days, the rows that I added last time are in the Database. When I first open the site, an action will get the rows from the Database and load them into the session. Then the action will forward the request to index.jsp. Index.jsp just has to display the rows from the session (as it did was back was clicked from lookup.jsp). Rest is the same as the previous case.

Storing too many values in Session might slow down your site, but since there isn't much details available to what you want to achieve, this is the best strategy that I could recommend...
Pradeep Adibatla
Ranch Hand

Joined: Oct 27, 2009
Posts: 336
Sorry long time... but would clarify that!

The rows are added to DB at the same time when row is added to table. 'Save' only saved the current status of UI(perhaps it should mean I store them in session now, and on 'back' I should retrieve values from session using JSTL and show them...
Pradeep Adibatla
Ranch Hand

Joined: Oct 27, 2009
Posts: 336

Long time,so please trace back for details...


I have a 2 objects... concept object which has concepts(conceptNames) and Attribute object which has attributes and datatypes...

These 3 form my row in the table... So my row would sample as : ROW1: concept1 attribute1 datatype1

Each row represents one vocabulary object i.e attributes are added to concepts and concepts are inturn stored in vocabulary object...

How to store in a session?

I implemented sessionAware in my action class... (i am not clear whether to use application or session Aware)







session.put(???) what key, value pair should go? vocabulary?


Ok,I go ahead like this...

I created objects for concept,attribute and vocabulary and say,

--- a methos which sets up vocabulary...

and thn ,

session.put("vocbularies",vocabulary);
request.setAttribute("vocabulary",vocabulary);

is this correct?? Now my problem is how how to read using OGNL??


My whole table(tr,td) is in control of Javascript... If I go back and visit the previous page,how will I manage my jsp to read them from session
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9307
    
  17

Pradeep please don't cross post the same thing in more than one thread. You asked the same (almost same) thing in this thread. It gets very confusing like this.
Now my problem is how how to read using OGNL??

Why are you storing these values in the session?? If all that you want is to access the values in the JSP, then you can put these values as properties in the action class and access them using OGNL in your JSP. If you want to access session/request attributes in your JSP, then you can use EL i.e. ${attributeName} or ${sessionScope.attributeName} (for session attributes) or ${requestScope.attributeName} (for request attributes)...
Pradeep Adibatla
Ranch Hand

Joined: Oct 27, 2009
Posts: 336
I wanted to delete that post but don't know how to do it...

Well, Ankit, My big problem :


The table is in total control of JS... I cant access using property as my TR,TD are in JS... so what should be done here?

can I manage without a session?





Pradeep Adibatla
Ranch Hand

Joined: Oct 27, 2009
Posts: 336
Yes,I am afraid,I have to use session...

so what I am doing is

I am calling this function to create TR and TD and set the values from session to the table...is it the right procedure or anyother (easier) way
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: struts.xml doubt (Action classes,struts-2)