• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Struts 2 forms with different actions

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm developing an e-commerce project, and I have some doubts about how to organize input fields and related errors.
In this particular case (but for me it's a general, unanswered question) I wrote two actions, one for a detailed view of a product (viewProduct) and another one for adding it to cart (addToCart).

viewProduct (associated with it's own JSP page) comes with a form like this:


This is good if the form contains no errors, but if not, I cannot display them because I've already moved from viewProduct, losing its state.

I read that usually Struts 2 programmers use the same action for both showing the "first-request" page (the one I print now with viewProduct) and then working with inputs, but for both code cleanness and logic subdivision I prefer to keep two separated actions (merging them would result in a little chaos).
Plus, using the same action does not allow the framework (or I don't know how to make it) to distinguish between a first request, where input fields are obviously empty, and subsequent requests, where empty fields mean errors. This is not good, of course, because in this way a customer gets an error by simply viewing a product.

I hope I've been clear, english's not my language... someone can suggest me a solution?

 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jack Dragano wrote:This is good if the form contains no errors, but if not, I cannot display them because I've already moved from viewProduct, losing its state.



What does your action configuration look like? One usually specifies an "input" result for the validator to return to.

Jack Dragano wrote:I read that usually Struts 2 programmers use the same action for both showing the "first-request" page (the one I print now with viewProduct) and then working with inputs, but for both code cleanness and logic subdivision I prefer to keep two separated actions (merging them would result in a little chaos).



This sounds like your are putting all your logic in the action class. Bad Idea:

It is wise to avoid creating lengthy and complex Action classes. If you start to embed too much logic in the Action class itself, you will begin to find the Action class hard to understand, maintain, and impossible to reuse. Rather than creating overly complex Action classes, it is generally a good practice to move most of the persistence, and "business logic" to a separate application layer. When an Action class becomes lengthy and procedural, it may be a good time to refactor your application architecture and move some of this logic to another conceptual layer; otherwise, you may be left with an inflexible application which can only be accessed in a web-application environment. The framework should be viewed as simply the foundation for implementing MVC in your applications. Struts provides a useful control layer, but it is not a fully featured platform for building MVC applications, soup to nuts.


Struts 1.3 User Guide: Action Class Design Guidelines

Jack Dragano wrote:
Plus, using the same action does not allow the framework (or I don't know how to make it) to distinguish between a first request, where input fields are obviously empty, and subsequent requests, where empty fields mean errors. This is not good, of course, because in this way a customer gets an error by simply viewing a product.



One can map any number of action names to an action class, and those action names can invoke different methods with different behavior.
 
Jack Dragano
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Joe Ess wrote:What does your action configuration look like? One usually specifies an "input" result for the validator to return to.




In a few words, when I click on a product from the catalog I obtain viewProduct.jsp from ViewProductAction, which contains also the form for adding the product to cart.
Then AddToCartAction checks (using built-in XML validators) that all parameters are right, then it puts the product in the cart and simply redirects the user to the previous page.

But in the case that parameters are wrong, I don't know how I can go back to viewProduct with making Struts informing the user about which fields are wrong.
AddToCartAction does not provide needed information to fill viewProduct.jsp (it's a minimal action), and I've no idea about how redirecting errors too (notfound.jsp is currently an empty page).

Joe Ess wrote:This sounds like your are putting all your logic in the action class. Bad Idea:


They are short, but I don't want to merge them because in my mind they are two different operations.

Joe Ess wrote:One can map any number of action names to an action class, and those action names can invoke different methods with different behavior.



Did you mean something like this?

But in this way the framework calls the same execute() method and validates the fields using the same XML file... or not?
 
Joe Ess
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

in the case that parameters are wrong, I don't know how I can go back to viewProduct with making Struts informing the user about which fields are wrong.



What happens if you make viewProduct.jsp the input result for the addToCart action?

They are short, but I don't want to merge them because in my mind they are two different operations.



If you don't want to merge them, then don't. I prefer to keep all my CRUD methods together.

There's a "method" attribute that changes the method invoked in your action from execute() to whatever you want:


The Validation Guide tells you how to associate a validator file with an action name so each action is validated separately (or not at all).
 
Jack Dragano
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Joe Ess wrote:What happens if you make viewProduct.jsp the input result for the addToCart action?


viewProduct.jsp misses all dynamic content, because AddToCartAction does not provide needed getters.

Joe Ess wrote:There's a "method" attribute that changes the method invoked in your action from execute() to whatever you want:



This sounds really, really interesting.
I think I will move cart's logic directly into Cart class and use a dedicated method through class ViewProductAction, in order to perform addToCart. Thank you for your help, you solved my problem!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic