У меня проблемы с пониманием того, почему я не могу перейти от регистрационного потока к защищенному каталогу. В моем приложении в index.html
у меня есть кнопка, которая перенаправляет пользователя на регистрационную форму (которая является потоком) , Это та часть, где пользователь выбирает, хочет ли он войти или зарегистрировать нового пользователя. Моя структура папок:
- Веб-страницы
- index.x html
- защищенный каталог
- регистрационный каталог
- registration.x html
- addClients.x html
<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
метод возврата страницы, но он не сработал.