0

你好,我是stackoverflow的新手我正在使用primefaces 3.5我的问题是,是否有必要在静态块或构造函数中设置selectonemenu项目(Java util的映射)..比如:

滑块.xhtml

<h:outputLink value="circle">
<h:outputText value="click Circle!"/>
</h:outputLink>

漂亮的配置.xml

<url-mapping id="circle"> 
  <pattern value="/circle" /> 
  <view-id value="xhtmls/circle.xhtml" />
  <action>#{circleAction.action}</action>
 </url-mapping> 

CircleAction.java

public class CircleAction {
private String favCoffee2;
private Map<String,Object> coffee2Value;

//setter n getter

public Map<String,Object> getFavCoffee2Value() {
return coffee2Value;
}

public String action(){
coffee2Value = new LinkedHashMap<String,Object>();
coffee2Value.put("mohsin - Cream Latte", "Cream Latte"); //label, value
coffee2Value.put("mohsin - Extreme Mocha", "Extreme Mocha");
coffee2Value.put("mogsin - Buena Vista", "Buena Vista");

return "pretty:addcircle";
}           
} 

圆圈.xhtml

<p:selectOneMenu value="#{circleAction.favCoffee2}">
<f:selectItems value="#{circleAction.favCoffee2Value}" />
</p:selectOneMenu>

selectOneMenu 在这种情况下是空白的,但是当我从操作方法中删除下面的代码并将其放在静态块中时,selectOneMenu 将生成 java.util.map 中的所有项目;

coffee2Value = new LinkedHashMap<String,Object>();
coffee2Value.put("mohsin - Cream Latte", "Cream Latte"); //label, value
coffee2Value.put("mohsin - Extreme Mocha", "Extreme Mocha");
coffee2Value.put("mogsin - Buena Vista", "Buena Vista");
4

1 回答 1

0

问题是当您访问时/circle,PrettyFaces 将执行您的操作方法,并在您返回时pretty:addcircle将您重定向到新页面。因此,您的请求范围 bean(尤其是地图)的所有值都将丢失。只需null从该方法返回即可。在这种情况下,PrettyFaces 只会渲染xhtmls/circle.xhtml,这可能是您想要的。

于 2013-03-20T06:02:10.040 回答