faces icon indicating copy to clipboard operation
faces copied to clipboard

Allow redirect via Annotation on action

Open tandraschko opened this issue 4 years ago • 12 comments

Currently the return value of the action triggers a navigation

public String action() {
    return "myNewView.xhtml"
}

to force a redirect, we have to add a string param:

public String action() {
    return "myNewView.xhtml?faces-redirect=true"
}

Wouldnt it be nice if we allow:

@Redirect
public String action() {
    return "myNewView.xhtml"
}

and

@Redirect("myNewView.xhtml")
public void action() {

}

tandraschko avatar Feb 17 '21 10:02 tandraschko

Will this also be applicable to void methods e.g.

@Redirect public void action() { doSomeOtherAction(); }

edwin-amoakwa avatar Feb 17 '21 13:02 edwin-amoakwa

I rather see it only apply to void methods and then taking an argument

manorrock avatar Feb 17 '21 14:02 manorrock

We could even allow both, see my updated example.

tandraschko avatar Feb 17 '21 14:02 tandraschko

I think this is great if it also allows external URLs.

Old

public void action() throws IOException {
    // ...
    FacesContext.getCurrentInstance().getExternalContext().redirect("https://google.com");
}

New (just match ^https?://)

@Redirect("https://google.com")
public void action() { // Look ma, no throws!
    // ...
}

And EL expressions.

Old

public String action() {
    // ...
    boolean flag = compute();
    return flag ? "foo" : "bar";
}

New

private boolean flag;

@Redirect("#{bean.flag ? 'foo' : 'bar'}")
public void action() {
    // ...
    flag = compute();
}

public boolean isFlag() {
    return flag;
}

BalusC avatar Feb 25 '21 19:02 BalusC

will prototype this in MF as next task

tandraschko avatar Apr 14 '21 09:04 tandraschko

What should we do with forward? Either make a @Forward or @Navigate with redirect or forward param

tandraschko avatar Apr 14 '21 09:04 tandraschko

I'd rather not implement/advocate a "forward" for now. It only encourages poor practices.

BalusC avatar Apr 14 '21 09:04 BalusC

Created a el-spec feature request, which would make this feature really easy to implement: https://github.com/eclipse-ee4j/el-ri/issues/154

tandraschko avatar Apr 14 '21 11:04 tandraschko

I currently think about something like that:

@Target(value = ElementType.FIELD)
@Retention(value = RetentionPolicy.RUNTIME)
public @interface Redirect
{
    /**
     * The relative or absolute path, which will be passed to the NavigationHandler.
     * EL-Expressions are also supported.
     * 
     * This parameter is required when the return-type of the method is void. 
     * 
     * @return 
     */
    String value() default "";

    boolean includeViewParams() default false;
}

which could be easily accessed in ActionListenerImpl

tandraschko avatar Apr 14 '21 11:04 tandraschko

EL feature is implemented now, i will do a prototype in MF next week

tandraschko avatar Sep 09 '21 09:09 tandraschko

@BalusC @arjantijms i have propably some time to work on it. Would you like to see it? im sure its only a small change, similar to my CDI bridges

tandraschko avatar Jul 09 '24 15:07 tandraschko

@tandraschko sounds good! Sure would like to see it.

arjantijms avatar Jul 09 '24 20:07 arjantijms