jsf 2 - Equivalent of JSP scriptlet method call in JSF -
i need convert jsp file equivalent jsf file. jsp follows:
step 1: class import:
<%@ page import="org.keycloak.constants.serviceurlconstants" %> <%@ page import="org.keycloak.common.util.keycloakuribuilder" %> <%@ page session="false" %> <html>
step 2: define variable:
<% string logouturi = keycloakuribuilder.fromuri("/auth").path(serviceurlconstants.token_service_logout_path).queryparam("redirect_uri", "/customer-portal").build("demo").tostring(); %>
step 3: refers variable:
<a href="<%=logouturi%>">logout</a>
the imported library external library project. in jsf, know how step 3. don't know how import classes in step 1 , how define variable shown in step 2 in jsf.
is there equivalent way of performing step 1-3 in jsf? thank you.
you can't call methods directly in jsf or create variables, therefore don't need imports. way use el-expressions. since calling static methods not possible el, you'll have create bean, makes call of keycloakuribuilder.fromuri...
named bean call methods:
example:
import java.io.serializable; import javax.enterprise.context.requestscoped; import javax.inject.named; //this bean available default under name 'mybean', can change in @named annotation if desired @named @requestscoped public class mybean implements serializable { public string mymethod(string inupt){ return "hello " + input; } }
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:ui="http://xmlns.jcp.org/jsf/facelets" xmlns:f="http://java.sun.com/jsf/core" > <head></head> <body> <h:outputtext value ="#{mybean.mymethod('world')}"/> </body> </html>
will give html:
<!doctype html> <html xmlns="http://www.w3.org/1999/xhtml"> <head></head> <body>hello world </body> </html>
the preferred way show on page use getters , setters, if have field getter , setter
private string name; public string getname() { return name; } public void setname(string name) { this.name = name; }
you can use
#{mybean.name}
jsf call getter if needs value (for output) or call setter if there value set (from input-fields)
the <%@ page session="false" %>
neither needed nor possible. in jsf bean has scope, example requestscoped
should match <%@ page session="false" %>
- request scoped bean lives 1 request, after bean disposed. there many other scopes, e.g. @sessionscoped
(from javax.enterprise.context
) bean lives long session active.
as mentioned user, scopes exists in cdi-variant (package javax.enterprise.context
) , jsf-variant (package javax.faces.bean
). should use cdi-variant, since jsf-variant might deprecated (see here).
explanation of scopes see here.
Comments
Post a Comment