patternjavaMinor
Survey program using JSF and Ajax
Viewed 0 times
surveyajaxandprogramusingjsf
Problem
I'm new to AJAX and am trying to learn by myself with a small test:
It's a survey with 3 questions. I would like to know if I'm using AJAX the correct way, before starting bigger projects.
Here is the question.xhtml page:
The
Facelet Title
Survey is done ! Thank you
Question page
Question page
Regular HTML Form
Over: false
What is your age?PrimeFaces.cw('InputText','widget_survey_answer',{id:'survey:answer'});
PrimeFace Form
Over: false
What is your age?PrimeFaces.cw('InputText','widget_pfsurvey_answer',{id:'pfsurvey:answer'});
NextPrimeFaces.cw('CommandButton','widget_pfsurvey_submit',{id:'pfsurvey:submit'});
Question page
Question page
Regular HTML Form
Over: f
It's a survey with 3 questions. I would like to know if I'm using AJAX the correct way, before starting bigger projects.
Here is the question.xhtml page:
Question page
Question page
Regular HTML Form
Over:
PrimeFace Form
Over:
The
GeneralController bean is:import javax.inject.Named;
import javax.inject.Inject;
import javax.enterprise.context.ApplicationScoped;
@Named(value = "generalController")
@ApplicationScoped
public class GeneralController {
String[] questions = {"What is your age?","Where do you come from?","Are you married?"};
String answer;
String question;
int currentQuestion = 0;
boolean over = false;
boolean last = false;
public String getAnswer() {
System.out.println("Get Answer");
return answer;
}
public void setAnswer(String answer) {
System.out.println("Set Answer");
if(currentQuestion
When the survey is done, it's redirected to the end.xhtml page:
Facelet Title
Survey is done ! Thank you
Here is the generated HTML:
Question 1:
Question page
Question page
Regular HTML Form
Over: false
What is your age?PrimeFaces.cw('InputText','widget_survey_answer',{id:'survey:answer'});
PrimeFace Form
Over: false
What is your age?PrimeFaces.cw('InputText','widget_pfsurvey_answer',{id:'pfsurvey:answer'});
NextPrimeFaces.cw('CommandButton','widget_pfsurvey_submit',{id:'pfsurvey:submit'});
Question 3:
Question page
Question page
Regular HTML Form
Over: f
Solution
-
I think your bean should be
-
Instead of an array I'd use a
When to use a List over an Array in Java?
-
You could format the code better:
According to the Code Conventions for the Java Programming Language
-
The fields could be private instead of the current package private.
The rule of thumb is simple: make each class or member as inaccessible as
possible. In other words, use the lowest possible access level consistent with the
proper functioning of the software that you are writing.
Source: Effective Java 2nd Edition, Item 13: Minimize the accessibility of classes and members, p68.
@ApplicationScoped
public class GeneralController {
...
}I think your bean should be
@SessionScoped instead of @ApplicationScoped. Otherwise all of you clients will use the same controller instance.-
Instead of an array I'd use a
List. It's easier to work with.String[] questions = {"What is your age?","Where do you come from?","Are you married?"};When to use a List over an Array in Java?
-
You could format the code better:
if(!over)
return questions[currentQuestion];According to the Code Conventions for the Java Programming Language
if statements always should use braces. Omitting them is error-prone, and hard to read if you don't indent the conditional statement like above.-
The fields could be private instead of the current package private.
The rule of thumb is simple: make each class or member as inaccessible as
possible. In other words, use the lowest possible access level consistent with the
proper functioning of the software that you are writing.
Source: Effective Java 2nd Edition, Item 13: Minimize the accessibility of classes and members, p68.
Code Snippets
@ApplicationScoped
public class GeneralController {
...
}String[] questions = {"What is your age?","Where do you come from?","Are you married?"};if(!over)
return questions[currentQuestion];Context
StackExchange Code Review Q#9916, answer score: 6
Revisions (0)
No revisions yet.