seam用户验证时的注出

由于建立的验证组件生命周期是@Stateless,若用户验证成功,要将用户注出为SESSION生命周期。

登陆页面:

<h:form id="login">
     <fieldset>
 <div>
      <h:outputLabel id="UsernameLabel" for="username">Login Name</h:outputLabel>
      <h:inputText id="username" value="#{identity.username}" style="width: 175px;"/>
      <div class="errors"><h:message id="UsernameMessage" for="username"  style= "height:28px"/></div>
 </div>
 <div>
      <h:outputLabel id="PasswordLabel" for="password">Password</h:outputLabel>
               <h:inputSecret id="password" value="#{identity.password}" style="width: 175px;"/>
 </div>
 <div class="errors"><h:messages id="messages" globalOnly="true"  errorStyle= "width:193px;height:28px;"/></div>
 <div>
      <h:outputLabel for="rememberMe" value="Remember me"/>
               <h:selectBooleanCheckbox id="rememberMe" value="#{rememberMe.enabled}"/>
          </div>
 <div class="buttonBox"><h:commandButton id="login" action="#{identity.login}" value="Account Login"/></div>
 <div class="notes"><s:link id="register" view="/register.xhtml" value="Register New User"/></div>
          
     </fieldset>
</h:form>

经典代码:AuthenticatorAction.java

import static org.jboss.seam.ScopeType.SESSION;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Out;
import org.jboss.seam.faces.FacesMessages;

@Stateless
@Name("authenticator")
public class AuthenticatorAction 
    implements Authenticator
{
    @PersistenceContext 
    private EntityManager em;

    @In(required=false)   
    @Out(required=false,scope = SESSION)
    private User user;
   
    public boolean authenticate()
    {
    	
	List results = em.createQuery("select u from User u where u.username=#{identity.username} and u.password=#{identity.password}")
                         .getResultList();

	if (results.size()==0)
	{
	    return false;
	} else {
	    user = (User) results.get(0);
	    return true;
	}
    }
    
}


                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值