Best practice question: Properties (ShoppingCart) per user
Hi, I'm currently writing a short Struts application without a database (I know that this would be better, but as it is only for teaching purposes, I don't want to 'overflow' my audience with databases and ORM). It should be a small online sales system -- I've got ShoppingItems, ShoppingUsers (which are not yet used) and ShoppingCarts. I want a ShoppingCart which stores the ordered items per user. I had something like this: public class ShoppingCartAction extends DispatchAction { private ShoppingCartManager shoppingCartManager; ... public ActionForward order(...) ... public ActionForward list(...) ... } and thought that it would work. Unfortunately, each user (which is not authenticated or anything yet) gets the same shopping cart when he calls shoppingCart.do?method=list. So I did some research and found this: http://www.jguru.com/faq/view.jsp?EID=1057609 So I created an ShoppingCartMapping class public class ShoppingCartMapping extends ActionMapping { private ShoppingCartManager shoppingCartManager; ... } and call it like this public ActionForward list(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { request.setAttribute(orderedItems, ((ShoppingCartMapping)mapping).getShoppingCartManager().getItems()); return mapping.findForward(list); } which gives me a ClassCastException. Do I have to set my custom ActionMapping in web.xml or struts-config.xml? Am I on the right way, or is there a better way of doing this? Thanks in advande and kind regards, Simon PS: Please cc me in your answers. -- GMX ProMail mit bestem Virenschutz http://www.gmx.net/de/go/mail +++ Empfehlung der Redaktion +++ Internet Professionell 10/04 +++ - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: Best practice question: Properties (ShoppingCart) per user
Simon, Why would the shopping cart be in the ActionMapping? I would think it belongs in the session or new data for the cart would come in to the FORM. Regards, David -Original Message- From: Simon MARTIN [mailto:[EMAIL PROTECTED] Sent: Tuesday, December 07, 2004 7:01 AM To: [EMAIL PROTECTED] Subject: Best practice question: Properties (ShoppingCart) per user Hi, I'm currently writing a short Struts application without a database (I know that this would be better, but as it is only for teaching purposes, I don't want to 'overflow' my audience with databases and ORM). It should be a small online sales system -- I've got ShoppingItems, ShoppingUsers (which are not yet used) and ShoppingCarts. I want a ShoppingCart which stores the ordered items per user. I had something like this: public class ShoppingCartAction extends DispatchAction { private ShoppingCartManager shoppingCartManager; ... public ActionForward order(...) ... public ActionForward list(...) ... } and thought that it would work. Unfortunately, each user (which is not authenticated or anything yet) gets the same shopping cart when he calls shoppingCart.do?method=list. So I did some research and found this: http://www.jguru.com/faq/view.jsp?EID=1057609 So I created an ShoppingCartMapping class public class ShoppingCartMapping extends ActionMapping { private ShoppingCartManager shoppingCartManager; ... } and call it like this public ActionForward list(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { request.setAttribute(orderedItems, ((ShoppingCartMapping)mapping).getShoppingCartManager().getItems()); return mapping.findForward(list); } which gives me a ClassCastException. Do I have to set my custom ActionMapping in web.xml or struts-config.xml? Am I on the right way, or is there a better way of doing this? Thanks in advande and kind regards, Simon PS: Please cc me in your answers. -- GMX ProMail mit bestem Virenschutz http://www.gmx.net/de/go/mail +++ Empfehlung der Redaktion +++ Internet Professionell 10/04 +++ - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Best practice question: Properties (ShoppingCart) per user
You asked this same question last week... I'll try and give you some specific answers this time... (1) You have a class member (shoppingCartManager) in your Action. This is bad. Here's a direct quote from the Struts User's Guide: The controller servlet creates only one instance of your Action class, and uses it for all requests. Thus, you need to code your Action class so that it operates correctly in a multi-threaded environment, just as you must code a servlet's service method safely. The most important principle that aids in thread-safe coding is to use only local variables, not instance variables, in your Action class. Local variables are created on a stack that is assigned (by your JVM) to each thread request, so there is no need to worry about sharing them. In other words, multiple requests can and usually will see the same class members or an Action, which is almost certainly why you were seeing that behavior. You have some options here. The one I would personally go for is to create a ShoppingCart class that has the typical addItem, removeItem, listItems methods. Put this in session for a given user (probably the first request they make should check to see if such an object exists in session yet, and if not, put one in there). From then on you can get the object from session, do what you need to with it, put it back (if changes were made) and go from there. This class should be ignorant of the fact that it's running as part of a webapp... To really do it properly you probably also want to create a ShoppingCartItem class that represents a single item, and add instances of that class to the cart. But, for simplcity's sake you can not bother with that. As an alternative, you might also consider making the shopping cart a Struts ActionForm, which you can put in session declaratively in struts-config.xml. This will relieve you of some coding dealing with session, but for reasons I'll get to in a moment, I'd say not to do this... (2) I'm not really sure what you were trying to do with the ActionMapping class... It looks like you were trying to return it to your view (a JSP) to display. If that's the case, what you want to do is one of two things, assuming you do what I said in #1 above... (a) get the ShoppingCart instance from session, call listItems() (probably returns an ArrayList or some other collection), shove it in request as an attribute and forward to your JSP, then just display it as usual, or (b) populate an ActionForm instance, which you get as an argument to execute(), with the results of the call to listItems(). (b) is probably the more correct way of doing it, but either will work fine. In general, an ActionForm can be thought of as a data passing mechanism between the view and control layer. In this sense, it's really not proper in my opinion to store shopping cart-like data in it, even if it does make things a little simpler. So, while you COULD have Struts always put the ActionForm in session, and design it in such a way that it is really a storage location for your shopping cart data, I would advise against that because it's not really proper I think. I think those two points will put you on the right track if you understand what I'm saying. Write back if you have specific questions, I'll try and answer them. -- Frank W. Zammetti Founder and Chief Software Architect Omnytex Technologies http://www.omnytex.com Simon MARTIN wrote: Hi, I'm currently writing a short Struts application without a database (I know that this would be better, but as it is only for teaching purposes, I don't want to 'overflow' my audience with databases and ORM). It should be a small online sales system -- I've got ShoppingItems, ShoppingUsers (which are not yet used) and ShoppingCarts. I want a ShoppingCart which stores the ordered items per user. I had something like this: public class ShoppingCartAction extends DispatchAction { private ShoppingCartManager shoppingCartManager; ... public ActionForward order(...) ... public ActionForward list(...) ... } and thought that it would work. Unfortunately, each user (which is not authenticated or anything yet) gets the same shopping cart when he calls shoppingCart.do?method=list. So I did some research and found this: http://www.jguru.com/faq/view.jsp?EID=1057609 So I created an ShoppingCartMapping class public class ShoppingCartMapping extends ActionMapping { private ShoppingCartManager shoppingCartManager; ... } and call it like this public ActionForward list(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { request.setAttribute(orderedItems, ((ShoppingCartMapping)mapping).getShoppingCartManager().getItems()); return mapping.findForward(list); } which gives me a ClassCastException. Do I have to set my custom ActionMapping in web.xml or struts-config.xml? Am I on the right way, or is there a better
Re: Best practice question: Properties (ShoppingCart) per user
Though I've used Struts quite a bit in the past, I found this freely downloadable book a very good source of '101' Struts information: http://www.theserverside.com/books/sourcebeat/JakartaStrutsLive/index.tss Mike Frank W. Zammetti [EMAIL PROTECTED] wrote: Sounds good! You'll find that the people here are extremely helpful, but they do tend to shy away from general questions (unless they are SO general as to be theoretical, in which case EVERYONE has an opinion :) ), so the more specific a question you can ask, the better. As for books to read, I would highly recommend grabbing the latest copy of Head First Servlets and JSP. It's not Struts-specific, but it is really necassery to know what is going on beneath Struts, and it is the best I've seen in this area (I'm currently using it as a study guide for the SCWCD exam, but it's good for anyone I'd say). Frankly, if you get everything in that book, most of Struts will make sense almost immediately. For Struts, O'Reilly has a pretty good book, the title of which elludes me at the moment, but I suspect it's the only one they sell. At times it goes into a little TOO much detail for people trying to get a start in Struts, but most of it is excellent. As for your specific architecture questions... Generally speaking, your concern about putting all application state for a user in session is valid. This isn't what session is for. It's really for fairly small BITS of state information, things that will in all likelihood change frequently, and certainly nothing that should persist beyond a session (hence the name!). But, in your case, since you are just using it as an example, I wouldn't worry about it, go ahead and use session, it'll be easier. In Struts, Actions can be shared across requests, which means that if you have class-level variables, they are, potentially, shared across multiple requests. Clearly this is a formula for disaster. There are two ways you could go here... one is to use session as I said, the other is to have a data storage class that just has some static members, most probably a Map of some sort. You can use this as something of a poor man's database. Think of a HashMap for instance as a database table. Now, assume each element you add to the HashMap is itself a HashMap. You can think of these inner HashMaps as rows in the table. Then, each element in that inner HashMap is a field in the table. Make sense? You can go so far as to create insertRow(), deleteRow() and updateRow() methods for your data storage class... These would do the functions their name implies on the HashMap, and you can in that way key your data, i.e., each row has an element recordKey let's say. So, your insert method can throw an exception if a record exists with that key already. In fact, you could go so far as to throw a SQLException, and in that way you could replace that data storage class with a class that talks to a database down the road, and nothing that uses that class should have to change. But that's getting ahead of ourselves a bit :) If you intend to build something that you know would usually use a database, then the second approach is a good idea. Be sure to synchronize your access to your Maps (which would be a bottleneck in a real application, but you can get away with it here, and in general in any application that isn't going to have a big load). But, using session might be easier, depends on how much work (and control) you want. For a shopping cart, the volume of data isn't going to be great, so no worries there. But, the golden rule with Struts to remember is no class-level variables! That's what all this is designed to avoid. -- Frank W. Zammetti Founder and Chief Software Architect Omnytex Technologies http://www.omnytex.com Simon MARTIN wrote: Hi, thanks for your offering of help. I know that my Struts knowlegde is not the best yet -- no question about it -- but I'm willing to change that, as far as my time allows it. Fortunately, my lecture is not of that importance you might have assumed, as I'm 'only' giving a presentation at school. However, I of course want to present something I know about, so this does not really change the situation. As I can see what you're telling me, I think my learning has been to practice-orientated and too less theory-orientated, which brings me into trouble when the requirements change. So far, I've only strictly written Spring apps with Struts MVC or without Struts MVC, but, in any case, with Hibernate as ORM-framework. Therefore, I really came into troubles when using Struts alone, and then even doing so without a database. Of course, I had thought about putting the stuff into the session, either, but putting complete client-state into the session seemed unclean to me, as I personally had prefered some kind of property-setting statically. Please let me know about further architectural mistakes I've done. I've
Re: Best practice question: Properties (ShoppingCart) per user
good answer- I would also strongly encourage reading Struts in Action by Ted Husted if you Read the foreword from Craig McClanahan you'll want to read this book cover to cover Martin - Original Message - From: [EMAIL PROTECTED] To: Struts Users Mailing List [EMAIL PROTECTED] Sent: Thursday, December 09, 2004 10:21 AM Subject: Re: Best practice question: Properties (ShoppingCart) per user For Struts, O'Reilly has a pretty good book, the title of which elludes me at the moment, Programming Jakarta Struts 2nd ed - Chuck Cavaness Stuts The Complete Reference - James Holmes was the first Struts book I read and was very helpful. Each chapter gave a good overview before getting into the details and the code example was simple and manageable. Bart Frank W. Zammetti [EMAIL PROTECTED] wrote on 12/08/2004 11:11:01 PM: Sounds good! You'll find that the people here are extremely helpful, but they do tend to shy away from general questions (unless they are SO general as to be theoretical, in which case EVERYONE has an opinion :) ), so the more specific a question you can ask, the better. As for books to read, I would highly recommend grabbing the latest copy of Head First Servlets and JSP. It's not Struts-specific, but it is really necassery to know what is going on beneath Struts, and it is the best I've seen in this area (I'm currently using it as a study guide for the SCWCD exam, but it's good for anyone I'd say). Frankly, if you get everything in that book, most of Struts will make sense almost immediately. For Struts, O'Reilly has a pretty good book, the title of which elludes me at the moment, but I suspect it's the only one they sell. At times it goes into a little TOO much detail for people trying to get a start in Struts, but most of it is excellent. As for your specific architecture questions... Generally speaking, your concern about putting all application state for a user in session is valid. This isn't what session is for. It's really for fairly small BITS of state information, things that will in all likelihood change frequently, and certainly nothing that should persist beyond a session (hence the name!). But, in your case, since you are just using it as an example, I wouldn't worry about it, go ahead and use session, it'll be easier. In Struts, Actions can be shared across requests, which means that if you have class-level variables, they are, potentially, shared across multiple requests. Clearly this is a formula for disaster. There are two ways you could go here... one is to use session as I said, the other is to have a data storage class that just has some static members, most probably a Map of some sort. You can use this as something of a poor man's database. Think of a HashMap for instance as a database table. Now, assume each element you add to the HashMap is itself a HashMap. You can think of these inner HashMaps as rows in the table. Then, each element in that inner HashMap is a field in the table. Make sense? You can go so far as to create insertRow(), deleteRow() and updateRow() methods for your data storage class... These would do the functions their name implies on the HashMap, and you can in that way key your data, i.e., each row has an element recordKey let's say. So, your insert method can throw an exception if a record exists with that key already. In fact, you could go so far as to throw a SQLException, and in that way you could replace that data storage class with a class that talks to a database down the road, and nothing that uses that class should have to change. But that's getting ahead of ourselves a bit :) If you intend to build something that you know would usually use a database, then the second approach is a good idea. Be sure to synchronize your access to your Maps (which would be a bottleneck in a real application, but you can get away with it here, and in general in any application that isn't going to have a big load). But, using session might be easier, depends on how much work (and control) you want. For a shopping cart, the volume of data isn't going to be great, so no worries there. But, the golden rule with Struts to remember is no class-level variables! That's what all this is designed to avoid. -- Frank W. Zammetti Founder and Chief Software Architect Omnytex Technologies http://www.omnytex.com Simon MARTIN wrote: Hi, thanks for your offering of help. I know that my Struts knowlegde is not the best yet -- no question about it -- but I'm willing to change that, as far as my time allows it. Fortunately, my lecture is not of that importance you might have assumed, as I'm 'only' giving a presentation at school. However, I of course want to present something I know about, so this does not really change the situation. As I can see what you're telling me, I
Re: Best practice question: Properties (ShoppingCart) per user
agree, I've read the O'reilly struts book and Struts in Action. The Oreilly is more WHAT you can do, where Struts in Action is takes a more practical approach. Mr. Husted has already sorted out the good vs bad approaches for you. A good read. Henrik Bentel Laconia Data Systems [EMAIL PROTECTED] 12/09/04 09:31 AM Please respond to Struts Users Mailing List To: Struts Users Mailing List [EMAIL PROTECTED] cc: Subject:Re: Best practice question: Properties (ShoppingCart) per user good answer- I would also strongly encourage reading Struts in Action by Ted Husted if you Read the foreword from Craig McClanahan you'll want to read this book cover to cover Martin - Original Message - From: [EMAIL PROTECTED] To: Struts Users Mailing List [EMAIL PROTECTED] Sent: Thursday, December 09, 2004 10:21 AM Subject: Re: Best practice question: Properties (ShoppingCart) per user For Struts, O'Reilly has a pretty good book, the title of which elludes me at the moment, Programming Jakarta Struts 2nd ed - Chuck Cavaness Stuts The Complete Reference - James Holmes was the first Struts book I read and was very helpful. Each chapter gave a good overview before getting into the details and the code example was simple and manageable. Bart Frank W. Zammetti [EMAIL PROTECTED] wrote on 12/08/2004 11:11:01 PM: Sounds good! You'll find that the people here are extremely helpful, but they do tend to shy away from general questions (unless they are SO general as to be theoretical, in which case EVERYONE has an opinion :) ), so the more specific a question you can ask, the better. As for books to read, I would highly recommend grabbing the latest copy of Head First Servlets and JSP. It's not Struts-specific, but it is really necassery to know what is going on beneath Struts, and it is the best I've seen in this area (I'm currently using it as a study guide for the SCWCD exam, but it's good for anyone I'd say). Frankly, if you get everything in that book, most of Struts will make sense almost immediately. For Struts, O'Reilly has a pretty good book, the title of which elludes me at the moment, but I suspect it's the only one they sell. At times it goes into a little TOO much detail for people trying to get a start in Struts, but most of it is excellent. As for your specific architecture questions... Generally speaking, your concern about putting all application state for a user in session is valid. This isn't what session is for. It's really for fairly small BITS of state information, things that will in all likelihood change frequently, and certainly nothing that should persist beyond a session (hence the name!). But, in your case, since you are just using it as an example, I wouldn't worry about it, go ahead and use session, it'll be easier. In Struts, Actions can be shared across requests, which means that if you have class-level variables, they are, potentially, shared across multiple requests. Clearly this is a formula for disaster. There are two ways you could go here... one is to use session as I said, the other is to have a data storage class that just has some static members, most probably a Map of some sort. You can use this as something of a poor man's database. Think of a HashMap for instance as a database table. Now, assume each element you add to the HashMap is itself a HashMap. You can think of these inner HashMaps as rows in the table. Then, each element in that inner HashMap is a field in the table. Make sense? You can go so far as to create insertRow(), deleteRow() and updateRow() methods for your data storage class... These would do the functions their name implies on the HashMap, and you can in that way key your data, i.e., each row has an element recordKey let's say. So, your insert method can throw an exception if a record exists with that key already. In fact, you could go so far as to throw a SQLException, and in that way you could replace that data storage class with a class that talks to a database down the road, and nothing that uses that class should have to change. But that's getting ahead of ourselves a bit :) If you intend to build something that you know would usually use a database, then the second approach is a good idea. Be sure to synchronize your access to your Maps (which would be a bottleneck in a real application, but you can get away with it here, and in general in any application that isn't going to have a big load). But, using session might be easier, depends on how much work (and control) you want. For a shopping cart, the volume of data isn't going to be great, so no worries there. But, the golden rule with Struts to remember is no class-level variables! That's what all this is designed to avoid. -- Frank W. Zammetti Founder and Chief Software Architect
Re: Best practice question: Properties (ShoppingCart) per user
Hi, thanks for your offering of help. I know that my Struts knowlegde is not the best yet -- no question about it -- but I'm willing to change that, as far as my time allows it. Fortunately, my lecture is not of that importance you might have assumed, as I'm 'only' giving a presentation at school. However, I of course want to present something I know about, so this does not really change the situation. As I can see what you're telling me, I think my learning has been to practice-orientated and too less theory-orientated, which brings me into trouble when the requirements change. So far, I've only strictly written Spring apps with Struts MVC or without Struts MVC, but, in any case, with Hibernate as ORM-framework. Therefore, I really came into troubles when using Struts alone, and then even doing so without a database. Of course, I had thought about putting the stuff into the session, either, but putting complete client-state into the session seemed unclean to me, as I personally had prefered some kind of property-setting statically. Please let me know about further architectural mistakes I've done. I've started reading Wiley's 'Mastering Jakarta Struts', which unfortunately seems kind of outdated to me. Please let me know about better alternatives. Furthermore, I'd appreciate any kind of help. Kind regards and thanks in advance, Simon [EMAIL PROTECTED] wrote: PLEASE do not take offense from what I am about to say. I do not mean to be anything but constructive and helpful... I am concerned that you are trying to build an example for those you will be teaching because I can see some fundamental misunderstandings, or gaps in your knowledge. I'd be willing to guess that someone (your emoployer?) asked you to teach some people because you were the best of the group, and if that's the case I most certainly commend you for taking on the task. However, I think it would be in your best interest, and certainly in your students' best interest, to take some time and become more familiar with the topic(s) you are going to be teaching. I and others here could give you the answers, and we'd be happy to do so, but the questions you are asking are things that you really should be able to answer yourself in light of the fact that you are going to be teaching, and might face the same questions, or others that will test your fundamental knowledge. I'm not just talking about learning Struts here, which certainly is a big part of it. I sense you may have some learning to do with regard to web application development in general. Again, I hope I have not offended you, that is not my intent in any way. If your just learning this stuff yourself, that's great, we'll all help any way we can, but I would really try and hold off on the teaching for a while until you see these answers yourself. On Tue, December 7, 2004 1:59 pm, Bill Siggelkow said: Simon, Actions should not hold client-state; instead, you can create a ShoppingCart in the Action but then save it in the HttpSession. Without going into to many details, I suggest you take a look at the Struts MailReader example distributed with Struts. -Bill Siggelkow Simon MARTIN wrote: Hi, I'm currently writing a short Struts application without a database (I know that this would be better, but as it is only for teaching purposes, I don't want to 'overflow' my audience with databases and ORM). It should be a small online sales system -- I've got ShoppingItems, ShoppingUsers (which are not yet used) and ShoppingCarts. I want a ShoppingCart which stores the ordered items per user. I had something like this: public class ShoppingCartAction extends DispatchAction { private ShoppingCartManager shoppingCartManager; ... public ActionForward order(...) ... public ActionForward list(...) ... } and thought that it would work. Unfortunately, each user (which is not authenticated or anything yet) gets the same shopping cart when he calls shoppingCart.do?method=list. So I did some research and found this: http://www.jguru.com/faq/view.jsp?EID=1057609 So I created an ShoppingCartMapping class public class ShoppingCartMapping extends ActionMapping { private ShoppingCartManager shoppingCartManager; ... } and call it like this public ActionForward list(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { request.setAttribute(orderedItems, ((ShoppingCartMapping)mapping).getShoppingCartManager().getItems()); return mapping.findForward(list); } which gives me a ClassCastException. Do I have to set my custom ActionMapping in web.xml or struts-config.xml? Am I on the right way, or is there a better way of doing this? Thanks in advande and kind regards, Simon PS: Please cc me in your answers. - To unsubscribe, e-mail: [EMAIL
Re: Best practice question: Properties (ShoppingCart) per user
Sounds good! You'll find that the people here are extremely helpful, but they do tend to shy away from general questions (unless they are SO general as to be theoretical, in which case EVERYONE has an opinion :) ), so the more specific a question you can ask, the better. As for books to read, I would highly recommend grabbing the latest copy of Head First Servlets and JSP. It's not Struts-specific, but it is really necassery to know what is going on beneath Struts, and it is the best I've seen in this area (I'm currently using it as a study guide for the SCWCD exam, but it's good for anyone I'd say). Frankly, if you get everything in that book, most of Struts will make sense almost immediately. For Struts, O'Reilly has a pretty good book, the title of which elludes me at the moment, but I suspect it's the only one they sell. At times it goes into a little TOO much detail for people trying to get a start in Struts, but most of it is excellent. As for your specific architecture questions... Generally speaking, your concern about putting all application state for a user in session is valid. This isn't what session is for. It's really for fairly small BITS of state information, things that will in all likelihood change frequently, and certainly nothing that should persist beyond a session (hence the name!). But, in your case, since you are just using it as an example, I wouldn't worry about it, go ahead and use session, it'll be easier. In Struts, Actions can be shared across requests, which means that if you have class-level variables, they are, potentially, shared across multiple requests. Clearly this is a formula for disaster. There are two ways you could go here... one is to use session as I said, the other is to have a data storage class that just has some static members, most probably a Map of some sort. You can use this as something of a poor man's database. Think of a HashMap for instance as a database table. Now, assume each element you add to the HashMap is itself a HashMap. You can think of these inner HashMaps as rows in the table. Then, each element in that inner HashMap is a field in the table. Make sense? You can go so far as to create insertRow(), deleteRow() and updateRow() methods for your data storage class... These would do the functions their name implies on the HashMap, and you can in that way key your data, i.e., each row has an element recordKey let's say. So, your insert method can throw an exception if a record exists with that key already. In fact, you could go so far as to throw a SQLException, and in that way you could replace that data storage class with a class that talks to a database down the road, and nothing that uses that class should have to change. But that's getting ahead of ourselves a bit :) If you intend to build something that you know would usually use a database, then the second approach is a good idea. Be sure to synchronize your access to your Maps (which would be a bottleneck in a real application, but you can get away with it here, and in general in any application that isn't going to have a big load). But, using session might be easier, depends on how much work (and control) you want. For a shopping cart, the volume of data isn't going to be great, so no worries there. But, the golden rule with Struts to remember is no class-level variables! That's what all this is designed to avoid. -- Frank W. Zammetti Founder and Chief Software Architect Omnytex Technologies http://www.omnytex.com Simon MARTIN wrote: Hi, thanks for your offering of help. I know that my Struts knowlegde is not the best yet -- no question about it -- but I'm willing to change that, as far as my time allows it. Fortunately, my lecture is not of that importance you might have assumed, as I'm 'only' giving a presentation at school. However, I of course want to present something I know about, so this does not really change the situation. As I can see what you're telling me, I think my learning has been to practice-orientated and too less theory-orientated, which brings me into trouble when the requirements change. So far, I've only strictly written Spring apps with Struts MVC or without Struts MVC, but, in any case, with Hibernate as ORM-framework. Therefore, I really came into troubles when using Struts alone, and then even doing so without a database. Of course, I had thought about putting the stuff into the session, either, but putting complete client-state into the session seemed unclean to me, as I personally had prefered some kind of property-setting statically. Please let me know about further architectural mistakes I've done. I've started reading Wiley's 'Mastering Jakarta Struts', which unfortunately seems kind of outdated to me. Please let me know about better alternatives. Furthermore, I'd appreciate any kind of help. Kind regards and thanks in advance, Simon [EMAIL PROTECTED] wrote: PLEASE
Best practice question: Properties (ShoppingCart) per user
Hi, I'm currently writing a short Struts application without a database (I know that this would be better, but as it is only for teaching purposes, I don't want to 'overflow' my audience with databases and ORM). It should be a small online sales system -- I've got ShoppingItems, ShoppingUsers (which are not yet used) and ShoppingCarts. I want a ShoppingCart which stores the ordered items per user. I had something like this: public class ShoppingCartAction extends DispatchAction { private ShoppingCartManager shoppingCartManager; ... public ActionForward order(...) ... public ActionForward list(...) ... } and thought that it would work. Unfortunately, each user (which is not authenticated or anything yet) gets the same shopping cart when he calls shoppingCart.do?method=list. So I did some research and found this: http://www.jguru.com/faq/view.jsp?EID=1057609 So I created an ShoppingCartMapping class public class ShoppingCartMapping extends ActionMapping { private ShoppingCartManager shoppingCartManager; ... } and call it like this public ActionForward list(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { request.setAttribute(orderedItems, ((ShoppingCartMapping)mapping).getShoppingCartManager().getItems()); return mapping.findForward(list); } which gives me a ClassCastException. Do I have to set my custom ActionMapping in web.xml or struts-config.xml? Am I on the right way, or is there a better way of doing this? Thanks in advande and kind regards, Simon PS: Please cc me in your answers. - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: Best practice question: Properties (ShoppingCart) per user
-Original Message- From: Simon MARTIN [mailto:[EMAIL PROTECTED] Sent: Tuesday, December 07, 2004 11:47 AM To: [EMAIL PROTECTED] Subject: Best practice question: Properties (ShoppingCart) per user Hi, I'm currently writing a short Struts application without a database (I know that this would be better, but as it is only for teaching purposes, I don't want to 'overflow' my audience with databases and ORM). It should be a small online sales system -- I've got ShoppingItems, ShoppingUsers (which are not yet used) and ShoppingCarts. I want a ShoppingCart which stores the ordered items per user. I had something like this: public class ShoppingCartAction extends DispatchAction { private ShoppingCartManager shoppingCartManager; ... public ActionForward order(...) ... public ActionForward list(...) ... } and thought that it would work. Unfortunately, each user (which is not authenticated or anything yet) gets the same shopping cart when he calls shoppingCart.do?method=list. That's because you're violating one of the cardinal rules of servlet programming... your using a class property. Don't do that. So I did some research and found this: http://www.jguru.com/faq/view.jsp?EID=1057609 So I created an ShoppingCartMapping class public class ShoppingCartMapping extends ActionMapping { private ShoppingCartManager shoppingCartManager; ... } and call it like this public ActionForward list(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { request.setAttribute(orderedItems, ((ShoppingCartMapping)mapping).getShoppingCartManager().getItems()); return mapping.findForward(list); } which gives me a ClassCastException. Do I have to set my custom ActionMapping in web.xml or struts-config.xml? Am I on the right way, or is there a better way of doing this? Thanks in advande and kind regards, Simon PS: Please cc me in your answers. - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Best practice question: Properties (ShoppingCart) per user
Simon, Actions should not hold client-state; instead, you can create a ShoppingCart in the Action but then save it in the HttpSession. Without going into to many details, I suggest you take a look at the Struts MailReader example distributed with Struts. -Bill Siggelkow Simon MARTIN wrote: Hi, I'm currently writing a short Struts application without a database (I know that this would be better, but as it is only for teaching purposes, I don't want to 'overflow' my audience with databases and ORM). It should be a small online sales system -- I've got ShoppingItems, ShoppingUsers (which are not yet used) and ShoppingCarts. I want a ShoppingCart which stores the ordered items per user. I had something like this: public class ShoppingCartAction extends DispatchAction { private ShoppingCartManager shoppingCartManager; ... public ActionForward order(...) ... public ActionForward list(...) ... } and thought that it would work. Unfortunately, each user (which is not authenticated or anything yet) gets the same shopping cart when he calls shoppingCart.do?method=list. So I did some research and found this: http://www.jguru.com/faq/view.jsp?EID=1057609 So I created an ShoppingCartMapping class public class ShoppingCartMapping extends ActionMapping { private ShoppingCartManager shoppingCartManager; ... } and call it like this public ActionForward list(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { request.setAttribute(orderedItems, ((ShoppingCartMapping)mapping).getShoppingCartManager().getItems()); return mapping.findForward(list); } which gives me a ClassCastException. Do I have to set my custom ActionMapping in web.xml or struts-config.xml? Am I on the right way, or is there a better way of doing this? Thanks in advande and kind regards, Simon PS: Please cc me in your answers. - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Best practice question: Properties (ShoppingCart) per user
PLEASE do not take offense from what I am about to say. I do not mean to be anything but constructive and helpful... I am concerned that you are trying to build an example for those you will be teaching because I can see some fundamental misunderstandings, or gaps in your knowledge. I'd be willing to guess that someone (your emoployer?) asked you to teach some people because you were the best of the group, and if that's the case I most certainly commend you for taking on the task. However, I think it would be in your best interest, and certainly in your students' best interest, to take some time and become more familiar with the topic(s) you are going to be teaching. I and others here could give you the answers, and we'd be happy to do so, but the questions you are asking are things that you really should be able to answer yourself in light of the fact that you are going to be teaching, and might face the same questions, or others that will test your fundamental knowledge. I'm not just talking about learning Struts here, which certainly is a big part of it. I sense you may have some learning to do with regard to web application development in general. Again, I hope I have not offended you, that is not my intent in any way. If your just learning this stuff yourself, that's great, we'll all help any way we can, but I would really try and hold off on the teaching for a while until you see these answers yourself. -- Frank W. Zammetti Founder and Chief Software Architect Omnytex Technologies http://www.omnytex.com On Tue, December 7, 2004 1:59 pm, Bill Siggelkow said: Simon, Actions should not hold client-state; instead, you can create a ShoppingCart in the Action but then save it in the HttpSession. Without going into to many details, I suggest you take a look at the Struts MailReader example distributed with Struts. -Bill Siggelkow Simon MARTIN wrote: Hi, I'm currently writing a short Struts application without a database (I know that this would be better, but as it is only for teaching purposes, I don't want to 'overflow' my audience with databases and ORM). It should be a small online sales system -- I've got ShoppingItems, ShoppingUsers (which are not yet used) and ShoppingCarts. I want a ShoppingCart which stores the ordered items per user. I had something like this: public class ShoppingCartAction extends DispatchAction { private ShoppingCartManager shoppingCartManager; ... public ActionForward order(...) ... public ActionForward list(...) ... } and thought that it would work. Unfortunately, each user (which is not authenticated or anything yet) gets the same shopping cart when he calls shoppingCart.do?method=list. So I did some research and found this: http://www.jguru.com/faq/view.jsp?EID=1057609 So I created an ShoppingCartMapping class public class ShoppingCartMapping extends ActionMapping { private ShoppingCartManager shoppingCartManager; ... } and call it like this public ActionForward list(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { request.setAttribute(orderedItems, ((ShoppingCartMapping)mapping).getShoppingCartManager().getItems()); return mapping.findForward(list); } which gives me a ClassCastException. Do I have to set my custom ActionMapping in web.xml or struts-config.xml? Am I on the right way, or is there a better way of doing this? Thanks in advande and kind regards, Simon PS: Please cc me in your answers. - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]