1

I need to convert a JSP file to an equivalent JSF file. The JSP is as 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 a variable:

<%
        String logoutUri = KeycloakUriBuilder.fromUri("/auth").path(ServiceUrlConstants.TOKEN_SERVICE_LOGOUT_PATH).queryParam("redirect_uri", "/customer-portal").build("demo").toString();  %>

Step 3: Then refers to this variable:

<a href="<%=logoutUri%>">logout</a>

The imported library is an external library into the project. In JSF, I know how to do Step 3. But I don't know how to import the classes in Step 1 and how to define a variable as shown in Step 2 in JSF.

Is there an equivalent way of performing Step 1-3 in JSF? Thank you.

BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
user697911
  • 8,475
  • 15
  • 71
  • 139

1 Answers1

2

You can't call Methods directly in JSF or create variables, therefore you don't need imports. The only way is to use EL-Expressions. Since calling static Methods is not possible with EL, you'll have to create yourself a Bean, that makes the call of KeycloakUriBuilder.fromUri... With a Named Bean you call its Methods:

Example:

import java.io.Serializable;
import javax.enterprise.context.RequestScoped;
import javax.inject.Named;
//This Bean is available by default under the Name 'myBean', you can change it in the @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 you this HTML:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head></head>
<body>Hello world
</body>
</html>

The preferred way to show Something on the page is to use getters and setters, if you have a field with getter and Setter

private String name;
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}

You can just use

#{myBean.name}

JSF will call the getter if it needs the value (for output) or call the Setter if there is a value to set (from input-fields)

The <%@ page session="false" %> is also neither needed nor possible. In JSF the Bean has a Scope, this example with RequestScoped should be a good match for <%@ page session="false" %> - a Request Scoped Bean lives only for one Request, after that the Bean is disposed. There are many other scopes, e.g. @SessionScoped (from javax.enterprise.context) for a Bean that lives as long as the Session is active.

As mentioned by another User, those Scopes exists in the CDI-Variant (Package javax.enterprise.context) and a JSF-variant (package javax.faces.bean). You should use the CDI-Variant, since the JSF-Variant might be deprecated soon (see here).

Explanation of the Scopes see here.

Community
  • 1
  • 1
hinneLinks
  • 3,293
  • 20
  • 36