JSF h:outputLink 在 GET 之前调用 bean 方法

JSF h:outputLink call bean method before GET

我需要将一些参数从一个 xhtml 发送到另一个 xhtml,但我不希望这些参数出现在 URL 中。怎么做?我可以使用 p:commandLink,但我不知道如何从 bean 方法打开目标页面。目标页面应该可以通过友好 URL 访问,而不是通过 xhtml 名称访问。 此代码将打开页面 /users/view。如何发送参数而不让它们出现在 URL 中?

   <h:outputLink value="/users/view">
      <h:outputText value="#{entry.employee}" />
   </h:outputLink>

忽略 <h:commandLink> 操作方法中的 strange design, you could use put the data in the flash scope and send a redirect

public void view() throws IOException {
    ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
    ec.getFlash().put("employee", employee);
    ec.redirect(ec.getRequestContextPath() + "/users/view");
}

然后在与目标页面关联的支持 bean 中:

@PostConstruct
public void init() {
    ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
    employee = (Employee) ec.getFlash().get("employee");
}

另请参阅:

  • Pass an object between @ViewScoped beans without using GET params
  • What is the difference between redirect and navigation/forward and when to use what?