<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
	<channel>
		<title><![CDATA[Latest posts for the topic "Setting up actions"]]></title>
		<link>http://book.mentaframework.org/posts/list/3.page</link>
		<description><![CDATA[Latest messages posted in the topic "Setting up actions"]]></description>
		<generator>JForum - http://www.jforum.net</generator>
			<item>
				<title>Setting up actions</title>
				<description><![CDATA[ This chapter is important because it will show you all the ways you can use to setup your actions in the application manager. If you decide not to configure anything, then Mentawai will use its conventions, for example a request to http://www.mysite.com/HelloAction.hello.mtw will call the hello() method of the HelloAction class. The default consequence for the action will be a forward to the /HelloAction/hello.jsp.<br /> <br /> As we have discussed in earlier chapters, sometimes there is no way out and you have to configure your actions. This chapter will show you how to do it.<br /> <br /> [code]<br /> <br /> public class ApplicationManager extends org.mentawai.core.ApplicationManager {<br /> <br />     @Override<br />     public void loadActions() {<br />         <br />         // More verbose style...<br />         <br />         addGlobalFilter(new AuthenticationFilter());<br /> <br />         addGlobalConsequence(LOGIN, new Redirect("/login.jsp"));<br />         <br />         ActionConfig ac = new ActionConfig("/User", UserAction.class);<br />         ac.addFilter(new VOFilter("user", User.class);<br />         ac.addConsequence(SUCCESS, new Forward("/ok.jsp"));<br />         ac.addConsequence(JSP, new Redirect("/users.jsp"));<br />         <br />         addActionConfig(ac);<br />         <br />         // Less verbose style...<br />         <br />         filter(new AuthenticationFIlter());<br />         <br />         on(LOGIN, redir("/login.jsp"));<br />         <br />         action("/User", UserAction.class)<br />             .filter(new VOFilter("user", User.class))<br />             .on(SUCCESS, fwd("/ok.jsp"))<br />             .on(JSP, redir("/users.jsp"));<br />     }<br /> <br /> [/code]<br /> <br /> Note that we are adding a global filter that will be applied to all actions. We are also adding a filter specific to the action ([i]VOFilter[/i]). Two consequences (one for [i]JSP [/i]and one for [i]SUCCESS[/i]) and a global consequence for the result [i]LOGIN[/i] are being added as well.<br /> <br /> No inner action was specified, so by default this configuration will be considered for all inner actions inside UserAction.class. The following actions will be executed with the filters and consequences configured above:<br /> <br /> http://www.mysite.com/User.mtw<br /> http://www.mysite.com/User.add.mtw<br /> http://www.mysite.com/User.del.mtw<br /> <br /> Basically all inner actions inside the [i]UserAction [/i]class will have the configuration specified for the [i]UserAction[/i] class. Now let's get more specific.<br /> <br /> You can add consequences and filters specific for a single inner action. Let's add a filter and a consequence specific for the [i]add [/i]inner action, in other words, for requests to http://www.mysite.com/User.add.mtw:<br /> <br /> [code]<br />     @Override<br />     public void loadActions() {<br />         <br />         // More verbose style...<br />         <br />         addGlobalFilter(new AuthenticationFilter());<br /> <br />         addGlobalConsequence(LOGIN, new Redirect("/login.jsp"));<br />         <br />         ActionConfig ac = new ActionConfig("/User", UserAction.class);<br />         ac.addFilter(new VOFilter("user", User.class);<br />         ac.addConsequence(SUCCESS, new Forward("/ok.jsp"));<br />         ac.addConsequence(JSP, new Redirect("/users.jsp"));<br />         <br />         ac.addFilter(new AuthorizationFilter(new Permission("add")), "add");<br />         ac.addConsequence(SUCCESS, "add", new Forward("/add.jsp"));<br />         <br />         addActionConfig(ac);<br />         <br />         // Less verbose style...<br />         <br />         filter(new AuthenticationFIlter());<br />         <br />         on(LOGIN, redir("/login.jsp"));<br />         <br />         action("/User", UserAction.class)<br />             .filter(new VOFilter("user", User.class))<br />             .on(SUCCESS, fwd("/ok.jsp"))<br />             .on(JSP, redir("/users.jsp"))<br />             <br />             .filter(new AuthorizationFilter(new Permission("add")), "add")<br />             .on(SUCESS, "add", fwd("/add.jsp"));<br />     }<br /> [/code]<br /> <br /> So for our example, we will have the following filters being executed, in the following order:<br /> <br /> AuthenticationFilter -&gt; VOFilter -&gt; AuthorizationFilter -&gt; UserAction.add<br /> <br /> Note that global filters are always executed first, then comes the action specific filters, then comes the inner action specific filters.<br /> <br /> For consequences, the rule is simple:<br /> <br /> First try to get a consequence specific to the inner action. If not found, try to get a consequence specific to the action. If not found, try to get a global consequence. If still not found, go by the Mentawai conventions: forward to /User/add.jsp.<br /> <br /> You can get even more specific creating a whole [i]ActionConfig [/i]object just for the inner action you want to configure. The filters and consequences you define will be applied only for the inner action specified in the action config. Below is an example:<br /> <br /> [code]<br /> <br /> // More verbose style...<br /> <br /> ActionConfig ac = new ActionConfig("/User", UserAction.class, "add");<br /> <br /> ac.addFilter(new AuthorizationFilter(new Permission("add")));<br /> <br /> ac.addConsequence(SUCCESS, new Forward("/add.jsp"));<br /> <br /> // Less verbose style...<br /> <br /> action("/User", UserAction.class, "add")<br />     .filter(new AuthorizationFilter(new Permission("add")))<br />     .on(SUCCESS, fwd("/add.jsp"));<br /> [/code]<br /> <br /> You can play with the order of the filters with some methods like [i]ActionConfig.addFilterFirst()[/i], that will add a filter to be executed BEFORE the global filters. You can also make your action implement the [i]GlobalFilterFree [/i]interface, to signal to the controller that it does not want any global filters. And you can also add a global filter that will be executed LAST, in other words, after all other filters have been executed. This will be rare cases, when for some very particular scenario you need to take more control in the invocation chain of filters.<br /> <br /> Below are some examples of these rare methods:<br /> <br /> [code]<br /> <br /> // More verbose style...<br /> <br /> ac.addFilterFirst(new TheFirstFilter()); // before global filters<br /> <br /> addGlobalFilter(new TheLastFilter(), true); // true = LAST<br /> <br /> // Less verbose style...<br /> <br />     .filterFirst(new TheFirstFilter()) // before global filters...<br /> <br /> filterLast(new TheLastFilter()); // after all other filters...<br /> <br /> [/code]<br /> <br /> [code]<br /> <br /> <br /> public class UserAction extends BaseAction implements GlobalFilterFree {<br />     <br />     public String add() {<br />         <br />         // ...<br />     }<br />     <br />     public String execute() {<br />         <br />         // ...<br />     }<br />     <br />     public boolean isGlobalFilterFree(String innerAction) {<br />         <br />         if (innerActoin != null && innerAction.equals("add")) {<br />             <br />             return true;<br />         }<br />         <br />         return false;<br />     }<br /> }<br /> [/code]<br /> ]]></description>
				<guid isPermaLink="true">http://book.mentaframework.org/posts/preList/14/15.page</guid>
				<link>http://book.mentaframework.org/posts/preList/14/15.page</link>
				<pubDate><![CDATA[Sat, 5 Jan 2008 16:27:24]]> GMT</pubDate>
				<author><![CDATA[ saoj]]></author>
			</item>
	</channel>
</rss>
