I am facing problem in getting a default value selected in the dropdown.
Code in my jsp is as below
<form:select path="codes" multiple="false">
<form:option value="NONE" label="--- Select Code ---" />
<form:options items="${statusQueryForm.codes}" />
</form:select>
Below is the code in statusQueryForm:
public void setCodes(Collection<String> codes) {
this.codes = codes ;
}
public Collection<String> getCodes() {
return codes;
}
Codes contains array list of Strings contains data like {"123","345"}
Now when the page is getting loaded,last option is selected 345.What I want is "Select code" should be selected by default.
Please someone can guide me in right direction. I tried to understand the documentation but have failed to arrive at some sense in this example.
Well I was able to fix this by adding one more property code to the StatusQueryForm. Jsp code was changed as below:.path was changed from codes to code
<form:select path="code" multiple="false">
<form:option value="NONE" label="--- Select Code ---" />
<form:options items="${statusQueryForm.codes}" />
</form:select>
Related
I am working on a Spring MVC web app and I'm trying to figure out a way to display a property of type Set that is part of the entity in question. For the rest of the properties (String, boolean, int, etc) I can get away with the JSP file just using something like this to display them :
<li>
<label for="active">Active : </label>
<form:checkbox path="active" id="active" disabled="true"/>
<span><form:errors path="active" cssClass="error"/></span>
</li>
However, for a property of the entity which is a Set of other entities I cannot figure out how to display it. I'm thinking of using some sort of sub-table but I fiddled with it a lot with no success. I tried searching google and can't quite find what I need.
Thanks!
* New problem, but getting there! *
My OrderedMed class has a property of type StrengthUnit and I'm able to display its name using the method in the comments below :
<li>
<label for="meds[${loop.index}].strengthUnit.name">Strength Units : </label>
<form:input path="meds[${loop.index}].strengthUnit.name"/>
<form:errors path="meds[${loop.index}].strengthUnit.name" cssClass="error" />
</li>
However, I need this to be a drop-down that the user can change. My Med entity also has a property of type StrengthUnit and I'm able to accomplish what I want in its respective edit page by sending a list of all Strength Units from the controller and using the following code :
<select name="strengthUnit" path="strengthUnit.name" id="strengthUnit">
<option value="0" ${med.strengthUnit eq null ? 'selected' : ''}></option>
<c:forEach items="${strengthUnits}" var="strengthUnitSingle">
<option value="${strengthUnitSingle.id}" ${med.strengthUnit.name eq strengthUnitSingle.name ? 'selected' : ''}>${strengthUnitSingle.name}</option>
</c:forEach>
</select>
I'm trying to do the same thing with this entity and I cannot seem to make it work using the method suggested in the comments. I'm trying the below code :
<select name="strengthUnit" path="strengthUnit.name" id="strengthUnit">
<option value="0" ${meds[loop.index].strengthUnit eq null ? 'selected' : ''}></option>
<c:forEach items="${strengthUnits}" var="strengthUnitSingle">
<option value="${strengthUnitSingle.id}" ${meds[loop.index].strengthUnit.name eq strengthUnitSingle.name ? 'selected' : ''}>${strengthUnitSingle.name}</option>
</c:forEach>
</select>
Now i am getting this exception again :
javax.el.PropertyNotFoundException: Property '0' not found on type mdhis_webclient.entity.OrderedMed
I am using the same method to access the Set's index, what am I doing wrong?
Thanks!
You will probably need a forEach loop with the varStatus, example:
<c:forEach var="yourVar" items="${yourList}" varStatus="loop" >
<label for="yourVar[${loop.index}].something">Something ${loop.index + 1}</label>
<form:input path="yourVar[${loop.index}].someting" cssClass="form-control" autocomplete="off" />
<form:errors path="yourVar[${loop.index}].something" cssClass="text-danger" />
</c:forEach>
For the second part of my question, this is what i needed! I just needed a toString() method to my StrengthUnit entity (they should all have one anyway!)
<form:select name="strengthUnit" path="meds[${loop.index}].strengthUnit.name" id="strengthUnit">
<form:option value="0" label=""></form:option>
<form:options items="${strengthUnits}" />
</form:select>
I am trying to preselect some values from a form:select tag. So, let's take a look at my sample class below:
public class Post
{
private boolean anonymize;
private boolean videoRequired;
/* getters, setters */
}
As you can see, there are two boolean fields. I'm using the first one in JSP like this:
<label>Anonymize</label>
<form:select path="anonymize" id="anonymize" class="form-control">
<form:option value="true" label="Yes" selected="${ (post.anonymize) ? true : ''}" />
<form:option value="false" label="No" selected="${ (!post.anonymize) ? true : ''}" />
</form:select>
which, when I inspect that element, results in (let's say that anonymize is true):
<select id="anonymize" name="anonymize" class="form-control">
<option selected="true" value="true">Yes</option>
<option value="false">No</option>
</select>
So far so good.
Now I do the EXACT same thing with my other boolean field videoRequired. First the JSP:
<label>Video required</label>
<form:select path="videoRequired" id="videoRequired" class="form-control">
<form:option value="true" label="Yes" selected="${ (post.videoRequired) ? true : '' }" />
<form:option value="false" label="No" selected="${ (!post.videoRequired) ? true : '' }" />
</form:select>
Which, results in (again, let's say that videoRequired is also true):
<label>Video required</label>
<select id="videoRequired" name="videoRequired" class="form-control">
<option selected="true" value="true">Yes</option>
<option value="false" selected="selected">No</option>
</select>
Take a look at this line:
<option value="false" selected="selected">No</option>
How can this even happen? I'm stuck with this for a few hours now, I have even tried with <c:choose> tag and then hardcoding the selected attribute - it gave me the exact same weird result. Can anyone please explain where have I gone wrong? I'm pulling my brains out since I'm doing the same thing on both fields, but still one works, the other one doesn't. Btw, when I take a look at mysql, both fields are properly set.
In order to pre-select a form:select element, you can set the corresponding variable anonymize to true in your controller method that returns your view.
#Controller
public String getForm(ModelMap model, Post post){
post.setVideoRequired(true);
return "view-name";
}
in my jsp I list Projects and Home Office states. Projects is a nice drop-down that is collapsed, but Home Office drop-down shows first several entries. Why does Home Office drop-down act differently than Projects? I want it to be collapsed like Projects. getProjects() returns a List of Project objects and getHomeOffice() returns a List of String objects.
<p>
Project: <form:select path="project">
<form:option value="NONE" label="--- Select ---" />
<form:options items="${filterBoard.getProjects()}" />
</form:select>
</p>
<p>
Home Office: <form:select path="homeOffice">
<form:option value="NONE" label="--- Select ---" />
<form:options items="${employee.getHomeOffice()}" />
</form:select>
</p>
I figured it out. I needed to change the data type of the homeOffice field in my java class from List to String.
The "project" field that corresponds with <form:select path="project"> is a String in my java class and filterBoard.getProjects() returns a List. And that works. I had both the "homeOffice" field and employee.getHomeOffice() as Lists. I changed the "homeOffice" field to be a String and renamed my getHomeOffice() method to getHomeOfficeList() and still had it return a List and that fixed it.
So recently I have run into an error that I have never encountered.
The code of method _jspService(HttpServletRequest, HttpServletResponse) is exceeding the 65535 bytes limit
I set the model attribute in the controller, granted the object is fairly large I wouldn't expect for it to throw an error. I started by limiting the code to figure out what was causing the issue. I have read here to remove the <jsp:include and use <%# include.
Next step I took was to break up the JSP. I realize now that I am building out my Colors and Fonts individually like this
<form:select path="bottom.backgroundColor" class="form-control">
<form:option value="#000000">Black</form:option>
<form:option value="#FFFFFF">White</form:option>
<form:option value="#FF0000">Red</form:option>
<form:option value="#00FF00">Green</form:option>
<form:option value="#0000FF">Blue</form:option>
<form:option value="#F0F000">Yellow</form:option>
<form:option value="#FF7000">Orange</form:option>
<form:option value="#600060">Purple</form:option>
<form:option value="#905030">Brown</form:option>
</form:select>
Removing these items has fixed my issues.
QUESTION: Why? My understanding is that a JSP builds out its own class however it doesn't seem like it took much to break it. Is the problem the size of the modelAttribute that was passed in? It sounds like others have solved issues with loops and such. Can anyone point me to a resource or explain this better? I can supply more code if necessary.
The problem is that your JSP is too large, even after reducing several parts of it. Seems that you still are repeating this part of the JSP code several times (or similar):
<form:select path="bottom.backgroundColor" class="form-control">
<form:option value="#000000">Black</form:option>
<form:option value="#FFFFFF">White</form:option>
<form:option value="#FF0000">Red</form:option>
<form:option value="#00FF00">Green</form:option>
<form:option value="#0000FF">Blue</form:option>
<form:option value="#F0F000">Yellow</form:option>
<form:option value="#FF7000">Orange</form:option>
<form:option value="#600060">Purple</form:option>
<form:option value="#905030">Brown</form:option>
</form:select>
which in the end is fill the _jspService method in the auto generated servlet. A solution for this problem is to store this static data in application scopde a.k.a. ServletContext, and fill the values of your <form:option>s from it.
I have a Spring MVC application. My JSP allows the user to select a value from a drop down list - this is optional - they do hot HAVE to make a selection.
On my JSP I have code similar to
<form:select path="referral.gpsurgery.id" >
<form:option value="-1" label="--- Select ---"/>
<form:options items="${gpsurgery}" itemValue="id" itemLabel="practiseName"/>
</form:select>
If the user does not make a selection, a referral.gpsurgery object is passed thru to my controller method with an id value of "-1" ( which is what I would expect ). What this actually means as far as my code is concerned is that when the data is persisted, the gpsurgery field on my referral entity should be set to null.
It feels like Spring should sort this out for me, rather than me having to look at my object graph and remove any gpsurgery instances which have an id of "-1", but I am drawing a blank on how to achieve this. It seems like such a common use case that there must be a standard way of doing it.
As also stated in this question, you can use:
<form:select path="referral.gpsurgery.id">
<form:option value=""> </form:option>
<form:options items="${gpsurgery}" itemValue="id" itemLabel="practiseName"/>
</form:select>