调用Bean方法而不显式调用

时间:2016-10-11 00:36:12

标签: jsf-2.2

我正在学习如何使用Shiro编写JSF应用程序,但遇到了我所做的登录页面的某些问题。在调试模式下,它似乎在我通过HTML命令按钮调用它之前调用bean方法。该方法也未在构造函数中调用。关于发生了什么,我有点遗憾。

Backing Bean

package bean;

import javax.faces.*;
import javax.faces.application.FacesMessage;
import javax.faces.bean.*;
import javax.faces.context.FacesContext;
import models.User;
import java.io.*;
import javax.annotation.*;
import org.apache.shiro.*;
import org.apache.shiro.authc.*;
import org.apache.shiro.web.*;
import org.apache.shiro.web.util.*;
import org.primefaces.context.RequestContext;
import javax.persistence.*;

@ManagedBean(name = "loginBacking")
@RequestScoped
public class Login {

private User user = new User();
private String username;
private String password;
private boolean remember;
@ManagedProperty("#{request.requestURL}")
private String url;
@PersistenceContext(unitName = "Builder")
private EntityManager em;

public Login()
{
    System.out.print("create bean");
}

// getters and setters

public String userlogin() throws IOException {

    try{
        System.out.print("trying");
        SecurityUtils.getSubject().login(new UsernamePasswordToken(username, password, remember));

        Query query = em.createNamedQuery("user.finduser");
        query.setParameter("username", username);
        user = (User) query.getSingleResult();
        return "/builder/builder.xhtml";
    }
    catch (AuthenticationException e) {
        FacesContext.getCurrentInstance().addMessage(username, 
                new FacesMessage(FacesMessage.SEVERITY_ERROR, "Bad username or password", null));
        e.printStackTrace(); // TODO: logger.
    }
    return "failed.xhtml";
}   

}

Login.xhtml

     <!DOCTYPE html PUBLIC "-//W3C//DTD XHMTL 1.0 Transicational//EN" "http//www.w3.org/TR/xhmtl1/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:ui="http://java.sun.com/jsf/facelets"
    xmlns:p="http://primefaces.org/ui">

 <h:head/>
 <h:body>
 <h2>Login</h2>
 <h:form>
         <p:panelGrid columns="3">
             <h:outputLabel for="username" value="Username:" />
             <h:inputText id="username" required="true" value="#{loginBacking.username}" />
             <h:message for="username" />

             <h:outputLabel for="password" value="Password" />
             <h:inputSecret id="password" required="true" value="#{loginBacking.password}"/>
             <h:message for="password" />

             <h:outputLabel for="rememberMe" value="Remember Me" />
             <h:selectBooleanCheckbox id="rememberMe" value="#{loginBacking.remember}"/>
             <h:commandButton value="Login">
                <f:param name="action" value="#{loginBacking.userlogin()}"/>
             </h:commandButton>

         </p:panelGrid>
 </h:form>
 </h:body>

 </html>

<小时/> 在Eclipse的调试模式下,控制台日志似乎显示正在调用userlogin方法。 broswer的构建甚至还没有呈现形式。一旦调试器到达Login bean的末尾,那么页面就会呈现。当我填写表格并点击命令按钮时,我被引导到&#34; login.jsp&#34;我的导航规则中没有定义(我没有)。

0 个答案:

没有答案