我如何在jsf页面中嵌入Java代码? [英] How do i embed java code inside jsf page?

查看:246
本文介绍了我如何在jsf页面中嵌入Java代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有: 称为"LoginBean"的托管Bean. 一个名为"login.xhtml"的JSF页面

I have: A managed bean called "LoginBean". A JSF page called "login.xhtml"

在这个jsf页面中,我有一个登录表单.

In this jsf page, i have a login form.

在managebean中,我有一个loginCheck功能.

Inside the managebean i have a loginCheck function.

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

我要在jsf页面中执行的操作是,当锁定计数会话== 2时(意味着用户无法正确登录2次,我需要显示一个Recaptcha标记.

What i want to do in my jsf page is that when the lock out count session == 2 (means users failed to login correctly 2 times, i need a recaptcha tag to be displayed.

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

显然,<%标签不起作用.感谢Java/jsf专家的任何帮助.

Apparently, the <% tag does not work. Appreciate any help from java/jsf experts.

推荐答案

小脚本(类似于PHP的<% %>内容)是JSP的一部分,它是设计不良且可维护性差的代码库.忘了他们吧. Java代码属于完全有价值的Java类.只需在控制器(JSF支持bean类)中准备模型(某些Javabean类),然后使用taglibs和EL(表达语言,那些#{}东西)在视图中访问模型.

Scriptlets (those PHP-like <% %> things) are part of JSP which is deprecated since JSF 2.0 in favor of its successor Facelets (XHTML). Facelets doesn't support any alternative to scriptlets anymore. Using scriptlets in JSP has in almost all cases lead to badly designed and poorly maintainable codebase. Forget about them. Java code belongs in fullworthy Java classes. Just prepare the model (some Javabean class) in the controller (a JSF backing bean class) and use taglibs and EL (Expression Language, those #{} things) to access the model in the view.

您的具体用例,

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

可以在完全有价值的JSF/EL中解决,如下所示:

can be solved in fullworthy JSF/EL as follows:

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

与将某些属性手动放置在会话映射中相比,可以将numberOfLogins更好地成为JSF @SessionScoped @ManagedBean的属性.

That numberOfLogins can by the way much better be made a property of a JSF @SessionScoped @ManagedBean than some attribute being manually placed in the session map.

  • Our JSF wiki page - contains some links to decent JSF tutorials as well

这篇关于我如何在jsf页面中嵌入Java代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