aspose file tools*
The moose likes JSF and the fly likes JSF 2.0 Login Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "JSF 2.0 Login" Watch "JSF 2.0 Login" New topic
Author

JSF 2.0 Login

Thomas Albrecht
Greenhorn

Joined: Aug 31, 2010
Posts: 2
Hallo,

I've got the following scenario:
Trough templating (basic.xhtml und header.xhtml):

[code]

basic.xhtml:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head>
<title>Function Box</title>
<h:outputStylesheet name="skin.css" library="theme" />
</h:head>

<h:body>

<div id="header">
<ui:insert name="header">
<ui:include src="/WEB-INF/template/Header.xhtml"/>
</ui:insert>
</div>


<div id="content">
<ui:insert name="content">
</ui:insert>
</div>

<div id="footer">
<ui:insert name="footer">
<ui:include src="/WEB-INF/template/Footer.xhtml"/>
</ui:insert>
</div>

</h:body>

</html>

[/code]


[code]header.xhtml:

<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.prime.com.tr/ui">

<h:head>
</h:head>
<h:body>
<h:form>
<h:panelGrid columns="4" style="padding:4px; margin: 0 auto;">

<h:graphicImage library="images" name="pic.png"
style="width: 80%; height:80% "></h:graphicImage>
<h:inputText value="#{funcBean.darst}" "></h:inputText>
<p:commandButton value="Run" action="run" />
<h:outputLink value="login.jsf"><h:outputText value="login" /></h:outputLink>


</h:panelGrid>
</h:form>

</h:body>
</html>[/code]

which is integrated on the Startpage (index.xhtml):


[code]index.xhtml:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">

<ui:composition template="/WEB-INF/template/basic.xhtml">

<ui:define name="content">

</ui:define>
</ui:composition>
</html>[/code]

In this scenario I want that a user who is visiting the startpage (index.xhtml) has only limited scope of actions.
Chose the user now the <h:outputLink> to get to the loginpage (login.xhtml) he will redirected to that page:
[code]login.xhtml:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.prime.com.tr/ui">

<h:head>
<h:outputStylesheet name="skin.css" library="theme" />
<title>Login</title>
</h:head>
<h:body>
<h:form>

<p:panel id="panel" header="Login" style="margin-bottom:10px;">
<p:messages />
<h:panelGrid columns="3">
<h:outputLabel for="username" value="Username: " />
<h:inputText id="username" value="#{loginBean.username}" required="true" label="Username" />
<p:message for="username" />

<h:outputLabel for="password" value="Password: " />
<h:inputSecret id="password" value="#{loginBean.password}" required="true" label="Password"/>
<p:message for="password" />
</h:panelGrid>


<p:commandButton ajax="false" value="Login" actionListener="#{loginBean.CheckValidUser}" />
<p:commandButton immediate="true" ajax="false" id="index" value="Cancel" action="/index.xhtml"/>
</p:panel>

</h:form>
</h:body>
</html>[/code]


On that page are 2 commandButtons. The first Button (<p:commandButton ... value="Login">) is used to log in and calls a Bean. The second Button (<p:commandButton ... value="Cancel" >) redirect the use back to the startpage (index.xhtml).

The problem is now, how I could redirect the user logged-in using the Bean to the index.xhtml and show it (e.g. instead of "login" in the startpage should now an "logout" link appear.

Here my first try to code the Bean:
[code]
[code]import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.validation.constraints.*;

@ManagedBean
@SessionScoped
public class loginBean {

@Size(min=2, max=30) @Pattern(regexp = "[\\s\\w-,!\\.]*", message = "must match a-z,A-Z,0-9 or !,-_.")
private String username;
@Size(min=2, max=20) @Pattern(regexp = "[\\s\\w-,!\\.]*", message = "must match a-z,A-Z,0-9 or !,-_.")
private String password;
private String message;


public String getMessage() {
return message;
}


public void setMessage(String message) {
this.message = message;
}


public String getUsername ()
{
return username;
}


public void setUsername (final String username)
{
this.username = username;
}


public String getPassword ()
{
return password;
}


public void setPassword (final String password)
{
this.password = password;
}

public String CheckValidUser() throws ValidatorException {
if(username.equals("admin") && password.equals("admin")){
return "/start.xhtml";
}
else{
String msgText = "Wrong username or password";
FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, msgText, null);
throw new ValidatorException(msg);
}
}




}
[/code]


The goal should be, if a wrong password- or user-input was executed, the user gets an Errormessage on the loginpage (login.xhtml).
For a successful login, the user should be redirected to the startpage (index.xhtml). Now there should some new features be visible, since he logout.
At the moment there isn'nt any feature for an logged-in user, cause I haven't any idea how to do it :confused:


Since I am new to the area J2EE and JSF, I'm not quite sure how to implement it (the best way) :-)
Or rather how can I make sure, that after I logged-in successful new components are visible on the startapge and how it have to look in the bean?


Thanks everybody for your support :-) and sry for my bad english :rolleyes:

P.S. it seems that the formatting doesn't work or I use it the wrong way :rolleyes:


greetz thomas
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JSF 2.0 Login