首页 > 解决方案 > JSF 控制器在 spring 中不支持 post 方法

问题描述

我正在将 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>

托管豆代码:

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>

面孔-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>

标签: springjsfprimefaces

解决方案


推荐阅读