【问题标题】:JSF 2.0 Navigation-Rules doesn't workJSF 2.0 导航规则不起作用
【发布时间】:2013-12-27 08:15:18
【问题描述】:

我的导航规则有问题。重定向不起作用,但导航规则的返回值起作用。

这是我的 faces-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- This file is not required if you don't need any extra configuration. -->
<faces-config version="2.0" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://java.sun.com/xml/ns/javaee
        http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd">

    <!-- This descriptor activates the JSF 2.0 Servlet -->
    <validator>
        <validator-id>usernameValidator</validator-id>
        <validator-class>ch.akros.emember.validator.UsernameValidator</validator-class>
    </validator>

    <!-- Write your navigation rules here. You are encouraged to use CDI for 
        creating @Named managed beans. -->
    <navigation-rule>
        <from-view-id>index.xhtml</from-view-id>
        <navigation-case>
            <from-outcome>registration</from-outcome>
            <to-view-id>/pages/registration/registration.xhtml</to-view-id>
            <redirect />
        </navigation-case>
    </navigation-rule>
    <navigation-rule>
        <from-view-id>/pages/registration/verification.xhtml</from-view-id>
        <navigation-case>
            <from-outcome>registrationDetail</from-outcome>
            <to-view-id>/pages/registration/registrationDetail.xhtml</to-view-id>
            <redirect />
        </navigation-case>
    </navigation-rule>
</faces-config>

这是我的 Bean:

package ch.akros.emember.controller;

import javax.annotation.PostConstruct;
import javax.enterprise.inject.Model;
import javax.enterprise.inject.Produces;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;

import ch.akros.emember.facade.ClubFacadeRemote;
import ch.akros.emember.facade.UserFacadeRemote;
import ch.akros.emember.model.Club;
import ch.akros.emember.model.User;

// The @Model stereotype is a convenience mechanism to make this a request-scoped bean that has an
// EL name
// Read more about the @Model stereotype in this FAQ:
// http://sfwk.org/Documentation/WhatIsThePurposeOfTheModelAnnotation
@Model
public class RegistrationController {

   @Inject
   private FacesContext     facesContext;

   @Inject
   private UserFacadeRemote ufr;

   @Inject
   private ClubFacadeRemote cfr;

   private String           verification_key;

   private Club             newClub;

   private User             newUser;

   @Produces
   @Named
   public Club getNewClub() {

      return newClub;
   }

   @Produces
   @Named
   public User getNewUser() {

      return newUser;
   }

   public void register() throws Exception {

      try {
         ufr.saveUser(newUser);

         newClub.setUser(newUser);
         newClub.generateVerificationKey(newUser.getEmail());
         cfr.saveClub(newClub);
         facesContext.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Registered!",
               "Registration successful"));
      } catch (Exception e) {
         String errorMessage = getRootErrorMessage(e);
         FacesMessage m = new FacesMessage(FacesMessage.SEVERITY_ERROR, errorMessage, "Registration Unsuccessful");
         facesContext.addMessage(null, m);
      }
   }

   @PostConstruct
   public void init() {

      newClub = new Club();
      newUser = new User();
   }

   private String getRootErrorMessage(Exception e) {

      // Default to general error message that registration failed.
      String errorMessage = "Registration failed. See server log for more information";
      if (e == null) {
         // This shouldn't happen, but return the default messages
         return errorMessage;
      }

      // Start with the exception and recurse to find the root cause
      Throwable t = e;
      while (t != null) {
         // Get the message from the Throwable class instance
         errorMessage = t.getMessage();
         t = t.getCause();
      }
      // This is the root cause message
      return errorMessage;
   }

   public String saveUser() {

      return "registrationDetail";
   }

   /**
    * Get the verification_key.
    * 
    * @return verification_key
    */
   public String getVerification_key() {

      return verification_key;
   }

   /**
    * Set the verification_key.
    * 
    * @param verification_key, the verification_key to set
    */
   public void setVerification_key(String verification_key) {

      this.verification_key = verification_key;
   }
}

从我的页面它调用方法 saveUser() 并且这有效。但是重定向不起作用。

有人给我线索,如果这里出错了怎么办?

【问题讨论】:

  • 重定向不起作用是什么意思?当您访问方法saveUser 并返回"registrationDetail" 时,浏览器中的地址栏是否从index.xhtml 更改为/pages/registration/registrationDetail.xhtml,或者您可能想要/需要触发其他导航规则?
  • 我来自地址 /pages/registration/verification.xhtml,在调用 saveUser() 操作后会转到 /pages/registration/registrationDetail.xhtml
  • 根据您发布的内容,它将从index.xhtml 工作到另一个视图。看起来您没有发布相关文章来复制您的问题。
  • 不,我不是来自我的 index.xhtml 页面。我直接打开verification.xhtml页面。第二条导航规则将我从 verify.xhtml 重定向到 RegistrationDetail 页面。
  • 只是一个观察。由于您使用的是 JSF 2.2,因此在 faces-config.xml 中定义导航规则不是强制性的。

标签: jsf


【解决方案1】:

正如 Sébastien Vanmechelen 在 JSF 2.2 中正确指出的那样,faces-config.xml 不是强制性的(我可能会添加它过于复杂)。

只需将您的 saveUser() 方法更改为:

public String saveUser() {

      return "registrationDetail";
}

到:

public String saveUser() {
      return "/pages/registration/registrationDetail.xhtml?faces-redirect=true";
}

它可以从任何视图工作,无需在 faces-config.xml 中添加行。

您的代码中仍有一些内容我没有理解。为什么要使用托管 Bean 方法进行简单导航(不涉及业务逻辑)。您可以只使用 h:outputLink 甚至 HTML 标记来重定向用户。

【讨论】:

    猜你喜欢
    • 2011-11-26
    • 2012-02-05
    • 1970-01-01
    • 1970-01-01
    • 2012-05-04
    • 2013-10-22
    • 1970-01-01
    • 2014-04-26
    • 1970-01-01
    相关资源
    最近更新 更多