Struts 2 post back default - annotations

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.

Related

Add field on registration form for specific website

I created a custom module that add a field to the registration form and account modification.
The problem is that it is added to all websites. I only need it on a specific website.
This is the module I created:
https://github.com/stfn97/magento2-custom-registration-field
If you want to add logic to a template (for example, render it only if certain "requirements" are met) you will use either a block or a view model.
In this case I recommend using a block because the Magento\Framework\View\Element\Template class contains a _toHtml() method which you can override to either render or not render your template.
class MyBlockClass extends Template {
public function _toHtml() {
if (what you want your condition to be) {
return '';
}
return parent::_toHtml();
}
}
you can use a ternary operator for this but I thought the above notation would be more obvious
Magento\Store\Model\StoreManagerInterface can then be used to get the store the customer is currently in and thus the website as well.
If you call Magento\Store\Model\StoreManagerInterface::getStore() without an argument (store id) it will return the store the customer is currently in.
Because the above method returns a Magento\Store\Api\Data\StoreInterface the Magento\Store\Api\Data\StoreInterface::getWebsiteId() can then be used to get the website associated with the store the customer is currently in.
If you want more information about the website you'll need a Magento\Store\Api\Data\WebsiteInterface, this you can get by calling Magento\Store\Model\StoreManagerInterface::getWebsite($websiteId) with the website id returned by the previously mentioned method.
Combining all this you can determine the website the customer is currently in and use that to either render or not render the template using the Magento\Framework\View\Element\Template::_toHtml() method.

I want a custom attribute to be fired automatically without writing that on the web api method

I want a custom attribute to be fired automatically without writing that on the web api method.
What is the way I can achieve that?
Let us say for example you have an action filter attribute
public class MyApiActionFilterAttribute : FilterAttribute {
//...
}
With that you can assign it to the class or action depending on how you want it to be used
public class MyApiController : ApiController {
[MyApiActionFilter]
public IHttpActionResult MyAction() {
return Ok();
}
}
This applies to this action only.
But, if you wanted it to be applied on all actions (globally) within the web API, you would need to add it to the configuration.
GlobalConfiguration.Configuration.Filters.Add(new MyApiActionFilterAttribute ());
This is normally done at startup web configuring the web API.
Note that in doing the above, the filter will be applied to all requests.

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(){

POST -ing data with Laravel 4's resourceful controller does not work (store method) but works using index (GET) method. What am I doing wrong?

Hi I have a resource controller in Laravel 4. It has all the default methods generated by artisan's controller:make.
Models etc are in place.
User clicks on a link in a view that does a URL::route to a named route pointing at a controller action. It points to the 'store()' method in the controller, which is meant to be a POST method.
I write my code in the 'store()' method to handle this request. It uses eloquent to insert data into db. It returns a plain text response with HTTP code 200.
When user clicks on the above mentioned link (that points to the store() method), it seems the browser simply jumps to the index (GET) of that controller and the code doesn't run because the store() method is bypassed.
When I move all code from within the store() method into the index() method, everything works as expected.
What am I doing wrong here that my 'store()' method is not handling my code. Even when creating URL to the store action directly using URL::action, this fails.
Can someone please enlighten me?
Code:
Store method:
public function store()
{
$itemsArray = Session::get('sdata');
$cartItem = new Cart;
$cartItem->session_id = Session::get('sid');
$cartItem->items = json_encode($itemsArray);
$cartItem->save();
return Response::make('an item was added to carts', 200);
}
View:
Go
Same result for this view also:
`Go`
This is because <a> tag, is able to send only GET request. Try to create a new method, for example addToCart, and then set new rout on routes.php

Identifying Redirect/Forward Action in Struts2 Incerceptor

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.
}