Checked radion button based on model variable + Spring forms + JSP - spring

I have model called cuntry.java with variable lang .I want to check the radio button option in JSP based on the lang. It could be 'EN' or 'FR'. when i wrote below code, throwing error in JSP page.
Code:
<form:radiobutton path="lang" id="language" value="FR" name="radios" <c:if ${cuntry.lang == 'FR' ? 'checked="checked"' : '' } /> ></form:radiobutton>
Error : Unterminated <form:radiobutton tag

There are multiple errors in the form tag. When using Spring tags, you may not specify id and name attributes. They will be generated automatically from path variable. Tag <c:if ... /> inside another tag <form:radiobutton ... /> is not allowed. Remove end tag as well. This is the right way
<form:radiobutton path="lang" value="FR" checked="${cuntry.lang == 'FR' ? 'checked' : '' }" />
If the value of cuntry.lang is FR, the Spring tag will result the following HTML (after compiling)
<input id="lang" name="lang" checked="checked" type="radio" value="FR" />
otherwise
<input id="lang" name="lang" type="radio" value="FR" />
If you absolutely need attributes id = language and name = radios, you can not use Spring tag .
You will find a good example here.

<c:if> tag works on tags rather than attributes. As a result, your html syntax becomes invalid.
I haven't done jsp for quite a while. this should more or less work:
<form:radiobutton checked="${cuntry.lang == 'FR' ? 'checked':''}" path="lang" id="language" value="FR" name="radios">
</form:radiobutton>
If you insist on using <c:if> tag for this, you can do this:
<c:if test="${cuntry.lang == 'FR'}">
<form:radiobutton checked="checked" path="lang" id="language" value="FR" name="radios">
</form:radiobutton>
</c:if>
<c:if test="${cuntry.lang != 'FR'}">
<form:radiobutton path="lang" id="language" value="FR" name="radios">
</form:radiobutton>
</c:if>
You can also use <c:choose>,<c:when>,<c:otherwise> tags to solve this problem.
Obviously the first solution looks better.

Related

JSTL - Promotional Code

I'm trying to use JSTL, so that if a user enters a code, it is looked up in the database, if it equals the value, then the following discount is applied.
I kind of have an idea/pseudocode of what i want to write but i cant get it working or where i should go from here
In my purchase page i have:
form action="promo.jsp" method="GET">
Promotional Code: <input type="text" name="promo">
<input type = "submit" id="promo" value = "Apply Code" />
</form>
<form action="complete_purchase.jsp" method="POST">
<input type="submit" value="Complete Purchase" />
</form>
And in my "promo.jsp"
I have something a long the lines of:
<sql:query var="item">
select item_price from inventory;
</sql:query>
<sql:query var="pro">
select code,discount from promotion_code;
</sql:query>
<c:if test="${pro.promo eq 'spring01'}">
<c:set var="user" value="${(item_price-(item_price*discount))}" scope="session" />
</c:if>
<c:out value="${sessionScope.user}" />
So basically, when i click apply code, it stores the new value of the price in a variable, that i want to call on the "final purchase page"

Thymeleaf How to check current URL?

i was familiar with JSTL before and thymeleaf is something new to me.
What i want is to check if the current URL is the index page (/index), if so then make visible a div.
Here is a JSTL equivalent example
<c:set var="url" value="${ pageContext.request.requestURI }" />
<c:if test="${url=='/example/WEB-INF/views/inbox.jsp'}">
...
</c:if>
Try this:
<div th:if="${#httpServletRequest.requestURI == '/example/WEB-INF/views/inbox.jsp'}">
some content
</div>

How to give c:if tag in display tag

