Spring <form:select initial value - spring-mvc

Is there a way of setting a initial value (One of x in the list) for a
<form:select drop down similar to the text area default value?
i.e.
<td valign="top">
<form:select id="${appNameFormId}" path="metadata.appName" items="${dbList}"
disabled="true" itemValue="appName" itemLabel="appName">
</form:select>
</td>
<td valign="top">
<TEXTAREA id="${nameFormId}" name=jobName ROWS="1" COLS="25" >${job.jobName}
</TEXTAREA>
</td>

Because SELECT value comes from command object during rendering and go back to command object after submission I suggest to assign default value to metadata.appName inside Controller (manually or by using method with #ModelAttribute annotation: http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-ann-modelattrib).

Related

How to display properties of specific object selected via dropdown?

I wrote a query in which I get the semester class, display its values ​​as a name and get its id. Accordingly, when I select the semester I need in the drop-down list, I am given the semester that I chose. But I also need to display its duration in another field, I can do this if I create a ForEach loop and use it to extract this value, but it displays absolutely all the available values, and not the one I need. Tell me how to do it right? For display I use Spring MVC. Now the page looks like this:
I am also attaching the jsp content:
<form:form action="allSemestrsSem" method="get" id="selectSemestr" modelAttribute="semestr">
<tr>
<td style="font-size: large;">Select semester</td>
<td style="padding-left: 50px">
<select name="selectSemestr">
<c:forEach items="${semestr}" var="semestr">
<option value="${semestr.id}">${semestr.name}</option>
</c:forEach>
</select>
</td>
<td style="padding-left: 20px">
<security:authorize access="hasRole('ADMIN')">
<input type="submit" id="button" value="select" /></td>
</security:authorize>
</tr>
<tr height="80px" style="font-size: large; font-weight: bold;">
<td colspan="3">Semester duration:
<c:forEach items="${semestr}" var="semestr"> ${semestr.duration} </c:forEach>
</td>
</tr>
</form:form>
And just in case my controller is:
#RequestMapping(value = "/allSemestrsSem", method = RequestMethod.GET)
public String allSemestrs(#RequestParam(name = "selectSemestr") String selectSemestr, Model model) {
List<Semestr> semestr = service.getSemestr();
model.addAttribute("semestr", semestr);
List<Discipline> allDisciplines = service.getDisciplineSemestrId(Integer.valueOf(selectSemestr));
model.addAttribute("allDisc", allDisciplines);
return "semestr";
}

Using a datalist in a form in Spring-MVC

I'm attempting to convert an input form from a to a but does not accept the list attribute, does anyone know a workaround? This is my first time using Spring and I'm at a loss here.
<tr>
<th><form:label path="livrable.composant"><s:message code="composant"/></form:label></th>
<td>
<form:input path="livrables.composants" list="composants">
<c:if test = "${not empty composants}">
<datalist id="composants">
<c:forEach var="composantValue" items="${composants}">
<option value="${composantValue.nom}"/>
</c:forEach>
</datalist>
</c:if>
<br/>
<form:errors path="livrable.composant" cssClass="error"/>
</td></tr>
Nevermind I'm an idiot, I didn't close the tag it should be:
<form:input path="livrables.composants" list="composants"/>

How to a use a web user control more than one time on the same page?

I have created web user control for calendar. I.e it has a textbox and a calendar image. By clicking on calendar popup the calendar opens. Then by selecting any date it stores the year of that date. I use this code:
<script language="javaScript" type="text/javascript" src="Scripts/Calendar.js"> </script>
<link href="Styles/Calendar.css" rel="stylesheet" type="text/css" />
<table>
<tr>
<td>
<input type="text" name="datum1"/>
</td>
<td>
<img id="Img1" src="images/calFinal.jpg" alt="" runat="server" onclick="setYears(1947, 2040);
showCalender(this, 'datum1');" />
</td>
</tr>
</table>
<div>
<!-- Calender Script -->
<table id="calenderTable">
<tbody id="calenderTableHead">
<tr>
<td colspan="4" align="center">
<select onchange="showCalenderBody(createCalender(document.getElementById('selectYear').value,
this.selectedIndex, false));" id="selectMonth">
<option value="0">Jan</option>
<option value="1">Feb</option>
<option value="2">Mar</option>
<option value="3">Apr</option>
<option value="4">May</option>
<option value="5">Jun</option>
<option value="6">Jul</option>
<option value="7">Aug</option>
<option value="8">Sep</option>
<option value="9">Oct</option>
<option value="10">Nov</option>
<option value="11">Dec</option>
</select>
</td>
<td colspan="2" align="center">
<select onchange="showCalenderBody(createCalender(this.value,
document.getElementById('selectMonth').selectedIndex, false));" id="selectYear">
</select>
</td>
<td align="center">
<font color="#003333" size="+1">X</font>
</td>
</tr>
</tbody>
<tbody id="calenderTableDays">
<tr style="">
<td>Sun</td>
<td>Mon</td>
<td>Tue</td>
<td>Wed</td>
<td>Thu</td>
<td>Fri</td>
<td>Sat</td>
</tr>
</tbody>
<tbody id="calender">
</tbody>
</table>
<!-- End Calender Script -->
</div>
The problem occurs when I use the user control twice on the same web form. I have registered the control. When I use it once, it works proper. But I want it at two places. So, please give some solution on how to use the user control more than one time on the same web form. I have given different IDs for the user control, but it still does not work.
Code for user control:
<tr>
<td class="directorytdWidth directorylabel">
School Name:
</td>
<td class="directoryTdPadding">
<asp:TextBox ID="txtSchoolName" runat="server" Width="120px" ForeColor="#FF9F00"></asp:TextBox>
</td>
</tr>
<tr>
<td class="directorytdWidth directorylabel">
School Passout Year:
</td>
<td class="directoryTdPadding">
<uc1:calendar ID="schoolPassout" runat="server" />
</td>
</tr>
<tr>
<td class="directorytdWidth directorylabel">
College Name:
</td>
<td class="directoryTdPadding">
<asp:TextBox ID="txtCollegeName" runat="server" Width="120px" ForeColor="#FF9F00"></asp:TextBox>
</td>
</tr>
<tr>
<td class="directorytdWidth directorylabel">
College Passout Year:
</td>
<td class="directoryTdPadding">
<uc1:calendar ID="collegePassout" runat="server" />
</td>
</tr>
Use the Same User control but With diferent ID. For example (See code below)
Reg Your User control in you Web page
<%# Register Src="~/Controls/SearchAffiliated.ascx" TagPrefix="uc" TagName="SearchAffiliated" %>
First User Control call
<uc:SearchAffiliated runat="server" ID="ucSearchAffiliated" />
Second User Control Call
<uc:SearchAffiliated runat="server" ID="SearchAffiliated1" />
See The Users Controls have diferents IDs, That Works For call The same UC in one web form.
Cheers.
Use the loop of how many times that u want the usercontrol to display:
for(int i=0;i < n; i++) //Define the number of time that u want to display
{
usercontrolname.id="UC"+i;//Assign the id for that usercontrol
//add line of code,that controls in placeholder or panel
}
You can use same user control in same page in N number of times. either it dynamically or statically. Only the thing is we need to give the different ID for the usercontrol
This need to add in your Reg
<%# Register Src="~/UserControls/Hello.ascx" TagPrefix="uc" TagName="Hello" %>
calling the user control 1
<uc:Hello runat="server" ID="ucHello1" />
calling the user control 2
<uc:Hello runat="server" ID="ucHello2" />
I faced same problem but the problem was not related with the ID you prefer to use inside the asp.net page. Because I guess asp.net automatically generates ID for each element in page if it doesn't have one.
The problem was I copied ascx files and forget to change their class names. Make sure you use different class names for each user control.
<%# Control Language="C#" ClassName="ucSchema2" %>
I know this i late to the game but maybe someone can use this answer anyway.
Using a static clientside id for a html element in a user control and then using that user control multiple times will results in invalid html.
You are only allowed to use an id once on a html page otherwise getElementById (which returns a single element) will not know which element to get and then you will probably not get any element whatsoever.
You need to make those clientside ids inside the user control truely unique, i.e. selectMonth, selectYear.
This might be done by appending the ClientID of the user control to the element id. Something like this (not sure if this approach really works but you get the idea):
<select onchange="showCalenderBody(createCalender(document.getElementById('selectYear_<%=this.ClientID%>').value,
this.selectedIndex, false));" id="selectMonth_<%=this.ClientID%>">
This way the clientside id should be a combination of locally unique ids and the page unique control ids.

