Calling a different method instead in 'execute' in Struts 1.2 - java

In struts 1.2, when an URL in the form http://foo.com/barAction.do is invoked, this will call the execute method (or process) in the action class that was mapped to barAction.
Is it possible to call a different method other than the execute (or process) in the same action class when the above URL is invoked?

You can use a DispatchAction. Basically they allow you to choose which method will be invoked based on a request parameter or action mapping configuration
There are at least 3 "flavours" of DispatchAction: EventDispatchAction, LookupDispatchAction, and MappingDispatchAction.
I'd recommend you to take a lookt at EventDispatchAction that was introduced in Struts 1.2.9.

Related

Access url parameters in Action classes Struts 2 in AJAX call?

I know that S2 provides a clean way to fetch the request parameters in you action class all you need to follow these simple rules.
Create a property with same name as request parameter name.
Create getter and setters for this property or make property public (for S2.1+)
However, when I do this in an AJAX call like this:
$.ajax({
url: '/gma/getJSONData.action?tspName='+tspName+'&thresholdType='+thresholdType,
I don't get the tspName parameter inside action class. I created the getter/setter for it. It's displaying null value.
Am I wrong somewhere?
EDIT:
I was checking the value of tspName in my Action class constructor, so was printing null. However, in my execute method it displays the value correctly. Why is it so? It means before constructor call it does not initialize values?
I was checking the value of tspName in my Action class constructor, so
was printing null. However, in my execute method it displays the value
correctly. Why is it so?? It means before constructor call it does not
initialize values?
Probably you should learn the basics how Struts2 works. When you make a request a filter is invoked and the dispatcher is handling the request via creating the action context and building action instance.
Then interceptors are invoked on this action. One of the interceptors of the defaultStack is params interceptor. It's responsible to populate your action with request parameters, to be more Struts2 action context parameters.
It means you can always get parameters from the action context. See How can we access request parameters passed into an Action.
The constructor of the action is called before any interceptor is invoked, so the action is not populated yet and properties aren't initialized. On the other hand when the action is executed all interceptors are already invoked, so the action is populated. Before constructor or after constructor it doesn't matter. What is matter is params interceptor in the action configuration.
You can always get parameters like described in the link above, or directly from the servlet request like in this answer. All features of Struts2 framework is available to you.

struts 2 action with no setter getter

I would like to create an action class with no setter and getter on properties for the data coming from the user interface. Instead, I would like to use ServletActionContext.getRequest().getParameterMap() in my own builder class to construct the object.
I had created my Action class with no properties. When I am submitting my form I am running into ognl.OgnlException: target is null for setProperty(null, "field-name", [Ljava.lang.String;#5513fab7)
Is there any additional conventions or configurations required to convey Struts2 framework to not set properties and stop avoid the exception I am receiving above?
You can exclude some properties from the accepted parameters for params interceptor by setting parameter excludeParams to the interceptor. By default this parameter is initialized with
<interceptor-ref name="params">
<param name="excludeParams">dojo\..*,^struts\..*,^session\..*,^request\..*,^application\..*,^servlet(Request|Response)\..*,parameters\...*</param>
</interceptor-ref>
You should add you properties here, it accepts the regex pattern to match the property names. The strategy applied with accepted parameter names could be compromised via the ParameterNameAware implemented actions where you could remove the restriction given above.
To be more specific about "data coming from the user interface" I'd adhere that parameters to the interceptor-ref element is applied to the interceptor on start up and is not stored elsewhere in the configuration manager. This means you can't get this parameters at runtime and only could change via updating and reloading the configuration file struts.xml. If you keep your configuration in the safe place and it's protected from modification then you could make more claims toward your running application safety.
Yes, you need to remove the Struts params interceptor mapping for this specific action. I believe you can take an approach similar to the one in this related question. Otherwise you will have to create another interceptor stack with the interceptors you desire, minus the parameter interceptor and map the action to that stack in struts.xml or use the #InterceptorRef annotation on your action class, assuming you're using the convention plugin.

Instance of resource in Java rest api

I have a java rest api. What I want to know is this:
Say one specific client (e.g. person named X is using computer Y) makes three calls to the same uri. (e.g. https://stackoverflow.com/firstname/kasav/lastname/bere). Further, say the resource has a non-static counter.
class ResourceA{
int count = 0;
#Get
public Response service(){
count++;
//return count below
}
}
Now does the caller get the same response for all three calls:
1
Or does the caller get 1 for the first call, then 2 for the second call, then 3 for the third call.
For some of you this may be obvious, but not to me. Thanks for helping.
Assuming you are using something that follows JAX-RS spec correctly.
3.1.1 Lifecycle and Environment
By default a new resource class instance is created for each request to that resource. First the
constructor (see section 3.1.2) is called, then any requested
dependencies are injected (see section 3.2), then the appropriate
method (see section 3.3) is invoked and finally the object is made
available for garbage collection. An implementation MAY offer other
resource class lifecycles, mechanisms for specifying these are outside
the scope of this specification. E.g. an implementation based on an
inversion-of-control framework may support all of the lifecycle
options provided by that framework.
As such the answer to your question is all callers will receive 1 as the class is initiated for each request.
See JSR-311 for the JAX-RS spec.
By default JAX RS resources are request scoped, means it create new instances for each request.
To process the multiple requests with the same instance of the resource you can mark the resource (class ResourceA in this case) with the annotation #Singleton.
Marking as Singleton will make the output of the ResourceA class as 1 for first request, 2 for second request and so on.

Can i call an controller action with POST-params from another controller as method?

I've a controller and some actions within. Am i able to call these actions as methods from another controller as if i would call this action with post params?
I'm using the grails paypal plugin and it has an action called "buy". I don't want the user to POST the product-data and information to the buy-action. I already know the things the user wants to buy and i want to call the buy-action within my Controller as i would call a normal method and then i want to redirect the user to paypal (to the url, the buy action returns).
I'm using grails 2.0 and newest paypal plugin.
No you cannot. You can tell from action A to chain to action B.
But this is not calling it as a method, this will redirect the call to action B, you will not be anymore in action A's scope.
Maybe you can implement your action in a service. Then you can call it as you like from your controller's action
As per my knowledge, You can redirect your action to another action which belongs to other controller.
you can use redirect method for that.
"redirect(controller: 'abc', action: 'actionName', params: 'params')"
You may want to use chain(...).
Chain:
Uses flash storage to implicitly retain the model following an HTTP
redirect from one action to another.

A servlet or filter that dynamically maps /xxx/yyy/zzz to class XxxYyyZzz.java

I want to write a servlet or filter that automatically maps the url /xxx/yyy/zzz to class XxxYyyZzz.java.
For example the following URLs will map to the following java classes:
/comment/add --> CommentAdd.java
/comment/delete --> CommentDelete.java
/comment/view --> CommentView.java
/search --> Search.java
/viewposts --> Viewposts.java
In addition the servlet or filter must comply with two extra requirements:
The servlet or filter should have a servlet mapping of "/*", I dont want a prefix with several servlets "/comment/*", "/search", etc.
Maybe difficult, but having a servlet mapping of /* should not allow it to override the JSP processing. Meaning, if a class is not found, it should check if a jsp page exists and run it.
I want to know how can this be done using the Servlet API. Please don't refer me to any framework that does the job. Just show me the code.
The classes that are mapped to will follow the command pattern or could be a subclass of the HttpServlet. In both cases, a method should exist like "execute(HttpServletRequest request, and HttpServletResponse response)". This method will be automatically executed once the URL is accessed and the java class is figured out possibly using a single servlet or filter.
I'm not sure, if I got what you mean. In case I did:
You need nothing special, write a single Servlet mapped to "/", so it gets everything. Parse the PATH_INFO (don't know now how it gets called in Java), use Class.forName (or use a pre-filled Map), and call its method execute.
Here is a http://www.tuckey.org/urlrewrite/ filter implementation that might help you. Check it out. I have not used it myself though.
You can use Stripes framework with its default NameBasedActionResolver config.

Categories

Resources