开发者

JSP Form and Struts 1

I have made a JSP form. It contains a simple a list of MCQ's that contains radio buttons for each option for every question.

How do I make a struts 1 form 开发者_开发百科that retrieves the users selected answers and compare them to the right answers which are defined in the class.

It should display the users answers and print "correct" if the users option was the correct one.

UPDATE 1:

<html:form action="/Ans" >
         <h3>What is 2+2</h3>
         1<html:radio name="MCQForm" property="usrAnswers" value="1"  disabled="false"/><br>
         4<html:radio name="MCQForm" property="usrAnswers" value="4"  disabled="false"/><br>
         2<html:radio name="MCQForm" property="usrAnswers" value="2"  disabled="false"/><br>             

    <html:submit value="Submit" />
</html:form>

I have managed to create a simple MCQ for one question.

Now how will i Implement an array. How will I map the 2nd radio buttons and compare them with the 2nd index of Answers[] Array which contains the right answer for every question.


There are 2 ways of doing this:

Either you have an action form that has all specified attributes for your questions e.g.

public class MultipleChoiceForm extends ActionForm {

    private String answer1;
    private String answer2;
    private String answer3;

    //continue till end of questions, e.g. this assumes you have 10 questions
    private String answer10;

    //Generate Getters and Setters for your variables;

}

OR, have a class that has an array (of String, in this case). This is more tricky as you will have to map your array to a questionId somehow.

Example:

public class MultipleChoiceForm extends ActionForm {

    private String[] answers;

    //Getters and Setters here.

}

and then your JSP will be something of this sort (repeated many times for all the answer fields you need):

<html:text name="multipleChoiceForm" property="answers">

I hope this helps!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