简体   繁体   English

如何使用JSF创建简单的重定向?

[英]How to create a simple redirect with JSF?

How do I create a simple redirect with jsf? 如何使用jsf创建简单的重定向?

I tried: 我试过了:

    <h:form>
        <h:commandButton action="www.google.de" value="go to google" />
    </h:form> 

But when I click the button, I just stay on the index page. 但是当我点击按钮时,我只是停留在索引页面上。 Nothing happens! 什么都没发生! What is wrong here? 这有什么不对?

Is JSF absolutely necessary here? JSF绝对必要吗? You don't seem to need to submit anything to your side at all. 您似乎根本不需要向您提交任何内容。 Just use plain HTML. 只需使用纯HTML。

<form action="http://www.google.de">
    <input type="submit" value="Go to Google" />
</form>

Please note that the URL to the external site must include the scheme (the http:// part), otherwise it would just be submitted relative to the current request URI, such as http://example.com/context/www.google.de . 请注意,外部网站的URL必须包含方案( http://部分),否则只会相对于当前请求URI提交,例如http://example.com/context/www.google.de

If you really need to submit to your side, eg to preprocess and/or log something, then you could use ExternalContext#redirect() in the action method. 如果你真的需要提交给你,例如预处理和/或记录某些东西,那么你可以在action方法中使用ExternalContext#redirect()

<h:form>
    <h:commandButton value="Go to Google" action="#{bean.submit}" />
</h:form>

with

public void submit() throws IOException {
    // ...

    FacesContext.getCurrentInstance().getExternalContext().redirect("http://www.google.de");
}

You can use: 您可以使用:

<h:commandButton value="Go to Google" type="button" onclick="window.location.href = 'http://www.google.de';" />

No need for a form. 不需要表格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM