Struts2- extending action classes and thread safety - struts2

I understand the instance variables on an action class are thread-safe since action classes are instantiated per request. But I have this need for extending action classes and I'm concerned about the thread-safety.
Say, for example I have some common attributes and a few methods handling those attributes among several action classes. I prefer to put them in a single action class and make that extend the ActionSupport. And all action classes will then extend the base action class that I just created. My question is, are the instance variables on the base action class thread-safe? Does S2 manage the base action class at all?
Also what makes an action class an action class to be managed by the S2 and instantiated per request? Getting declared in the struts.xml? Extending ActionSupport class?

I think you're a bit confused about how Java works.
If you have a class A and a class B extends A, when you instantiate B there is a B. It's not like there's a single instance of A backing all instances of B. There's no "management of base classes".
Classes declared as actions either via XML, annotations, or convention are instantiated by the Struts action instantiation mechanism. Extending ActionSupport has (almost) nothing to do with it, the only time it might have something to do with it is because ActionSupport implements the Action interface.

Also what makes an action class an action class to be managed by the S2 and instantiated per request? Getting declared in the struts.xml? Extending ActionSupport class?
Getting declared in the struts.xml: yes, is that that turns a Java class into an Action.
And every Action class is thread-safe because it's ThreadLocal, no matter what it extends nor implements. Every request of every user will have its own copy of each Action class.
P.S: The other classes (not declared in struts.xml) are not "(action classes) not managed by S2 and instantiated per request", they're simply NOT Actions.

Related

What is the difference between 'implements' keyword and 'extends' keyword in dart?

I couldn't understand the difference between these two keywords.
By using extends we can get features from parent class. I think implements does that too.
First I thought the difference is overriding methods but with extends I can do that.(I might be wrong)
Is the difference of these two keywords about overriding methods or what? Thank you
extends means we get the implementation of a given class and we can then override members if we want our own implementation for certain variables or methods. You can also add new variables and methods.
implements means you get nothing from the class you implement from. But you promise that your class will be compatible with the interface of the class you are implementing. So no, you are not getting any implementation from the super class and you need to implement everything or declare your class abstract.

How the object of controller classes is created in Rails?

When we navigate through pages in a rails app, inturn we call one of the functions defined in the controller class. Lets say we access localhost:3000/articles/new then new action (method) of the ArticlesController class is called/invoked.It's simple.
But what i can't figure out is that since ArticlesController class is a pure Ruby class with some methods and we need an instance of this class to call one of it's methods. But we never explicitly do that.
Then how the function call of any controllerclass is made possible ?
The controller is initialized automatically by rails. Specifically, this calls the action method on the controller class, which does the actual initialization.
The RouteSet generates instances of any controller on demand based on the needs of the ActionDispatch routing system. See here for how this is done.
So unless you are testing a controller directly, you can rely on the router to supply you with a controller instance. And if you are testing one directly, you should be using an ActiveController::TestCase to do this work for you.

Grails: #BindUsing on a class being ignored and #BindUsing vs ValueConverter

I need to do some custom data binding and I tried to use the #BindUsing annotation on a class (http://grails.org/doc/latest/api/org/grails/databinding/BindUsing.html), however, it's being ignored. I am under the assumption that since the annotation is used on the class that would mean that every time a data binding happens and that class is involved, the BindingHelper class would be used, but it's never actually called. Is there something that I'm missing or doing wrong?
Here's the class definition where UserBinding is a class that implements the BindingHelper interface:
#BindUsing(UserBinding)
class User extends SomeOtherClass
{
...
Also am I correct in understanding that basically creating a ValueConverter and using #BindUsing on a class accomplish the same thing?
BindUsing on a class is not used often and there seems to be a bug reported around that already. [From the link] The problem could be that there are multiple request parameters with the same name it might be using the helper only for the first one.
Using a property level #BindUsing annotation should be simpler to implement and is less likely to fail (even when there are multiple entries in the params map with the same name).

Class instantiation in Dart and sharing code between Class objects

When a class that contains code is instantiated, is the code is the class shared automatically by other instantiations of that same class? Eg. The data in the class that is instantiated may be minimal; however the code could be very significant. If the code is not "automatically" shared, is there a way to achieve that other than separating the code from the class data?
Sure.
Classes have the state and the behavior.
The state is encoded in member variables of the class. Each instance has its own copy of variables, thus its own state.
The behavior is specified by the methods implemented in the class ('methods' here stand for all static, non-static methods, setters and getters). Implementation is shared by all instances of the class, so all instances behave the same, but actual results and side-effects depend on instance state, obviously.

Access Properties from utility classes used by Action

I just want to know if there's a way to access the properties from a utility class used by an Action class. To access the properties from an Action class we extend the ActionSupport and use the getText("property.key.name") method.
So, my question is -should every other class extend the ActionSupport to access properties, even though its not an Action class? or is there any other way?
Thanks
I wouldn't extend ActionSupport unless you're actually defining an action.
The S2/XW2 ActionSupport class uses com.opensymphony.xwork2.DefaultTextProvider; you might be able to use it in your own classes. I'm a little wary of this since I'm not convinced non-action classes should be accessing the web-app's resources, but I haven't given it much thought, so it could be valid. I also haven't tried to do it.
ActionSuport is kind of helper class being developed by S2 developers to supplement the Development as it provides many features OOTB.
getText() is one of the use-case where S2 provides a way to read the property files.This method is specific to S2 as it know how to transverse the hierarchy to read the property files and in what order.
There are many ways to read the property files in a application and few of them are
ResourceBundle
if you are using Spring, it has a very handy mechanism to read property files
- how-to-read-properties-file-in-spring
Apache Common also provides a way to read the file
Apache-Common
In short to read properties file there are many ways, S2 getText() is a way developed by the S2 to read the property file with respect to your actions.
//I wanna make you understand how struts doing it.
public class ActionSupport implements Action, Validateable, ValidationAware, TextProvider, LocaleProvider, Serializable {
//Action support implementation.
//Here TextProvider takes care about resource bundle thing.
}

Resources