Action Class in Struts 1.x and Struts 2.x - java

I have a query that in Struts 1.x , in Action class we have Action,Dispatch Action,Lookup Dispatch Action. In the same way in struts 2.x what all action's we have?

In Struts 1 DispatchAction helps us in grouping a set of related functions into a single action. In Struts 2 all the Actions by default provide this functionality. To use this functionality we need to create different methods with the similar signature of the execute() method, only the name of the method changes. It’s a useful mechanism to avoid create separate action classe for each function.
for more info you can visit the following url: url1 and url2 . Hope that helps

Related

How to remove actions from the ValueStack?

I'm trying to make my action classes singletons. The point is to make action classes real controllers in MVC pattern. Especially when implementing REST controllers the scope of the controller could be extended to the life of the application. Like in Spring framework the controller is put to the default scope by default, the default scope in Spring is singleton.
Struts 2 has also a default scope, and it's also singleton. I want to put my action classes to this scope and remove them from the value stack.
How could I modify Struts 2 framework to make my actions like a controller in Spring? I know that I could just delegate management of action classes to Spring, but I can't use a default scope, and Struts 2 container is still keep running. I can't remove it from the framework, because it's not pluggable.
So, walking around Spring and Struts2 container I can't make my action classes singletons because Struts 2 instantiate and put them to the ValueStack.
This is my question:
If I choose the container between Struts 2 and Spring to put my action classes to default scope, how could I tell Struts 2 framework not to put them to the ValueStack?
Assuming you're using XWork's DefaultActionInvocation implementation, it's done there, by the init method. Pulling that out is a bit of a pain because it's layered underneath action proxies and action proxy factories in both XWork and S2.
That said, I'd be very hesitant at making a change like this; it has system-wide implications and is counter to essentially everything about XW/WW/S2.
(Unrelated, but singleton nature isn't what defines a controller, it's the responsibilities that define what a component is.)

struts 1 singleton classes

1) I would like to know which main classes in Struts 1 are are Singleton classes. Main classes like ActionServlet, RequestProcessor, Action, ActionForm etc.
2) Also, I heard from somebody that if we have multiple struts confix xml file in our struts application then for each module a new RequestProcessor will be instanstiated. Is this true ?
Thanks.
1) there is Actionclass which will be singleton like as we are not going to generate object explicitly.
2) and whatever number of struts-config file is there for that only requestProcessor instantiated
To understand classes that are used by the Struts framework better look the the source code. If you did it you'll see that none of the classes you've mentioned implement a Singlton pattern. That means that nothing prevent them to make as many instances as you needed. But it depends on how these instances are managed.
Not exactly, the request processor is created for each module in the case if there's not one is already created for a concrete module. See ActionServlet.getRequestProcessor

Struts and variable post parameters

I'm trying to integrate paypal IPN with my Java Struts web application. According to paypal, there are only 2 variables that are posted with IPN across all transaction types, but there are a bunch of other variables that could possibly be posted.
If a post variable is sent that's not in my action form, then struts returns an error about the form not having the property/setter.
IPN has like 300 variables and I really don't want to have a field for all possible ones. Is it possible in struts to accept post variables that my actionForm may not have accounted for?
If you want to use Post Variables which will not be in Struts Form, you should bypass struts form and use simple html form with struts action classes. Some post variable without getter setter in struts form will throw the exception as you specified. So it is better to use HTML form to gain more flexibility. Off course we should write some functionalities for validation and others again for this type of form.
All we want to say that, you can use simple html forms with struts action classes without any issues. We have already done these type of works in our production applications.
Thanks

Can anyone explain the concept of Action and ActionForm?

I am new to Struts2.On doing sample programs.I came to know that we are creating a class and extending Action and another one class extending Actionform.I dont know why we are doing that ?And I can understand that struts contains set of custom tag libs which can be used .We are defining the Action tag and the appropriate form-bean.I could not find the concept and advantage behind this framework. Could anyone provide the links or books where i can find the struts concept.
thanks in advance
Struts is an implementation of MVC. The Action class, in combination with the configuration (struts-config.xml normally) is the controller, deciding based on what the user submits what parts of the model (your business logic, should cotain nothing web-specific).
The ActionForm as mentioned about represents the form submitted by the user.
I personally read Struts in Action many years ago, but that only covers Struts 1. The Struts website itself should be helpful http://struts.apache.org/
Struts is far in the past for me, but let's see if I can dredge up a memory to explain.
Struts associates an Action class with a request URL. When that request comes in, Struts looks up the Action using the request URL and executes it.
The ActionForm associates an HTML form with an Action so Struts can marshall HTTP request parameter name/value pairs and bind them to Java objects. These Java objects are passed to the Action so it can do its execution work.
Struts is rather old now; it's been around since 2000. The Action and ActionForm concepts have been part of it from release 1.0. There are lots of books on Amazon and articles on the web. If you can't find any, or have to ask here to get some, I'd say you're being far too passive and lazy about it. Fire up Google and get to work.
Don't mix up Struts 1 and Struts 2 both are having different architectures
Struts 2 = Struts + WebWork.
Struts2 architecture http://struts.apache.org/2.3.1/docs/home.html
Struts2 guides http://struts.apache.org/2.3.1/docs/guides.html

In Struts 1.3, what's the best way for the Controller to fill the View with variables?

I've just inherited some old Struts code.
If Struts (1.3) follows the MVC pattern, how do the Action classes fill the View with variables to render in HTML ?
So far, I've seen the Action classes push variables in (1) the HTTP request with
request.setAttribute("name", user.getName())
(2) in ActionForm classes, using methods specific to the application:
UserForm form = (UserForm) actionForm;
form.setUserName(user.getName());
and (3) a requestScope variable, that I see in the JSP layer (the view uses JSP), but I can't see in the Action classes.
<p style='color: red'><c:out value='${requestScope.userName}' /></p>
So, which of these is considered old-school, and what's the recommended way of pushing variables in the View in Struts ?
My Struts days are long over, but as far as I remember we used to place one view-specific bean (which would work as a holder for fine-graner beans or collections of beans) into the request scope within our Action.perform() implementation. This view-specific bean would then be rendered by the view.
As Struts 1.3 is considered old-school, I'd recommend to go with the flow and use the style that already is used throughout the application you inherited.
If all different styles are already used, pick the most used one. After that, pick your personal favourite. Mine would be 1 or 3 - the form (2) is usually best suited for data that will eventually be rendered inside some form controls. If this is the case - use the form, otherwise - don't.

Categories