我无法理解为什么我无法从registrationFlow 导航到受保护目录。
在我的应用程序中,index.html
我有一个将用户重定向到注册表单的按钮(这是一个流程)。这就是用户选择是否要登录或注册新用户的部分。我的文件夹结构是:
- 网页
- 索引.xhtml
- 受保护的目录
- 主页面.xhtml
- 登记目录
- 注册.xhtml
- addClients.xhtml
<h:form>
<h:commandButton class="formButton" value="Zaloguj" action="/Login?faces-redirect=true"/>
<h:commandButton class="formButton" value="Zarejestruj się" action="registration" immediate="true"/>
</h:form>
我有一个registrationFlow.xml
文件,它定义了如何从一个页面重定向到另一个页面的行为
<?xml version='1.0' encoding='UTF-8'?>
<faces-config version="2.2"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd">
<flow-definition id="registration">
<view id="registration">
<vdl-document>/registration/registration.xhtml</vdl-document>
</view>
<flow-return id="Cancel">
<from-outcome>
/index
</from-outcome>
</flow-return>
<flow-return id="saveClient">
<from-outcome>
/protected/mainPage
</from-outcome>
</flow-return>
</flow-definition>
</faces-config>
我的registration.xhtml
档案
<h:body>
<p>
#{ null != facesContext.application.flowHandler.currentFlow}
#{facesContext.application.flowHandler.currentFlow.id}
</p>
<div class="registerContainer">
<div class="formContainer">
<h:form id="registerForm" class="registerForm">
<h:outputText value="Username "/>
<h:inputText id="username" value="#{registration.username}"></h:inputText>
<h:outputText value="Password "/>
<h:inputSecret id="password" value="#{registration.password}"></h:inputSecret>
<h:outputText value="Firstname "/>
<h:inputText id="first" value="#{registration.first}"></h:inputText>
<h:outputText value="Lastname "/>
<h:inputText id="last" value="#{registration.last}"></h:inputText>
<h:outputText value="Email "/>
<h:inputText id="email" value="#{registration.email}"></h:inputText>
<h:commandButton id="register" action="#{registration.register}" value="Zarejestruj" style="margin: 15px auto; width: 35%; "></h:commandButton>
<h:commandButton action="Cancel" value="Anuluj" style="margin: 15px auto; width: 35%; "></h:commandButton>
<h:messages for="register"/>
</h:form>
</div>
</div>
</h:body>
和addClients
<h:body>
<h1>Add clients to registered user</h1>
<p>
#{ null != facesContext.application.flowHandler.currentFlow}
#{facesContext.application.flowHandler.currentFlow.id}
</p>
<h:form id="clientsForm">
<c:forEach items="#{clientManagement.clientsList}" var="client">
<h:outputText value="Client Name"/>
<h:inputText id="clientName" value="#{client.name}"></h:inputText>
<h:outputText value="Client company name" />
<h:inputText id="clientCompany" value="#{client.companyName}"></h:inputText>
<h:outputText value="Website" />
<h:inputText id="clientWebsit" value="#{client.website}"></h:inputText>
<h:outputText value="Client Email"/>
<h:inputText id="clientEmail" value="#{client.clientEmail}"></h:inputText> <br></br><br></br>
</c:forEach>
<h:commandButton value="Add client" action="#{clientManagement.addClients()}" immediate="true">
<f:ajax execute="@form" render="clientsForm" />
</h:commandButton>
<h:commandButton id="saveClients" value="Save clients" action="#{clientManagement.saveClient}">
</h:commandButton><br></br>
<h:messages for="saveClients"/>
</h:form>
</h:body>
最后一个文件 -ClientManagement.java
验证通过的电子邮件的文件,为客户创建新表单并保存从addClients.xhtml
数据库传递的客户列表。这是我不知道为什么它不起作用的地方。在方法saveClient
中,如果保存了所有内容,我想将用户重定向到路径../protected/mainPage.xhtml
,但它不起作用。
@Named
@ViewScoped
public class ClientManagement implements Serializable {
private List<Client> clientsList = new ArrayList<Client>();
private List<String> clientsEmail;
private List<Client> allClients;
private User loggedUser;
@Inject
private ClientFacade clientDAO;
@PostConstruct
public void init() {
HttpSession session = SessionUtil.getSession();
User user = (User) session.getAttribute("user");
loggedUser = user;
System.out.println(loggedUser);
}
public Boolean validateEmail(String email) {
String regex = "^[\\w-_\\.+]*[\\w-_\\.]\\@([\\w]+\\.)+[\\w]+[\\w]$";
return email.matches(regex);
}
public void delete(Client client) {
clientDAO.remove(client);
}
public void addClients() {
clientsList.add(new Client());
}
public String saveClient() {
try {
clientsList.forEach((client) -> {
boolean validateEmail = validateEmail(client.getClientEmail());
if (validateEmail) {
Client newClient = new Client();
newClient.setName(client.getName());
newClient.setCompanyName(client.getCompanyName());
newClient.setWebsite(client.getWebsite());
newClient.setClientEmail(client.getClientEmail());
newClient.setUserid(loggedUser);
try {
clientDAO.add(newClient);
} catch (Exception e) {
throw new Error(e);
}
} else if (validateEmail == false) {
FacesContext ctx = FacesContext.getCurrentInstance();
ctx.addMessage("clientsForm:saveClients", new FacesMessage("Provided email is invalid." + client.getClientEmail()));
}
});
} catch (Error e) {
throw new Error(e);
}
FacesContext ctx = FacesContext.getCurrentInstance();
ctx.addMessage( "clientsForm:saveClients", new FacesMessage("Successfully added clients"));
return "mainPage";
}
// getters and setters
}
我也尝试过排除它registrationFlow.xml
并在saveClient
方法返回页面中,但效果不佳。