How to allow multiple select to form:radio buttons that are in loop - spring

Hei,
I ran into a problem with my survey application. I display all questions(6) using c:forEach loop and all of them are displayed on a single page. Using the same loop I also display all the textareas for answers. Everything worked great until I got a stupid idea to add radio buttons with values as an answer input for some questions. My code looks like this:
<c:forEach items="${questions}" var="q">
<p><c:out value="${q.questionText}"></c:out></p>
<c:choose>
<c:when test="${q.questionID !='2' && q.questionID !='6'}">
<form:checkboxes path="answerList" items="${options}"/>
</c:when>
<c:otherwise>
<form:textarea path="answerList" rows="5" cols="40" placeholder="Vasta tähän"></form:textarea>
</c:otherwise>
</c:choose>
</c:forEach>
As it is all in loop so multiplay radio button rows will be displayed (1 per question) And the problem is that I need to get 1 selected answer in each row. I tried to change form:radiobuttons to form:checkboxes and it worked, but then problem is that user can select multiple option per 1 row. Has anybody ran into the same problem and found an answer? Thanx in advance.

Yeah I got it to work. Needed to simply use checkboxes instead of radio buttons and also one tiny javascript that prevent multiple selection in answer row.

Related

how to add check box inside select tag

I am trying to select multiple values inside a drop down using check boxes, I confused how to attain without changing TLD file. ie each option for the select will have the check box
sample code which iI work is below
<form:select path="brandIds" id="brandDropdownSelectId" tabindex="31" multiple="true" size="4" >
<option value="0">Select Brand</option>
<form:options items="${brandList}" itemValue="brandId"
itemLabel="brandName" checkbox="true"/>
</form:select>
You can use external component like Twitter Bootstrap Multi-select
You can never use Checkbox inside a Select tag.
But, to solve this issue, we make use of CSS+Javascript to render the effect.
You define a empty select tag with a javascript function to alter the visibility of the checkboxes on click, and CSS plays the role of portraying the rendering in single effect.
This might help you:
Adding checkboxes inside a Select Tag

Option labels do not show up

As you can see in the code, i send "ttypes" to select-option menu. "ttypes" includes a list which has two elements and i want to show these elements as option but when i do that operation, jtsl creates two options but these options don't have any values. I cannot see anything.
I post a list as an attibute below.
model.addAttribute("ttypes", technicalTypeService.getTechnicalType());
And then i want to show that list in the select-option menu
but i cannot see the values in the select-option menu.
Can anyone help me?
<c:forEach items="${ttypes}" varStatus="i">
<c:set var="name" value="${ttypes[i.index].name}" />
<option value="${name}"</option>
</c:forEach>
Disclaimer: I'll for simplicitly assume that the invalid HTML syntax (a missing >) is just caused by carelessness during preparing the question.
You've definitely set the option values. Rightclick the page in your favourite webbrowser and choose View Source. Look closer, they are there in the generated HTML output.
You only forgot to set the option labels. Labels are visible to the enduser while values are indeed invisible to the enduser. You can set the option label by setting the body of the <option> element.
<c:forEach items="${ttypes}" varStatus="i">
<c:set var="name" value="${ttypes[i.index].name}" />
<option value="${name}">${name}</option>
</c:forEach>
A less clumsy way is:
<c:forEach items="${ttypes}" var="ttype">
<option value="${ttype.name}">${ttype.name}</option>
</c:forEach>
Noted should be that usually some technical ID is been used as option value.
<c:forEach items="${ttypes}" var="ttype">
<option value="${ttype.id}">${ttype.name}</option>
</c:forEach>
Also noted should be that when you omit the value, then the label becomes implicitly the option value.
<c:forEach items="${ttypes}" var="ttype">
<option>${ttype.name}</option>
</c:forEach>
Finally, noted should be that this problem has got nothing to do with JSP/JSTL/Spring. They are in the context of this question merely acting as a HTML code generator. Your concrete problem is just related to basic HTML.
See also:
HTML beginner tutorial - forms - select
try with something like that:
<c:forEach items="${ttypes}" var="ttype" varStatus="i">
<option value="${ttype.name}">my label ?</option>
</c:forEach>
replace the "my label?" with whatever field in ttype contains the label.

How can I use Spring MVC "form" tag instead of my "input" tags?

What I have:
I have a generic JSP page that is used throughout my application for displaying certain entities. The code that I am interested in goes like this:
<form:form modelAttribute="object"/>
<core:forEach items="${sections}" var="section" varStatus="itemStat">
<core:forEach items="${section.fields}" var="fieldDef">
<form:input path="${fieldDef.fieldName}"/>
</core:forEach>
</core:forEach>
<form:form>
For each section, and for each field in that section, I have an input having the path fieldName, which is what I want to display from each field.
What I want:
I would like instead of the input to be a simple text, like a label.
What I have tried:
I am most certain that I can do it somehow with <form:label> but I can't really make it work. Making a <form:label path="${fieldDef.fieldName}" /> just tells the browser for which field I need the label, but doesn't get the actual value from it.
I have also tried something like ${object.fieldDef.fieldName}, but in order for this to work I would have to first analyze the value of ${fieldDef.fieldName}, which would give me the name of the column, and then do a ${object.column}, but column being a variable I haven't been able to make this work in any way.
Alternative:
An alternative would be to just make the inputs as disabled and remove the border with CSS, but that would be a dirty way and from what I saw it is also tricky for IE different versions. I am sure that I can handle it directly.
I am a little intrigued by the fact that <form:input path="..."> puts into the input what it finds corresponding to that path (same goes for other form elements), but with label it works different.
So, what I want is basically simple, but I haven't managed to find a way. If someone could shed some light, that would be great. Thanks in advance !
You could look into the spring bind tag. I haven't tried using it before but this may work for you, in place of the input tag
<spring:bind path="fieldDef.fieldName">
${status.value}
</spring:bind>
reference: http://static.springsource.org/spring/docs/1.1.5/taglib/tag/BindTag.html
Instead of
<form:input path="${fieldDef.fieldName}"/>
use
<c:out value="${fieldDef.fieldName}"/>
It would display whatever value is there instead of creating a input field. Hope this helps you. Cheers.
Using the spring form tab, one option would be to use
<form:input disabled="true" path="${fieldDef.fieldName}"/>
To further make it not look like an input you could use CSS to style it to your preference.
Some css styles you could use:
background-color:#EEEEEE;border: 0px solid;
Update:
You could look into the spring bind tag. I haven't tried using it before but this may work for you, in place of the input tag
<spring:bind path="fieldDef.fieldName">
${status.value}
</spring:bind>

Table cell click event work with an f:ajax tag

I have been playing around with JSF and ui:repeat to create a simple dynamic table. My next step in teh process is to allow each cell in the table to be clickable/edited and started to tie in f:ajax around an h:outputlabel. This is where my dilemma begins because I would like the entire cell to be clickable, not just the contents/text of the cell and I haven't found a way to make the cell respond to an ajax click.
I have been doing a lot of searching but haven't found the direction that I need or a JSF expert to say "That's not possible".
So, my questions are:
is there a jsf component that can be used that can work with f:ajax tag and can also be formatted to fit a table cell?
is there a way to make the actual table cell click event work with an f:ajax tag or something similar.
As always thanks for the input and suggestions!
Regards,
Mike
There is no standard JSF component which generates a <td> with an onclick attribute.
Your best bet is to include a <h:commandLink> in the table cell and let it span the entire space of the table cell by setting its CSS display property to block.
<td>
<h:commandLink ... style="display:block;">
<f:ajax ... />
<h:outputLabel ... />
...
</h:commandLink>
</td>
(note that best practice is to specify CSS classes in a separate .css file and to use styleClass instead; the above is just examplary)
This gives the enduser the impression that the entire table cell is clickable.

Read-only (print) version of JSP form using Spring 2.0.x and form tags?

Is there a way to easily create a readonly version of JSP form in Spring?
i.e., I have a command object that's filled and if I show it as a form it works great, all the selects and radiobuttons get bound properly. However, my command object only holds id's of properties, not labels (i.e. and id from a select or a radiobutton list that gets bound on JSP load).
What I'd like to do is make a read only version where there'd be just a label - value list, without html objects such as inputs, selects and such.
So basically, in an edit version, there'd be something like
<form:select path="type.id" id="type">
<form:options items="${types}" itemLabel="name" itemValue="id"/>
</form:select>
but in the read only version I'd like to be able to automatically print only the exact type.name that got selected, i.e.
<c:out value="${commandName.type.name}"/>
Is there such a possibility, or do I have to mess with this in controller?
Ok so I guess there's no elegant way of matching IDs and values from model with IDs in command. Instead of doing extra work in controller, I matched the IDs on JSP, i.e.
<c:forEach var="type" items="${types}">
<c:if test="${type.id == commandName.type.id}">
<c:out value="${type.name}"/>
</c:if>
</c:forEach>
It's a bit extra work, but I'd rather do this than have a number of iterations over a List in my controller.

Resources