JSP 表单和 Struts 1

发布于 2024-11-01 08:48:23 字数 793 浏览 0 评论 0原文

我制作了一个JSP表单。它包含一个简单的 MCQ 列表,其中包含每个问题的每个选项的单选按钮。

如何制作一个 struts 1 表单来检索用户选择的答案并将它们与类中定义的正确答案进行比较。

如果用户的选项正确,它应该显示用户的答案并打印“正确”。

更新 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>

我已成功为一个问题创建了一个简单的 MCQ。

现在我将如何实现一个数组。我将如何映射第二个单选按钮并将它们与包含每个问题的正确答案的 Answers[] Array 的第二个索引进行比较。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

赏烟花じ飞满天 2024-11-08 08:48:23

有两种方法可以做到这一点:

要么您有一个包含问题的所有指定属性的操作表单,例如

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;

}

,或者有一个包含数组(在本例中为字符串)的类。这比较棘手,因为您必须以某种方式将数组映射到 QuestionId。

示例:

public class MultipleChoiceForm extends ActionForm {

    private String[] answers;

    //Getters and Setters here.

}

然后您的 JSP 将是这种类型的(针对您需要的所有答案字段重复多次):

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

我希望这会有所帮助!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文