How to build an action url in a Struts 2 action? - url

I have an Action that returns URLs which need to call another Struts2 action. In a JSP I would do <s:url>. Is there something equivalent to this that I can call inside of the action?

Since struts is creating your action class, simply use the Inject annotation and have struts tell you!
#Inject
public void setActionMapper(ActionMapper mapper) {
this.actionMapper = mapper;
}

You will probably need to construct the URL yourself inside of your action. One thing you may want to look at is org.apache.struts2.components.URL. This is the class that is used by the s:url tag to create the URL, although it may just be easier to create the URL yourself.

You can use Action Chaining... All you will do is you will call another action.

Use Struts forwards:
http://publib.boulder.ibm.com/infocenter/iadthelp/v6r0/index.jsp?topic=/com.ibm.etools.struts.doc/topics/cstrdoc006.html
http://www.mkyong.com/struts/struts-forwardaction-example/

Related

Grails add a controller action in runtime

I need to add a controller action on runtime.
In a plugin i add a dynamic method using doWithDynamicMethods but i can't invoke as an action.
I try using a mixin but it doesnt work. There is a bug with #grails.web.Mixin and is useless for me. And i'm not sure if i can call it as an action.
I understood that i need to add the #Action annotation to method that i create dynamiclly in the doWithDynamicMethods.
Should i use an AstTransformation. Or i miss something
I need to add a controller action on runtime.
No, you don't. You've got some other problem, and you think you can solve it by adding a controller action during runtime. But as you've learned, adding controller actions during runtime is really hard. You should solve your problem in some other way.
I don't know what your problem is, so I can't be too specific. But here's a generically useful trick. In UrlMappings.groovy, you can do this:
"awesome/$stuff"(controller: 'awesome', action: 'doStuff')
And then in AwesomeController.groovy:
public doStuff(String stuff) {
// whatever arbitrary dynamic dispatch logic you want goes here
}
Hope that helps.

Passing parameters to next view from Action method

I am looking out for different alternatives of passing parameter from an action method in JSF Managed bean to next view.
For example, I have an action method in my managed bean.
public String actionMethod01(){
String outcome = "nextPage";
return outcome;
}
If I want to pass a parameter to nextPage, one option which I have is:
outcome += "?param1=value1";
But, its not so convinient if I have multiple parameters to be passed.
Is there a better way for doing it?
Best regards,
Anand.
There's nothing in the JSF API which makes this easier. Just create a helper/utility method yourself which makes it more convenient so that you can end up with something like this:
return "nextPage" + toQueryString("param1", "value1", "param2", "value2");

URL Mapping prefix in Grails

Recently, I'm trying to migrating my application from CakePHP to Grails. So far it's been a smooth sailing, everything I can do with CakePHP, I can do it with much less code in Grails. However, I have one question :
In CakePHP, there's an URL Prefix feature that enables you to give prefix to a certain action url, for example, if I have these actions in my controller :
PostController
admin_add
admin_edit
admin_delete
I can simply access it from the URL :
mysite/admin/post/add
mysite/admin/post/edit/1
mysite/admin/post/delete/2
instead of:
mysite/post/admin_add
mysite/post/admin_edit/1
mysite/post/admin_delete/2
Is there anyway to do this in Grails, or at least alternative of doing this?
Grails URL Mappings documentation doesn't help you in this particular case (amra, next time try it yourself and post an answer only if it's any help). Daniel's solution was close, but wouldn't work, because:
the action part must be in a closure when created dynamically
all named parameters excluding "controller", "action" and "id" are accessible via the params object
A solution could look like this:
"/admin/$controller/$adminAction?/$param?"{
action = { "admin_${params.adminAction}" }
}
The key is NOT to name the parameter as "action", because it seems to be directly mapped to an action and can not be overriden.
I also tried a dynamic solution with generic prefixes and it seems to work as well:
"/$prefix/$controller/$adminAction?/$param?"{
action = { "${params.prefix}_${params.adminAction}" }
}
I didn't test it, but try this:
"mysite/$prefix/$controller/$method/$id?"{
action = "${prefix}_${method}"
}
It constructs the action name from the prefix and the method.
Just take a look on grails URL Mappings documentation part

ASP.NET MVC: Uses a delegate field as an action method?

Is it possible in ASP.NET MVC via some extension/override points to allow a "delegate field" to be used as an "action"?
Something like:
using System;
using System.Web.Mvc;
namespace Company.Web.Controllers
{
public class SwitchboardController : BaseController
{
public Func<ActionResult> Index, Admin, Data, Reports;
public SwitchboardController()
{
// Generic views
Index = Admin = Data = Reports =
() => View();
}
}
}
I know I'm a little hell-bent for this one but if this is possible it'd open up many new ways of making actions. You could, for example, have Django-style generic views in MVC with only a single line of code to define the action or have different ways to factor duplicate logic across multiple controllers.
I'm not quiet sure where would be the place to slap this logic into or how much work would be required to alter something so fundamental in the framework.
You will probably have to build your own Controller factory. This class builds controllers, and implements IControllerFactory. You can inherit from DefaultControllerFactory. Override CreateController() to return your own IController.
Register your controller factory in Application_Start() of MvcApplication using this line:
ControllerBuilder.Current.SetControllerFactory(typeof(MyControllerFactory));
In your implementation of IController, override the Execute method. You can use the RequestContext to decide which delegate to invoke. It would probably be easiest to inherit from ControllerBase, and override Execute in there if you don't want to fully implement IController.
The RequestContext passed into Execute carries a RouteData object. This is a dictionary populated by the routing engine that tells you what action should be invoked, and any parameters. You can get the action name like this:
//context is a RequestContext object passed to IController.Execute()
string actionName = requestContext.RouteData.Values["action"];
You could even define your action as a dictionary, and just pull them out once you get the action name.
One last thing, normal action methods return an ActionResult, which the framework uses to decide which view to render. Once you execute your delegates, I think you'll have to set some stuff manually in your special base controller. I'm not exactly sure what to set or how to get your View executed from here without cracking open the MVC source.
Good luck! This looks like an interesting idea.
As you seem to be implementing a BaseController in your code sample, if you override the Execute (from the IController) you'll be able to interpret the request => action however you like.
No, it isn't. The base controller is looking for methods and not for fields to dispatch an action.
EDIT:
Sorry, I was a bit fast and fixed to the standard classes provided.
You can do that but you have to overwrite the Execute Method in your controller or implement and provide your own IActionInvoker to dispatch the action to fields. Look into the post action processing in detail. It explains the dispatching in detail.

Which action is executing during an HtmlHelper extension method

I'm working on a menu-generating HtmlHelper extension method. This method will need to know which Action is being executed. So if Home/Index is executing, the extension method would show all links to other actions that're "coordinated." In a sense, all I need to know during the execution of the Home controller's Index action is the name of the Controller and the name of the Action that are being executed so that other logic can be executed. Is this possible?
Try this
var action = HtmlHelper.ViewContext.RouteData.Values["action"];
var controller = HtmlHelper.ViewContext.RouteData.Values["controller"];
I do something similar with a filter attribute. You can get the action name like this:
filterContext.RouteData.Values["action"].ToString();
I use this to disable the menu item that represents the current context.
I need something like this, but not quite. I would love to be able to get some strongly typed way of knowing which action is executing.
So clarify in doing AOP where i only allow access to a given action if the user has rights for that action.
The problem with using a string for determining which rule to check for, is that if some developer renames an action, I wont get a compile error telling me that my rule is broken.

Resources