Java Reference
In-Depth Information
JSF page for the duration of the HTTP request. You can refer to their methods directly in the JSF. In
Listing 14‐5, you rewrite
ListUsersAction.class
to make it a backing bean.
LISTING 14‐5:
The ListUserAction class rewritten as a backing bean
package com.devchronicles.mvc.javaee;
import java.util.ArrayList;
import java.util.List;
import javax.enterprise.context.RequestScoped;
import javax.inject.Named;
@RequestScoped
@Named
public class ListUsersAction {
private List<String> userList = new ArrayList<>();
public List<String> getUserList() {
return userList;
}
public String execute() {
userList.add("John Lennon");
userList.add("Ringo Starr");
userList.add("Paul McCartney");
userList.add("George Harrison");
return "/WEB-INF/pages/listusers.xhtml";
}
}
Because all backing beans are annotated with at least
@Named
and
@RequestScope
, there is a
Stereotype
annotation you can use that gives it all the behavioral characteristics of a backing bean
when it's applied. This annotation is the
@Model
annotation.
Next, you need to create an
index.xhtml
i le. This replaces
home.jsp
and is called directly from the
browser. The purpose of this JSF is to call the
execute
method on
ListUsersAction
that prepares
the data for the view
listusers.xhtml
.
Listing 14‐6 shows how this method is provoked.
LISTING 14 ‐ 6:
The home page to the simple MVC example
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<html xmlns="
http://www.w3.org/1999/xhtml"
xmlns:h="
http://java.sun.com/jsf/html"
>
<h:head><title>Welcome</title></h:head>
<h:body>
continues