动态调用消息包?

时间:2010-12-01 20:22:07

标签: java jsf jsf-2 facelets resourcebundle

我有一个非常简单的JSF 2.0项目。

我有一个index.xhtml文件向我展示拉什莫尔山的照片。 在这个页面上,我可以点击图片,我希望它转到“president.xhtml” - 没问题。一个简单的动作=“”......

我的问题是我的消息包文件(messages.properties)是用静态键和值设置的,例如:

jeffersonPageTitle=Thomas Jefferson
rooseveltPageTitle=Theodore Roosevelt
lincolnPageTitle=Abraham Lincoln
washingtonPageTitle=George Washington

在我的“president.xhtml”文件中,我希望它根据我点击的内容显示这些标题。

<h:form>
  <span class="presidentPageTitle">#{msgs['rushmore.president'],PageTitle}</span>
  <br />
  <h:graphicImage library="images" name="jefferson.jpg" styleClass="leftImage" />
  <span class="presidentDiscussion">#{msgs.washingtonDiscussion}</span>
  <br />
  <h:commandLink action="index" styleClass="backLink">${msgs.indexLinkText}</h:commandLink>
</h:form>

上面代码中的第二行是我的问题 - 我不知道如何在我的java代码中引用get-Method。代码在这里:

@ManagedBean // or @Named
@RequestScoped
public class Rushmore {
 private String outcome = null;
 private Rectangle washingtonRect = new Rectangle(70, 30, 40, 40);
 private Rectangle jeffersonRect = new Rectangle(115, 45, 40, 40);
 private Rectangle rooseveltRect = new Rectangle(135, 65, 40, 40);
 private Rectangle lincolnRect = new Rectangle(175, 62, 40, 40);

 public Rushmore() {}

 public void handleMouseClick(ActionEvent e) {
  FacesContext context = FacesContext.getCurrentInstance();
  String clientId = e.getComponent().getClientId(context);
  Map<String, String> requestParams = context.getExternalContext().getRequestParameterMap();


  int x = new Integer((String) requestParams.get(clientId + ".x")).intValue();
  int y = new Integer((String) requestParams.get(clientId + ".y")).intValue();

  outcome = null;

  if (washingtonRect.contains(new Point(x, y))) {
   outcome = "washington";
  }

  if (jeffersonRect.contains(new Point(x, y))) {
   outcome = "jefferson";
  }

  if (rooseveltRect.contains(new Point(x, y))) {
   outcome = "roosevelt";
  }
  if (lincolnRect.contains(new Point(x, y))) {
   outcome = "lincoln";
  }
  System.out.println(requestParams.keySet());
 }

 public String getPresident() {
  return outcome;
 }

 public String navigate() {
  if(outcome != null) {
   return "president";
  }
  else return null;
 }
}

这是我试图在president.xhtml文件中找到的getPresident方法......

非常感谢任何帮助:)

1 个答案:

答案 0 :(得分:0)

使用c:set

<html xmlns:c="http://java.sun.com/jsp/jstl" ...>
...
<c:set var="pageTitle" value="#{rushmore.president}PageTitle" />
<span class="presidentPageTitle">#{msgs[pageTitle]}</span>