我正在使用prettyfaces来处理网址路径。
我想要做的是在链接后设置语言属性(选择语言)。像这样:
<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。
答案 0 :(得分:1)
这是一个仍然使用其他网址的选项。它不是非常优雅,但可能会完成你想要的东西:
<pretty:link mappingId="LanguageTopic">
<f:param name="link" value="#{currentTopic.link}" />
<f:param name="lang" value="eng" />
English
</pretty:link>
漂亮-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; }
}
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
}
}
}