【问题标题】:Prettyfaces: Set a bean property to some constant value, on observing a specific url patternPrettyfaces:在观察特定的 url 模式时将 bean 属性设置为某个常量值
【发布时间】:2013-10-20 16:54:32
【问题描述】:
在pretty-config.xml 中为 Prettyfaces 编写 URL 映射规则时,我想添加一条规则,即每当在 URL 中观察到特定模式时,就为 bean 属性设置一个特定的常量值。
例如当有像../products/electronics 这样的模式时,它应该将bean 属性bean.category 设置为ELECTRONICS_ITEMS。我该怎么做?
【问题讨论】:
标签:
java
jsf
url-rewriting
rewrite
prettyfaces
【解决方案1】:
您应该简单地使用路径参数并将 URL 中的值转换为页面操作方法中的常量。像这样的:
<url-mapping id="products">
<pattern value="/products/#{bean.category}/" />
<view-id value="/faces/shop/store.jsf" />
<action>#{bean.action}</action>
</url-mapping>
以及动作方法:
public void action() {
if( "electronics".equals(this.category) ) {
this.category = "ELECTRONICS_ITEMS";
}
// more categories...
}
【解决方案2】:
因为我正在使用漂亮的注释,所以我不知道 pretty-config.xml 如果我遇到这种情况,那么我将在这样的注释中使用它
@URLAction(mappingId = "someMappingId", onPostback = false)
public String setConstantValue() {
// set your bean here
return null;
}