Is it a good practice to create different action for page loading and form submission? let’s say I want to view to register to a website. Upon clicking the link for registration it will just go to a simple action that just typically redirects the page and on the registration form, and upon click the submit button, and the same action will also process the data to send it to the database?
or is it better to have a different action for form submissions?
There’s no real “better or worse” for this scenario. It’s similar complexity, but different locality of functionality. I tend towards many small classes in general, but there’s another option.
I tend towards Single Action, Multiple Methods, along with an interceptor that differentiates between GET and POST requests. The interceptor returns a known result for GET requests (e.g.,
"input") and allows normal processing for POST results (i.e., normal action invocation).This normalizes form handling and eliminates manual configuration of multiple methods for simple form handling scenarios across the entire application.
Using the Convention plugin makes this less interesting, although doing it automatically is kind of pleasant: once you know the app’s GET/POST “secret” you never have to think about it again.
To address your comment:
The different functionality has to live somewhere.
In S2 you have two ways of doing this: an action with multiple methods, or multiple actions. This is what I thought your question was addressing–which is “better”. (Answer: It depends.)
The below uses XML config; annotations would be essentially the same.
Using multiple actions can be trivially easy; one could even be the default action:
If there’s no form preparation, you’re done. Then you configure an action that does the login:
Using multiple methods is almost identical: