JSF Controller不支持spring

时间:2018-05-22 20:47:26

标签: spring jsf primefaces

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

faces-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>

0 个答案:

没有答案