Reputation: 1
I am trying to do a Examination Questionnaires. I'm retrieving the questions and choices from the database. I am using h:inputText
inside the ui:repeat
. But when I was is trying to get the answer it only duplicate the last answer. I am just new in using JSF, I hope someone can help me. Thank you!
Here is the JSF:
<h:body>
<h:outputLabel>Examination</h:outputLabel>
<h:form>
<ui:repeat var="item" value="#{bean.q}" varStatus="status">
<tr>
<td>
<br/>
#{status.index+1}.
#{item.question}<br/>
A. #{item.ans_1}<br/>
B. #{item.ans_2}<br/>
C. #{item.ans_3}<br/>
D. #{item.ans_4}<br/>
<h:outputLabel value="Answer:" />
<h:inputText value="#{bean.answer}" required="true">
<f:validateLength maximum="1"/>
<f:validateRegex pattern="(^[_A-D]$)" />
</h:inputText>
<br/>
</td>
</tr>
</ui:repeat>
<br/>
<h:commandButton id="btn" value="Check" action="#{bean.check}"/>
</h:form>
</h:body>
Here is the bean:
public List<Return> getQ(){
DatabaseManager db = new DatabaseManager();
Connection conn = db.connection("jdbc:sqlserver://LAPTOP-","sa","1234567890");
List<Return> qList = new ArrayList<Return>();
try{
PreparedStatement ps = conn.prepareStatement("SELECT * FROM [JavaEE].[dbo].[tbl_Finals]");
ResultSet rs = ps.executeQuery();
while(rs.next()){
Return e=new Return();
e.setQuestion(rs.getString("question"));
e.setAns_1(rs.getString("a1"));
e.setAns_2(rs.getString("a2"));
e.setAns_3(rs.getString("a3"));
e.setAns_4(rs.getString("a4"));
qList.add(e);
}
conn.close();
}catch(Exception e){e.printStackTrace();
}
return qList;
}
public void check(){
DatabaseManager db = new DatabaseManager();
Connection conn = db.connection("jdbc:sqlserver://LAPTOP","sa","1234567890");
try{
PreparedStatement ps = conn.prepareStatement("SELECT * FROM [JavaEE].[dbo].[tbl_Finals]");
ResultSet rs = ps.executeQuery();
while(rs.next()){
i++;
System.out.println(getAnswer());
if(getAnswer().equals(rs.getString("ans"))){
c++;
System.out.println(i+ ". CORRECT");
}
else{
System.out.println(i+ ". WRONG");
}
}
conn.close();
}catch(Exception k){;
}
System.out.println("Your score is: " +c);
}
Upvotes: 0
Views: 102
Reputation: 6184
1)
Effectively you have multiple inputTexts
that point to the same field #{bean.answer}
. When submitting the form, each of your inputTexts calls bean.setAnswer(..)
and you end up having the value of the last inputText
in that field.
Add a field tou your Return
class:
public class Return {
...
private String userAnswerInput;
public void setUserAnswerInput(..){..};
public String getUserAnswerInput(){..};
}
and change your input to <h:inputText value="#{item.userAnswerInput}" ...>
This way you'll have answers from users filled into Return
instances corresponding to asked questions each.
2)
You'll have to modify you check()
method to loop through your List of Return
instances.
3)
You shouldn't build that List of Return
instances within your getter as this is invoked several times. Instead create the list in a separte public method annoted with @PostContruct
. The getter should be reduced to public List<Return> getQ(){return qList;}
.
Upvotes: 1