我的导航规则存在问题。重定向不起作用,但导航规则的返回值有效。
这是我的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>
这是我的豆子:
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()和amd这是有效的。但重定向不起作用。
有人为我提供线索,如果出现问题怎么办?
答案 0 :(得分:1)
正如SébastienVanmechelen在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中添加行。
我的代码中仍有一些内容。为什么使用Managed Bean方法进行简单导航(不涉及业务逻辑)。您可以使用h:outputLink甚至HTML标记来重定向用户。