4

When i use command button to redirect to pages, inside my project, u just need to give the name of the page with no extention, followed by ?faces-redirect=true in the action attribute and i will get redirected.

But what if i want to get redirected to an external page(example:www.google.com)?

I tried in many ways:

www.google.com, google.com, http://google.com

but i failed.

This is what i did:

<h:form>
            <h:commandButton 
                action="#{mainPageBB.goToLink}" value="#{msgs.clickhere}"/>
        </h:form>

and then the backing bean:

@Named("mainPageBB")
@RequestScoped
public class MainPageBB {

    @EJB
    private ILinkManagerEJB linkManagerEJB;


    public String goToLink() {      
        String link = linkManagerEJB.retrieveLink();
        if(link != null) {
                    System.out.println(link);       
            return link.trim() + "?faces-redirect=true";
        }
        return null;
    }

Note: the value returned by retrieveLink(); is always www.google.com(100% sure)

I get no errors at all in the console, the page just refreshes. Also i am sure the first if clause validates to true, so i see no reason for it to jump to return null.

Update

I tried with external context, but i get a 404 because it appends the current url to the link string:

public String goToRandomLink() {        
        String link = linkManagerEJB.retrieveRandomLink();
        if(link != null) {      
            ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
            try {
                externalContext.redirect(link.trim());
            } catch (IOException e) {               
                e.printStackTrace();
            }           
        }
        return null;
    }
javing
  • 12,307
  • 35
  • 138
  • 211

2 Answers2

6

Use ExternalContext.redirect()

ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
externalContext.redirect(link.trim());

and if you know the link already just use

<a href="#{someBean.someLink}">#{msg.someMessage}</a>

jmj
  • 237,923
  • 42
  • 401
  • 438
  • I think i am close, what happens when i do that is that the link String is appended to my projects URL, so i get a 404 error because i try to get redirected to: http://localhost:8080/MyProject/www.google.com – javing Aug 23 '11 at 10:18
  • yes exactly . we aren't returning the URL but the view name.See [this](http://stackoverflow.com/questions/3807746/jsf-change-the-action-for-a-hcommandbutton-2-0) from BalusC – jmj Aug 23 '11 at 10:23
  • The only reason i know the link is because i placed the value www.google.com in purpose in the database, but in the future i don't know what the destination will be. So i need to find the way for that commandbutton to redirect to the falue saved in the link variable – javing Aug 23 '11 at 10:26
  • I just had a look at the link you gave me, i understand that but my problem is that my destination is some unknown external url that is retrived from the database(no in some folder in my project). I am not sure if ExternalContext is what i need for this, i am kind of confused. – javing Aug 23 '11 at 10:30
  • Yes I know the link was just for the purpose of understanding how `return` works from action. answer for your question is clearly mentioned with `ExternalContext.redirect()` stuff mentioned in my answer body – jmj Aug 23 '11 at 10:34
  • Ok i got it, the approach you proposed is correct, the thing that was making the problem, was that the URL needed the http//:, it does not work without it. This is what i did: `String link = linkManagerEJB.retrieveRandomLink();FacesContext.getCurrentInstance().getExternalContext().redirect("http://"+link.trim());return link;` – javing Aug 23 '11 at 11:06
2

You can do this creating an input with type="button" between a link tag "a". Something like this:

<a href="http://www.google.com">
    <input type="button" value="Button text" />
</a>
MMelo
  • 21
  • 2