How do I embed java code in JSF pages?

I have:

In this JSF page, I have a login form

Inside the managebean, I have a logincheck function

public void loginCheck(){
 if(logincorrect){
  //set user session 
 }else{
  //set lockout count session ++
 }
}

What I want to do in the JSF page is when the lock count session = = 2 (which means that the user cannot log in correctly twice, and I need to display a re receive tag)

<td>
    <%
         if(FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("numberOfLogins") == 2){
         <p:captcha label="Captcha" requiredMessage="Oops,are you human?"/>
       }
     %>

Obviously, the <% label does not work Thanks for any help from Java / JSF experts

Solution

Scriptlets (those <%% > things similar to PHP) are part of JSP. Since JSF 2.0, it supports its successor facelets (XHTML) as deprecated Facelets no longer supports any alternative to scriptlets Using scriptlets in JSP will lead to badly designed and poor maintainable codebase in almost all cases Forget them Java code is a fully available Java class Just prepare the model (some java bean classes) in the controller (JSF supports bean classes) and use taglibs and EL (expression language, those #{} things) to access the model in the view

Your specific use case,

<%
     if(FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("numberOfLogins") == 2){
     <p:captcha label="Captcha" requiredMessage="Oops,are you human?"/>
   }
 %>

It can be solved in full JSF / El as follows:

<p:captcha label="Captcha" requiredMessage="Oops,are you human?" rendered="#{numberOfLogins == 2}" />

Incidentally, numberoflogins can better be a property of JSF @ sessionscope @ managedbean than a property manually placed in the session map

You can also see:

>Our JSF wiki page – contains links to some decent JSF tutorials

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>