Identifying Redirect/Forward Action in Struts2 Incerceptor - redirect

I was looking for a way to identify Struts 2 actions which are of type 'Redirect/Forward' in Interceptor, so that I can add some common code for that particular type of Action.
Is there any way in Struts2 to find what type of Action it is?
Thanks in advance.

There is nothing called as RedirectAction or ForwardAction, what you need it Redirect Result Type.
In your interceptor you have an instance of ActionInvocation passes to your intercept method, you can get the result from ActionInvocation object and then check as per your use case. Different Results are listed here
public String intercept(ActionInvocation actionInvocation) {
//After invoking the action you can get the result of from ActionInvocation.
Result result = actionInvocation.getResult();
//As per your use case you can check against different types.
}

Related

Struts 2 post back default

In the Struts documentation, it says:
Another common workflow stategy is to first render a page using an alternate method, like input and then have it submit back to the default execute method.
https://struts.apache.org/core-developers/action-configuration.html#post-back-default
How to do it using annotation only? It seems that only the execute() method is called.
In the documentation it's said to render a page can be used an alternate method like input. This means that when you submit a form on the page it can return back with the input result. Usually it happens automatically during validation process if the validation fails or it hasErrors. Then you can submit the form back to the default action's execute method. You don't need to specify a method in the action configuration. Also if you didn't specify the action attribute in the form tag then the same action will execute which was used to render a page.
Configuring actions you can use the same page for success result when rendering a page using GET method and input when POST method is requested.
To use annotations to configure actions mapping you can use a Convention Plugin.
Also note, to map a class method to the action you should put #Action annotation directly on this method rather than on the class.
More detailed explanation and documentation you can find here.
#Namespace("/")
public class ProductAction extends ActionSupport {
public String execute() {
return SUCCESS;
}
#Action(value="product",
results=#Result(location="/product-list.jsp")
)
public String search() {
return SUCCESS;
}
}
Notice, that the method execute is not mapped, so it will not execute. If you need that method execute you should create mapping to it. For this purpose you could place annotation on class or on method execute.

play framework - redirecting with querystring

I am Using the play Framework 2.7.x
I have a Formular on my controller.list() with a view, let's call it "index". After you click "send" it open's controller.add() where it dos some stuff and then redirects back to controller.list(). If there was an error in the formular (a requiered field was empty) I need the queryString, which was send to controller.add() also redirected to controller.list()
The problem ist that if I do stuff like just passing the request, i get an error that it's not possible to add arguments.
public Result list(Http.Request request)
{
// .... stuff with foo, while foo is an Form<foo> Object
// ... foo.bindFromRequest(request)
ok(views.html.index.render(foo))
}
public Result add(Http.Request request)
{
// not allowed to add request as an argument. only empty is allowed.
return Results.redirect(controllers.routes.Controller.list(request));
}
I would like to just redirect the Form object, so I can handle the error in the controller.list() and not have to generate an extra view for the controller.add(). If I do everything inside controller.list() there is no problem with this code, but I like to use the controller.add() method instead.
Is the an option? except passing every querystring key and value by hand.
After I searched yesterday the half the day, I found something interessting today.
you are not allowed to use a default parameter with =. You have to use an optional default parameter with ?= inside the routes!!!!!
you can implement QueryStringBindable so it's a bit easier to bind the query String. But you still have to bind them "by hand".

Fetching the Odata query parameter in code

I have a rest Get method Ex:-
public IQueryable<Test> GetTest() {
return _service.GetAll();
}
I want to fetch the query parameter in code which was the part of the request url, I cant give OdataQueryOption parameter in the method like GetTest(OdataQueryOption
op).
Please suggest a solution
Assuming you're implementing an ODataController you should be able to access this.Request.RequestUri.Query. If you want the name/value pairs you should use System.Web.HttpUtility.ParseQueryString to get a NameValueCollection.

Portlets - Differences between setAttribute and setRenderParameter

I'm a beginner with portlets, and I don't understand the difference between request.setAttribute and response.setRenderParameter (for an action). Both save an attribute into the request and allow to access to it after. I think specially about transmission between a processAction and the render which is just after the action method.
I know that with setRenderParameter we cannot "stock" a complex object, but if I just want to transfer a String which one should I use?
In which case should we use the setRenderParameter method or the setAttribute method ?
Well, one sets an attribute on a request. The other sets a parameter on the response. They are different objects, obviously.
response.setRenderParameter is of use if you wish to call different render methods based on your action. For example, imagine your action method sends an email, and you want to show the user a different view on success and failure. In this case, you would do something like this in your ActionMapping
if(sentOK){
response.setRenderParameter("result", "success");
}else{
response.setRenderParameter("result", "fail");
}
And then have two RenderMapping methods:
#RenderMapping(params = "result=success")
public String success(){
#RenderMapping(params = "result=fail")
public String fail(){

HTML form POST method with querystring in action URL

Lets say I have a form with method=POST on my page.
Now this form has some basic form elements like textbox, checkbox, etc
It has action URL as http://example.com/someAction.do?param=value
I do understand that this is actually a contradictory thing to do, but my question is will it work in practice.
So my questions are;
Since the form method is POST and I have a querystring as well in my URL (?param=value)
Will it work correctly? i.e. will I be able to retrieve param=value on my receiving page (someAction.do)
Lets say I use Java/JSP to access the values on server side. So what is the way to get the values on server side ? Is the syntax same to access value of param=value as well as for the form elements like textbox/radio button/checkbox, etc ?
1) YES, you will have access to POST and GET variables since your request will contain both. So you can use $_GET["param_name"] and $_POST["param_name"] accordingly.
2) Using JSP you can use the following code for both:
<%= request.getParameter("param_name") %>
If you're using EL (JSP Expression Language), you can also get them in the following way:
${param.param_name}
EDIT: if the param_name is present in both the request QueryString and POST data, both of them will be returned as an array of values, the first one being the QueryString.
In such scenarios, getParameter("param_name) would return the first one of them (as explained here), however both of them can be read using the getParameterValues("param_name") method in the following way:
String[] values = request.getParameterValues("param_name");
For further info, read here.
Yes. You can retrieve these parameters in your action class.
Just you have to make property of same name (param in your case) with there getters and setters.
Sample Code
private String param;
{... getters and setters ...}
when you will do this, the parameters value (passed via URL) will get saved into the getters of that particular property. and through this, you can do whatever you want with that value.
The POST method just hide the submitted form data from the user. He/she can't see what data has been sent to the server, unless a special tool is used.
The GET method allows anybody to see what data it has. You can easily see the data from the URL (ex. By seeing the key-value pairs in the query string).
In other words it is up to you to show the (maybe unimportant) data to the user by using query string in the form action. For example in a data table filter. To keep the current pagination state, you can use domain.com/path.do?page=3 as an action. And you can hide the other data within the form components, like input, textarea, etc.
Both methods can be catched in the server with the same way. For example in Java, by using request.getParameter("page").