Fill In the Gap [Q.1] Answer the following questions We want to write a simple a
ID: 3577539 • Letter: F
Question
Fill In the Gap
[Q.1] Answer the following questions
We want to write a simple application for creating student data and retrieve them based on JSF framework. Student data are stored in HashMap.
lookupStudentData.xhtml //renders a user interface to query a student record
There are three valid student IDs: 001, 002, 003 (in String type).
When user clicks “Get Student Data button, student data is retrieved from the HashMap and shown to the user by showStudentData.xhtml as follows.
If studentID is invalid unknownStudent.xhtml will display the following page.
For implementing this application, we need StudentLookupAPI.java and StudentData.java to store student data.
StudentLookupAPI.java
public interface StudentLookupAPI {
public Student findStudent(String id);
}
StudentData.java
import java.util.HashMap;
import java.util.Map;
public class StudentData
implements StudentLookupAPI {
private Map<String,Student> students;
public StudentData() {
students = new HashMap<String,Student>();
addStudent(new Student("001", "Bob Feiner", 3.2));
addStudent(new Student("002", "Linda Brown", 3.5));
addStudent(new Student("003", "Bill Clinton", 2.9));
}
/** Finds the student with the given ID.
* Returns null if there is no match. */
public Student findStudent(String id) {
if (id != null) {
return students.get(id);
} else {
return null;
}
}
private void addStudent(Student student) {//helper method
students.put(student.getId(), student);
}
}
(1) Complete Student.java bean based on the following code skeleton (Note we do not need set methods). Fill out blank lines and box.
public class Student {
private String id, name;
private double GPA;
public Student(String id, String name, double GPA) {//constructor
}
public String ____________ {//accessor
return id;
}
public String _____________{//accessor
return name;
}
public double _____________{//accessor
return GPA;
}
}
StudentBean.java and StudentBean2.java are managed beans for searching student record from the HashMap.
(2) Complete StudentBean.java based on the following code skeleton. Fill out the blank lines
@Named
@SessionScoped
public class StudentBean implements Serializable {
protected String studentId;
protected Student student;
private static StudentLookupAPI lookupAPI =
new StudentData();
public String _________________ {//accessor
return studentId;
}
public void setStudentId(String studentId) {
this.studentId = studentId.trim();
if (this.studentId.isEmpty()) {
this.studentId = "none entered";
}
}
public Student _______________ {//accessor
return student;
}
public String getStudentData { //action control method
student = lookupAPI._______________(studentId); //get student object whose id is studentId
if (student == null) {
return ____________________
} else {
return _______________________
}
}
}
Studentbean2.java
@Named
@SessionScoped
public class StudentBean2 extends StudentBean implements Serializable {
@Override
public String getStudentData() {
String jsfPage = super.getStudentData();
return(jsfPage + "?faces-redirect=true"); //if faces-redirect=true, you can show the target
//xhtml on the URL window of the browser
}
}
(3) Complete studentLookup.xhtml based on the following code skeleton. Fill out blank lines.
<h:body>
<h1>Lookup Student Data</h1>
<h:form>
Student ID:
<h:inputText value="#{studentBean2._____________}"/><br/>
<h:commandButton value="Get Student Data"
action="#{studentBean2.______________}"/>
</h:form>
</h:body>
(4) Complete showStudentData.xhtml based on the following code skeleton. Fill out blank lines.
<h:body>
<h1>Selected Student Data</h1>
<ul>
<li>Student ID: ____________________________</li>
<li>Student Name: __________________________</li>
<li>Student GPA: ____________________________</li>
</ul>
</h:body>
(5) Complete unknowStudent.xhtml based on the following code skeleton. Fill out blank lines.
<h:body>
<h1>Unknown Student</h1>
<h2>No student found with id ____________________________</h2>
<p><h:link __________=_______________ value="Please try again"/></p>
</h:body>
The hyperlink redirects to studentLookup.xhtml.
Explanation / Answer
(1)
public class Student {
private String id, name;
private double GPA;
public Student(String id, String name, double GPA) {//constructor
}
public String getId() {//accessor
return id;
}
public String getName(){//accessor
return name;
}
public double getGPA(){//accessor
return GPA;
}
}
(2)
public class StudentBean implements Serializable {
protected String studentId;
protected Student student;
private static StudentLookupAPI lookupAPI =
new StudentData();
public String getStudentId() {//accessor
return studentId;
}
public void setStudentId(String studentId) {
this.studentId = studentId.trim();
if (this.studentId.isEmpty()) {
this.studentId = "none entered";
}
}
public Student getStudent() {//accessor
return student;
}
public String getStudentData { //action control method
student = lookupAPI.findStudent(studentId); //get student object whose id is studentId
if (student == null) {
return "No Student data available";
} else {
return student.getId() + " " + student.getName() + " " + student.getGPA();
}
}
}
(3)
<h:body>
<h1>Lookup Student Data</h1>
<h:form>
Student ID:
<h:inputText value="#{studentBean2.getStudentId()}"/><br/>
<h:commandButton value="Get Student Data"
action="#{studentBean2.getStudentData()}"/>
</h:form>
</h:body>
(4)
<h:body>
<h1>Selected Student Data</h1>
<ul>
<li>Student ID:<h:outputText value="#{studentBean2.getStudent().getId()}"/></li>
<li>Student Name: </li>
<li>Student GPA: <h:outputText value="#{studentBean2.getStudent().getGPA()}"/></li>
</ul>
</h:body>
(5)
<h:body>
<h1>Unknown Student</h1>
<h2>No student found with id <h:outputText value="#{studentBean2.getStudent().getId()}"/></h2>
<p><h:link outcome="studentLookup.xhtml" value="Please try again"/></p>
</h:body>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.