I have a display tag in my jsp file. Its like..
<display:table id="currentRow" name="${ListObj}" requestURI="" sort="page" defaultsort="2"
pagesize="5" class="displayTable">
<display:caption><font color="red">Users List</font></display:caption>
<display:column property="ID" title="Role" ></display:column>
<display:column property="Name" title="User Name" sortable="true"></display:column>
<c:if test="%{currentRow.ID ne '1'}">
<display:column >
<i>edit</i>
</display:column>
</c:if>
</display:table>
i had wrote the code <c:if test="%{currentRow.ID ne '1'}"> for that i didnt want to show the edit link for user with ID 1. But that condition is not working. Ie no rows in display tag shows edit link. But if i give <c:if test="%{currentRow.ID eq '1'}">, edit link will be displayed.
How can i make it displayed for all the rows except the one with ID=1???
You should put the if statement inside the <display:column> tag as you will always want the <td> tag rendered in the table even if it is empty.
<display:column >
<c:if test="%{currentRow.ID ne 1}">
<i>edit</i>
</c:if>
</display:column>
If the id attribute is an integer you will want to perform equals on it as an int.

String concatenation with ASP.NET MVC3 Razor

i'm trying to concatenate a string in asp.net mvc 3 razor and i'm getting a little sintax problem with my cshtml.
i what to generate an id for my checkboxes on a foreach statement, and my checkboxes should start with "chk" and what to cancatenate a fieldon the ID, something like that:
<input type="checkbox" id="chk+#obj.field" />
but or exampple the result for id attribute is: id="chk+8"
how can i just get a result for something like "chk8"?
Just put your variable next to prefix:
<input type="checkbox" id="chk#(obj.field)" />
Try
<input type="checkbox" id="#("chk" + obj.field)" />
or
<input type="checkbox" id="chk#obj.field" />
<input type="checkbox" id="chk#(obj.field)" /> should work.
The most direct and clean way to add a prefix a suffix.
#("PREFIX " + obj.field + " SUFFIX")
<input type="checkbox" id="chk#(obj.field)" /> should work.
Best way to concate any C# variable in rozer view by using string.Format
id="#string.Format("{0}_Title", _Id)" // Apend after
id="#string.Format("Title_{0}", _Id)" // Apend before
id="#string.Format("Title_{0}_Title", _Id)" // Apend Middle

How do I unhide a CFDIV that has dynamic content with AJAX binding itself?

I have the following CFSELECT tags that are used to populate a text input:
<cfselect id="this" name="this" bind="cfc:Data.getThis()" bindonload="true" />
<cfselect id="that" name="that" bind="cfc:Data.getThat({p1})" />
<cfselect id="theOther" name="theOther" bind="cfc:Data.getTheOther({p1}, {p2})" />
The text input is the only value that needs to be submitted in a form:
<cfform name="addItem" method="post" action="somepage.cfm">
<cfinput
type="text"
id="item"
name="item"
bind="cfc:Data.getResult({this}, {that}, {theOther})" /><br />
<cfinput
type="submit"
name="addButton"
value="Add Item" />
</cfform>
I want the form and it's contents to be visible only when all three selections have been made, and there is a value for the text input. What is the best way to do this? I'm assuming some use of CFDIV is the best way, but I'm not sure how to load the dynamic content (the CFINPUTs) this way.
<cfselect id="this" name="this" bind="cfc:Data.getThis()" bindonload="true" onChange="toggleForm();" />
<cfselect id="that" name="that" bind="cfc:Data.getThat({p1})" onChange="toggleForm();" />
<cfselect id="theOther" name="theOther" bind="cfc:Data.getTheOther({p1}, {p2})" onChange="toggleForm();" />
<div id="theForm" style="display:none">
<cfform name="addItem" method="post" action="somepage.cfm">
<cfinput
type="text"
id="item"
name="item"
bind="cfc:Data.getResult({this}, {that}, {theOther})" /><br />
<cfinput
type="submit"
name="addButton"
value="Add Item" />
</cfform>
</div>
<script type="text/javascript">
function toggleForm(){
var a = document.getElementById("this").selectedIndex;
var b = document.getElementById("that").selectedIndex;
var c = document.getElementById("theOther").selectedIndex;
if (a > -1 && b > -1 && c > -1){
document.getElementById("theForm").style.display = "";
}
}
</script>
Personally I would simplify that JS a bit by using jQuery, but I don't know if you're already using jQuery on your site, and I don't want to be another "use jquery" empty answer; so this should work without jQuery, should you want/need to go without it. (But jQuery is awesome!)

Resources