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.
Related
in my application I send all forms as POST request :
<s:form method="POST">
however I noticed when I prepare GET call like this:
www.domain.com?method:Save¶m1=aa
proper action is invoked.
My question: is it possible to prevent GET calls for particular actions ?
So if I run www.domain.com?method:Save¶m1=aa , action "Save" would not be invoked?
You can add interceptor to intercept the action and if the method is not POST then return error result. If you want to learn how to configure the result you can read this answer.
In the interceptor implementation you can use ServletActionContext.getRequest() similar to this answer.
Then use request.getMethod() to get the method of HTTP request.
In my web application I am sending two parameters: action and productCode from JSP to Servlet. Based on that action some processing will happen.
Now after action is performed I am forwarding control to a JSP. The problem is that when the new JSP is opened the URL still contains the name of Servlet and the Parameters. So in case if someone refreshes the page, the same action will be performed again and again.
If somehow I am able to remove the parameters from URL then I handled a no parameter situation in servlet.
Can anyone please tell me how can I remove the parameter from request object?
You can't remove a parameter from a HttpServletRequest. The very definition of a parameter is that it came from the client (browser).
Perhaps you mean a request attribute ?
For that you can use:
request.getAttribute(String name)
request.setAttribute(String name, Object o)
request.removeAttribute(String name)
A forward operation is transparent to the client and forwards the request on to another handler for processing. Perhaps a forward is not exactly what you want to do.
For the record (if someone does not find the other questions/answers):
The good practice is to wrap the request object in another object
using a servlet filter. (...)
Subclass HttpServletRequestWrapper and override the getParameter
methods. (...)
You can't remove a parameter from a HttpServletRequest - but you can change its value by passing new values for this parameter.
For example, after login to hide a password value you can forward to next servlet/page this way:
"*/PAM_show_orders?orderDate=2020-04-16&**password=+++***".
What you can do is set that parameter to null and check before performing any operation if that attribute is set to null. This way you still will be able to use request forwarding.
For example:
request.setAttribute("Your_attribute",null);
checking for not null while performing action can be done using
String para=request.getAttribute("Your_attribute");
if(para.equals(null)){
//do this
}
else{
//do something else
}
When your request handler is over you can just use:
response.setParameter("action") = "";
response.setParameter("productCode") = "";
Hope this helps.
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.
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.
I am using Stripes but i'm not sure if this problem is because of that. I have an actionBean with a setter method setSearchView. In this setter, I set a cookie. The problem I'm seeing is that if i call that method from my jsp, the cookie does not get set (i have debugged the code and it does go through the code). If i call the same setSearchView from an action handler, the cookie is set.
Is there something I'm missing? Is this a Stripes thing or a jsp/javabean thing?
I think you are misunderstanding the programming model, I'm guessing you are coming from a CGI/Php background.
Setters/getters on Stripes action beans are used to allow the ActionBean to receive the request parameters (URL parameters in the case of GET requests, form parameters in the case of POST requests) from the browser.
You wouldn't set them manually from JSPs because you wouldn't be putting controller logic in the JSPs but in the ActionBean.
The JSP will only be used to display ('View') any data provided by the controller from the model/view-model and to display input elements to allow the user to provide input. (See MVC on Wikipedia)