JSF - 在点击链接时设置属性

发布于 2024-11-06 23:15:52 字数 700 浏览 4 评论 0原文

我使用 prettyfaces 来处理 URL 路径。

我想要做的是在点击链接后设置语言属性(选择语言)。像这样的事情:

<h:link outcome="#{bean.currentPath}" value="English"> <!-- path goes to self -->
    <f:param name="link" value="#{currentTopic.link}" />
    <f:param name="lang" value="eng" />
</h:link>

prettyconfig 检索链接并跟踪它,但是如何将参数发送到语言 bean?

<url-mapping id="">
    <pattern value="/topic/#{link:bean.link}" />
    <view-id>/faces/topic.jsf</view-id>
</url-mapping>

该 bean 是无状态的 (@RequestScoped),而语言 bean 是有状态的 (@SessionScoped)。两者都是@ManagedBeans。

I'm using prettyfaces for handling URL paths.

What I want to do is set a language property upon following a link (to choose language). Something like this:

<h:link outcome="#{bean.currentPath}" value="English"> <!-- path goes to self -->
    <f:param name="link" value="#{currentTopic.link}" />
    <f:param name="lang" value="eng" />
</h:link>

The prettyconfig retrieves the link and follows it, but how can I send the parameter to the language bean?

<url-mapping id="">
    <pattern value="/topic/#{link:bean.link}" />
    <view-id>/faces/topic.jsf</view-id>
</url-mapping>

The bean is stateless (@RequestScoped) and the language bean is stateful (@SessionScoped). Both are @ManagedBeans.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

懵少女 2024-11-13 23:15:52

这里有一个仍然使用休息网址的选项。它不是非常优雅,但可能实现您正在寻找的东西:

<pretty:link mappingId="LanguageTopic">
    <f:param name="link" value="#{currentTopic.link}" />
    <f:param name="lang" value="eng" />
    English
</pretty:link>

pretty-config.xml:

<url-mapping id="LanguageTopic">
    <pattern value="/topic/#{link:linkBean.link}/#{lang:languageBean.lang}/" />
    <view-id>/faces/topic.jsf</view-id>
    <action>#{linkBean.cleanUrl}</action>
</url-mapping>

<url-mapping id="Topic">
    <pattern value="/topic/#{link:linkBean.link}/" />
    <view-id>/faces/topic.jsf</view-id>
</url-mapping>

语言bean:

@ManagedBean(name = "languageBean")
@SessionScoped
public class LanguageBean implements Serializable {

    private static final long serialVersionUID = 4460586548949990787L;

    /**
     * Stores the users language preference
     */
    private String lang;

    public String getLang() { return this.lang; }
    public void setLang(String lang) { this.lang = lang; }

}

链接bean:

@ManagedBean(name = "linkBean")
@RequestScoped
public class LinkBean {

    /**
     * Stores the resource the user is currently accessing
     */
    private String link;

    public String getLink() { return this.link; }
    public void setLink(String link) { this.link = link; }

    /**
     * Cleans language specific rest urls to non-specific urls
     */
    public void cleanUrl() {
        final ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();

        try {
            externalContext.redirect("/topic/" + this.link + "/");
        } catch (IOException ex) {
            //log or whatever you want here
        }    
    }
}

Here's an option for you that still uses rest urls. It's not extremely elegant but probably accomplishes what you're looking for:

<pretty:link mappingId="LanguageTopic">
    <f:param name="link" value="#{currentTopic.link}" />
    <f:param name="lang" value="eng" />
    English
</pretty:link>

pretty-config.xml:

<url-mapping id="LanguageTopic">
    <pattern value="/topic/#{link:linkBean.link}/#{lang:languageBean.lang}/" />
    <view-id>/faces/topic.jsf</view-id>
    <action>#{linkBean.cleanUrl}</action>
</url-mapping>

<url-mapping id="Topic">
    <pattern value="/topic/#{link:linkBean.link}/" />
    <view-id>/faces/topic.jsf</view-id>
</url-mapping>

language bean:

@ManagedBean(name = "languageBean")
@SessionScoped
public class LanguageBean implements Serializable {

    private static final long serialVersionUID = 4460586548949990787L;

    /**
     * Stores the users language preference
     */
    private String lang;

    public String getLang() { return this.lang; }
    public void setLang(String lang) { this.lang = lang; }

}

link bean:

@ManagedBean(name = "linkBean")
@RequestScoped
public class LinkBean {

    /**
     * Stores the resource the user is currently accessing
     */
    private String link;

    public String getLink() { return this.link; }
    public void setLink(String link) { this.link = link; }

    /**
     * Cleans language specific rest urls to non-specific urls
     */
    public void cleanUrl() {
        final ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();

        try {
            externalContext.redirect("/topic/" + this.link + "/");
        } catch (IOException ex) {
            //log or whatever you want here
        }    
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文