Prettyfaces: Set a bean property to some constant value, on observing a specific url pattern

别等时光非礼了梦想. 提交于 2019-12-04 05:24:56

问题


While writing the URL mapping rules for Prettyfaces in pretty-config.xml I would like to add a rule that whenever a particular pattern is observed in URL then set a specific constant value to bean property. For e.g. when there is a pattern like ../products/electronics then it should set bean property bean.category to ELECTRONICS_ITEMS. How do I do that ?


回答1:


You should simply use a path parameter and convert the value from the URL to your constant in a page action method. Something like this:

<url-mapping id="products"> 
  <pattern value="/products/#{bean.category}/" /> 
  <view-id value="/faces/shop/store.jsf" />
  <action>#{bean.action}</action>
</url-mapping>

And the action method:

public void action() {

  if( "electronics".equals(this.category) ) {
    this.category = "ELECTRONICS_ITEMS";
  }
  // more categories...

}



回答2:


as i am using prettyfaces annotations i dont know about pretty-config.xml if i got this condition then i will use it in annotations like this

 @URLAction(mappingId = "someMappingId", onPostback = false)
 public String setConstantValue() {

// set your bean here
return null;

}


来源:https://stackoverflow.com/questions/19470974/prettyfaces-set-a-bean-property-to-some-constant-value-on-observing-a-specific

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!