Pages

Wednesday, December 17, 2014

JSF:un simple exemple d'authentification


On va présenter un exemple de JSF.

Tout d'abord on doit créer la classe  BeanUser 


public class BeanUser {
String username;
String pwd;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}

public String login(){
if(username.equals("test") && pwd.equals("test"))
return "yes";
else

return "no";

}

}
Cette classe doit etre déclaré dans le fichier faces-config.xml  pour etre applelé de la page index.xhtml

<managed-bean>
<managed-bean-name>beanUser</managed-bean-name>
<managed-bean-class>training.jsf.BeanUser</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>



Voici la page index.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="f"  uri="http://java.sun.com/jsf/core"%>
<%@ taglib prefix="h"  uri="http://java.sun.com/jsf/html"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>demo jsf</title>
</head>
<body>
<f:view>
<h:form>
<h:messages style="color:red"/>
<h:panelGrid columns="2" bgcolor="cyan">
<h:outputText value="Username" />
<h:inputText value="#{beanUser.username }" required="true" requiredMessage="Username is required" />
<h:outputText value="Password" />
<h:inputSecret value="#{beanUser.pwd}" required="true" requiredMessage="Password is required"/>
<h:commandButton action="#{beanUser.login }" value="Ok" />
</h:panelGrid>
</h:form>
</f:view>
</body>
</html>

Maintenant , on va ajouter la page accueil.jsp suivante:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="f"  uri="http://java.sun.com/jsf/core"%>
<%@ taglib prefix="h"  uri="http://java.sun.com/jsf/html"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<f:view>
Bienvenue <h:outputText value="#{beanUser.username}" />
</f:view>
</body>
</html>

et dans le fichier  faces-config.xml  ,on doit ajouter les regles de navigation comme suit:

<?xml version="1.0" encoding="UTF-8"?>
<faces-config version="2.0" xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xi="http://www.w3.org/2001/XInclude"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd">
 <managed-bean>
  <managed-bean-name>beanUser</managed-bean-name>
  <managed-bean-class>training.jsf.BeanUser</managed-bean-class>
  <managed-bean-scope>session</managed-bean-scope>
 </managed-bean>
 <navigation-rule>
  <display-name>index</display-name>
  <from-view-id>/index.jsp</from-view-id>
  <navigation-case>
   <from-outcome>yes</from-outcome>
   <to-view-id>/accueil.jsp</to-view-id>
  </navigation-case>
 </navigation-rule>
</faces-config>

Le vidéo suivant peut vous aider à mieux compendre ceci.

No comments:

Post a Comment