Spring MVC Binding: How to find if bind action was successful

I have a Spring MVC form:select whose form:options are bind with a List<Custom_Object>. The List<Custom_Object> is named as LOCALIZATION_LIST in the code below.
The path attribute of form:select is used to set the selected option.
<form:form action="editNode.do" method="post" name="editNodeForm" commandName="editElementDetails">
<table>
<tr>
<td>Data Type</td>
<td>
<form:select path="datatype" onchange="" cssClass="large" id="datatypes">
<c:if test="${! empty LOCALIZATION_LIST}">
<form:options items="${LOCALIZATION_LIST}" itemLabel="local_Name" itemValue="local_Name"/>
</c:if>
</form:select>
</td>
</tr>
</table>
</form:form>
Now my problem is that there might be a scenario when editElementDetails.datatype may contain a value which is not there in LOCALIZATION_LIST at all. So currently Spring MVC shows the first element of LOCALIZATION_LIST as selected.
Is there a way I can figure out whether the bind action for setting the selected object in form:options of form:select was successful or not?
So that when the binding was not successful, I can then add one extra form:option with the new value in the form.
Note: Answering my own question
I figured it out for now by manually searching for the incoming command object field value in the LOCALIZATION_LIST and if not found, adding an extra form:option corresponding to it.
I was hoping to find a JSTL/Spring-MVC out-of-box solution for this though.
Here is the code:
<form:form action="editNode.do" method="post" name="editNodeForm" commandName="editElementDetails">
<table>
<tr>
<td>Data Type</td>
<td>
<form:select path="datatype" onchange="" cssClass="large" id="datatypes">
<c:if test="${! empty LOCALIZATION_LIST}">
<c:set var="contains" value="false" />
<c:forEach var="item" items="${LOCALIZATION_LIST}">
<c:if test="${item eq editElementDetails.datatype}">
<c:set var="contains" value="true" />
</c:if>
</c:forEach>
<c:if test="${ !contains }">
<form:option label="${editElementDetails.datatype}" value="${editElementDetails.datatype}"/>
</c:if>
<form:options items="${LOCALIZATION_LIST}" itemLabel="local_Name" itemValue="local_Name"/>
</c:if>
</form:select>
</td>
</tr>
</table>
</form:form>
What you want to achieve is the process of validating a form submission that is what I understood (maybe got you all wrong).You can use Spring-Validator here is one nice tutorial about using it.
Spring Form validation . Hope this helps.

