Because I am constantly busy working on something, I have never had time to actually put everything in words and pictures. But, since you got here, then you must have already seen some part of my work - and this is the way I’m talking. I'm 24, born in Romania, studied at UPG Romania in software development field. I started from 0, mostly with basic stuff, and I’m evolving every day to an expert. I'm focused on freelancing projects, from small websites, to really heavy stuff.

Thursday, June 30, 2011

Trivial login form in JSF 2.0


This tip is an example of how you can create a trivial login form in JSF 2.0 . The credentials are hardcoded as final variables – in real application, you need will compare the entered values agains a database.

. . .
@ManagedBean
@SessionScoped
public class User implements Serializable {

private static final long serialVersionUID = 1L;

private final String userName = "User";
private final String userPassword = "12345";
private String name;
private String password;

public String getName() {
return name;
}

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

public String getPassword() {
return password;
}

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

public String login() {
if((userName.equals(name))&&(userPassword.equals(password))){
return "welcome";
} else {
return "index";
}
}
}

The login page should look something as below:

. . .
<h:form>
User : <h:inputText value="#{user.name}" />
Password : <h:inputSecret value="#{user.password}" />
<h:commandButton action="#{user.login}" value="Submit" />
</h:form>
. . .

If the userName and userPassword corresponds with the name and password entered, you will be redirected to the welcome.xhtml page. Otherwise, you will remain on the login form page.

Wednesday, June 29, 2011

Declaring managed beans in JSF 2.0


In the next example we will see how to declare a managed bean in JSF 2.0. There are two ways to configure the managed bean :

1. Declaring a Managed Bean in faces-config.xml descriptor file as below:

<managed-bean>
<description>description of the managed bean</description>
<managed-bean-name>name of the managed bean</managed-bean-name>
<managed-bean-class>fully qualified class name</managed-bean-class>
<managed-bean-scope>scope of the bean</managed-bean-scope>
<managed-bean-property>
<property-name>name of the bean property</property-name>
<value>Default value of the property</value>
</managed-bean-property>
...
</managed-bean>

2. In JSF 2.0, you can annotated a Java class with @ManagedBean annotation to turn it into a Managed Bean:

@ManagedBean
public class MyBean ...

In addition, you can customize the bean name, by adding the name clause to the annotation:

@ManagedBean(name=”CustomName”)
public class MyBean ...