Mentawai Web Framework

Redirection

Mentawai has three different types of redirect. A redirection forces the browser to perform another http request to the web server and discards all information from the action, such as its output.

The first one is the most common where you simply define a final URL for the redirection:

public class ApplicationManager extends org.mentawai.core.ApplicationManager {
	
    public void loadActions() {
		
        // Java style
        ActionConfig ac1 = new ActionConfig("/Action1", Action1.class);
        ac1.addConsequence(SUCCESS, new Redirect("/welcome.jsp"));
        ac1.addConsequence(ERROR, new Forward("/error.jsp"));
        addActionConfig(ac1);
        
        // Ruby style
        action("/Action1", Action1.class)
            .on(SUCCESS, redir("/welcome.jsp"))
            .on(ERROR, fwd("/error.jsp"));
        
	}
}
					

The second one is when you need to pass dynamic values to the URL being redirected. Mentawai can put all the action output values in the URL. If the value is not a String, the toString() method will be called. To accomplish this all you have to do is make use of a different redirect constructor:

public class ApplicationManager extends org.mentawai.core.ApplicationManager {
	
    public void loadActions() {
		
        // Java style
        ActionConfig ac1 = new ActionConfig("/Action1", Action1.class);
        ac1.addConsequence(SUCCESS, new Redirect("/welcome.jsp", true));
        ac1.addConsequence(ERROR, new Forward("/error.jsp"));
        addActionConfig(ac1);
        
        // Ruby style
        action("/Action1", Action1.class)
            .on(SUCCESS, redir("/welcome.jsp", true))
            .on(ERROR, fwd("/error.jsp"));
	}
}
				

So if you want to pass the id of an user, along with the redirect URL, you can do this:

public class Action1 extends BaseAction {
	
	public String execute() throws Exception {
		String id = "12";
		output.setValue("id", id);
		return SUCCESS;
	}
	
}
		

Since you defined the consequence to use the action output values, mentawai will redirect the browser to:

    http://www.myapp.com/welcome.jsp?id=12

The third type of redirection is when you don't know at compile time any information about the redirection URL. This can happen, for example, if you want to redirect the browser to a bookmark stored in the database. The org.mentawai.filter.RedirectAfterLoginFilter also uses this type of redirection, to redirect the browser, after the login, to the first page the user tried to access in the site.

When you use this type of redirection, mentawai looks for an action output value called url and uses it as the redirection url. To indicate this type of redirection, you use the empty value constructor:

public class ApplicationManager extends org.mentawai.core.ApplicationManager {
	
    public void loadActions() {
		
        // Java style
        ActionConfig ac1 = new ActionConfig("/Action1", Action1.class);
        ac1.addConsequence(SUCCESS, new Redirect());
        ac1.addConsequence(ERROR, new Forward("/error.jsp"));
        addActionConfig(ac1);
        
        // Ruby style
        action("/Action1", Action1.class)
            .on(SUCCESS, redir())
            .on(ERROR, fwd("/error.jsp"));
        
	}
}
				

Then an action would have to define an url output value:

public class Action1 extends BaseAction {
	
	public String execute() throws Exception {
		String url = "http://www.another.com/show.jsp?id=12";
		output.setValue(Redirect.REDIRURL_PARAM, url);
		return SUCCESS;
	}
	
}