Unable to have fixed form field postions while dynamically adding new fileds in a form

I have a simple password reset dialog, which enables users to set a custom password.
<form id="resetPassword"
action="XXXXX">
<fieldset><input type="hidden" name="userId" value="${user.id}" />
<table>
<tbody>
<tr>
<td><input id="resetCustomPassword" class="ui-widget" type="checkbox"></td>
<td><label>Set Custom Password</label></td>
</tr>
</tbody>
<tbody style="display: none;">
<tr>
<td><label for="newPassword"> New Password: </label></td>
<td><input id="newPassword" class="ui-widget" type="password"></td>
</tr>
<tr>
<td><label for="reTypeNewPassword"> Retype New Password: </label></td>
<td><input id="reTypeNewPassword" class="ui-widget" type="password"></td>
</tr>
</tbody>
Now, if a user checks for "Set Custom password", I dynamically show the new password fields. The problem is in my this label in the above form:
<td><label>Set Custom Password</label></td>
This gets dynamically adjusted to the RIGHT side, so it aligns with the new password fields.
How can I let my above label be static, and not adjust itself dynamically?
Thanks!
Try this:
<td align="left"><label>Set Custom Password</label></td>
You're far better off using styled unordered lists as containers for forms, rather than tables.
See: http://www.alistapart.com/articles/multicolumnlists

Resources