Я интегрирую JSF и Primefaces со Spring, я хочу обработать навигацию с JSF и ManagedBean в качестве контроллера, но по какой-то причине я не могу отправить запрос из представления JSF в контроллер ManagedBean, получаю ошибку: "метод 'POST' не разрешен" Полагаю, это потому, что в контексте Spring мне нужно указать @PostMapping, но мой контроллер - это ManagedBean, как я могу решить эту проблему.Заранее благодарим.
Просмотр кода:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title>Bholamundo-jsf-cdi</title>
</h:head>
<h:body>
<f:view locale="#{languageController.locale}" />
<ui:composition xmlns:ui="http://java.sun.com/jsf/facelets" template="./WEB-INF/plantillas/plantilla.xhtml">
<ui:define name="content">
<h:form>
<table>
<tr>
<td><p:outputLabel for="username" value="#{msgs['username']}:" /></td>
<td><p:inputText id="username" required="true" value="#{userLoginView.username}" /></td>
<td><p:message for="username" class="errorMessage" /></td>
</tr>
<tr>
<td><p:outputLabel for="password" value="#{msgs['password']}:" /></td>
<td><p:password id="password" required="true" value="#{userLoginView.password}" /></td>
<td><p:message for="password" class="errorMessage" /></td>
</tr>
<tr>
<td></td>
<td><h:commandButton id="logIn" action="#{userLoginView.login}" value="#{msgs['logIn']}"/></td>
</tr>
</table>
</h:form>
</ui:define>
</ui:composition>
</h:body>
</html>
Код ManagedBean:
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
@ManagedBean
@SessionScoped
public class UserLoginView {
private String username;
private String password;
public UserLoginView() {
}
@PostConstruct
public void init() {
this.username = "";
this.password = "";
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String login() {
System.out.println("beans.backing.UserLoginView.login()->username: " + username + ", password: " + password);
// FacesMessage message = null;
if (username != null && username.equals("admin") && password != null && password.equals("admin")) {
// message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Welcome", username);
// FacesContext.getCurrentInstance().addMessage(null, message);
System.out.println("log in success");
return "indexLogged";
} else {
System.out.println("ok es por aqui");
// message = new FacesMessage(FacesMessage.SEVERITY_WARN, "Loggin Error",
// "Invalid credentials");
System.out.println("mensaje instanciado correctamente");
// FacesContext.getCurrentInstance().addMessage(null, message);
System.out.println("Faces Context obtenido correctamente");
return "index";
}
// PrimeFaces.current().ajax().addCallbackParam("loggedIn", loggedIn);
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
version="3.1">
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.xhtml</welcome-file>
</welcome-file-list>
</web-app>
face-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<faces-config 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"
version="2.2">
<application>
<el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
<!--No se necesita agregar la extension del archivo, para configurar los
mensajes -->
<resource-bundle>
<base-name>mensajes</base-name>
<var>msgs</var>
</resource-bundle>
<!--cambio de textos de validadores -->
<message-bundle>errorMessages</message-bundle>
</application>
</faces-config>