I'm trying to create a simple polling system in a JSP with a database backend. The basic table structure is +-----------+ | Id | |-----------| | Question | |-----------| | Answer1 | |-----------| | Answer2 | |-----------| | Answer3 | |-----------| | Answer4 | |-----------| | Answer5 | +-----------+
I manually specify the id for the question/answer set in question in the sql:query tag.
My question is how do I iterate through the 5 possible ansers for creating a bunch of radio buttons?
This is what I have right now but its not working.
Before anyone mentions it, yes I realize including the SQL tags is breaking with MVC patterns, but I'm just starting, and don't want to overcomplicate things.
[ July 21, 2006: Message edited by: Mike Spenser ] [ July 21, 2006: Message edited by: Mike Spenser ]
You didn't say what the symptoms of "not working" were but let me start by questioning your question (as well):
My question is how do I iterate through the 5 possible questions...
But your picture shows 5 possible answers. I suppose these 5 answers aren't in a separate table, you have a repeating group in your table and the column names are ANSWER1, ANSWER2, and so on? I don't know of any way to fiddle with the SQL tags to get to there, but what's wrong with
Joined: Jun 13, 2006
I did mean answers, I corrected the post. I was just trying to make the code a bit simplier, but that works too.
I have implemented online question/poll systems many times. Relational databases may be great for some things but they are a horrible way to store and access question data. (IM-not so-HO) Why? Because they are so inflexible - XML works well for flexible creation of questions. Bill
I have implemented online question/poll systems many times. Relational databases may be great for some things but they are a horrible way to store and access question data. (IM-not so-HO) Why? Because they are so inflexible - XML works well for flexible creation of questions.
You can also include the XML data in your database. You'd normally have to write a parser to deal with the XML, but for some time I've been working with Java's XML serialisation and it works really well and is very straightforward to start using... all you need to do is:
(1) Create a bean class (say PollBean) to store your poll data in. For example, containing the fields "question" (String), "answers" (String).
(2) Use java.beans.XMLEncoder to serialise a PollBean instance containing all the required data into XML. The instance can be obtained by completing an admin form or from some other console (or indeed written by hand once you see the format).
(3) Use java.beans.XMLDecoder to deserialise the object from the file/database when you're ready to read its contents (e.g. on each loading of the page). This will automatically extract all the data from your XML source into a new bean, which will then 'look like' the original bean.
Now you have a PollBean instance in the JSP, so you can easily use EL to manipulate your bean. For example, to write out all the answers just use the JSTL <c:forEach> on its "answers" property.
One final comment: you have incorrectly implemented this bit: This will not work because "count" here is a JSP scripting variable and not a scoped attribute. It is therefore not accessible from your EL code. You would therefore instead want to set the page-scoped attribute through the pageContext, or alternatively use <c:set> like:Better still, you needn't do any of that, you can simply use the "varStatus" attribute of <c:forEach>:Note that "status.count" is 1-based, so you don't need to initialise any variables, and you don't need to use scripting elements.
Charles Lyons (SCJP 1.4, April 2003; SCJP 5, Dec 2006; SCWCD 1.4b, April 2004)
Author of OCEJWCD Study Companion for Oracle Exam 1Z0-899 (ISBN 0955160340 / AmazonAmazon UK )