Im trying to access a list within a dropdown in Javascript. So I have an object called System (which is the object stored in the dropdown) and this has a List of Collections. I am new to Javascript and not sure what the correct syntax should be to access the list.
So this is the form that contains the dropdown:
<form:form id="systemForm" method="post" action="/application/SystemSave" commandName="systemForm">
<form:select path="uSelectedSystemId" id="uSystemId">
<c:forEach var="system" items="${systemsList}">
<form:option value="${system.id}" label="${system.name}" />
</c:forEach>
</form:select>
</form:form>
and this is the setup of the System object (from Java):
public class System {
private Long id;
private String name;
private List<Collection> collections;
}
and this is the setup of the SystemForm used to store the values:
public class SystemForm {
private String fUpdateSystemName;
private String uSelectedSystemId;
private List<Collection> uCollections;
}
I guess I need some sort of hidden field within the loop that stores the values but im not sure of the correct syntax. I could just rehit the database and get all Collections based on the systemID but I dont think this is the correct way of doing things seeing as the information should already be available.
Any help much appreciated!
try this
<form:form id="systemForm" method="post" action="/application/SystemSave" commandName="systemForm">
<select id="uSystemId">
<c:forEach var="system" items="${systemsList}">
<option value="${system.id}" label="${system.name}" />
</c:forEach>
</select>
</form:form>
i dont know exactly this will meet your requirement or not.
Related
I am having problems accessing a model attribute in my controller, using Spring.
When adding to the model, I write the status code as a key and the enumeration name as a value. The status code is e.g. AVAILABLE, NOTAVAILABLE, etc.:
String code = status.getCode();
String enumerationName = enumerationService.getEnumerationName(status, currentLocale);
model.addAttribute(code, enumerationName);
On my JPA page, I am trying to access the corresponding value using the key (status code, e.g. AVAILABLE):
<div data-availability>
<c:forEach items="${StockLevelDeliveryStatus.values()}" var="status">
<c:set var="textStyle" value="text-success" />
<c:if test="${status.code.toLowerCase() == 'notavailable'}">
<c:set var="textStyle" value="" />
</c:if>
<div class="d-none display-22 pb-2 ${textStyle}" data-availability-item data-${status.code.toLowerCase()}>
${status}
</div>
</c:forEach>
</div>
For example, the value of status is AVAILABLE and this is what is output in ${status}. However, I want the value AVAILABLE to be used as a key to return me the correct value that I set in the model above. If I change the ${status} statement to, say, ${AVAILABLE} instead, which is the concrete key, the appropriate value from the model is returned:
<div class="d-none display-22 pb-2 ${textStyle}" data-availability-item data-${status.code.toLowerCase()}>
${AVAILABLE}
</div>
If I understand it correctly, then instead of passing the enum value as a key, I need to somehow teach Spring to search in the model for the appropriate key.
As recommended in one of the replies, I also tried writing the Map<StockLevelDeliveryStatus, String> directly into the model:
Map<StockLevelDeliveryStatus, String> statusMap = new HashMap<StockLevelDeliveryStatus, String>();
for (StockLevelDeliveryStatus status : StockLevelDeliveryStatus.values()) {
statusMap.put(status, enumerationService.getEnumerationName(status, currentLocale));
}
model.addAttribute("statusMap", statusMap);
And the JSP accordingly:
<div data-availability>
<c:forEach items="${StockLevelDeliveryStatus.values()}" var="status">
<c:set var="textStyle" value="text-success" />
<c:if test="${status.code.toLowerCase() == 'notavailable'}">
<c:set var="textStyle" value="" />
</c:if>
<div class="d-none display-22 pb-2 ${textStyle}" data-availability-item data-${status.code.toLowerCase()}>
${statusMap[status]}
</div>
</c:forEach>
</div>
Here it already fails when accessing the model, because with this approach I do not get any output on the JSP.
Does anyone have any ideas on how to make this work?
Why not simply put a Map<StockLevelDeliveryStatus, String> into the model? You could then simply use ${statusMap[status]}.
I want to add a Select:multiple tag to my JSP and then map it to a form model class, where I'll retrieve the data loaded.
But I did not find what type to use for the variable so that I can do it.
What would it be?
Ex.:
JSP
<form:select multiple="true" path="harmonic">
<c:forEach begin='1' end='15' varStatus='loop'>
<form:options items="<--Variable?->" itemValue="loop" itemLabel="loop"/>
</c:forEach>
<form:select>
Class
public class HarmonicForm{
private ???? selectMultiple;
}
Spring selects admit any Collection like List or Set.
private List<YOUR_OBJECT_TYPE_HERE> selectMultiple;
You can pass your model to the view and there's no need to use foreach nor option tag.
<form:select multiple="true" path="harmonic" items="${items}" itemLabel="harmonicLabelAttribute" itemValue="harmonicValueAttribute" />
You can check the TLD documentation here.
Use a list of elements
public class HarmonicForm{
private Listy<Harmonic> harmonic;
}
public class Harmonic{
//Be sure to implement equals and hashcode
}
You can find a similar case here Spring select multiple tag and binding
You need to use Collection<?> with some object inside. You can create your own object or use SelectItem.class
and then,
<form:select multiple="true" path="harmonic">
<form:options items="${selectMultiple}" itemValue="value" itemLabel="name"/>
</form:select>
Someone guided me towards the spring MVC form tld yesterday morning and I've been struggling to do what looks like a very simple task ever since! It looks like a simple solution but I just can't seem to get it right. I'm thinking I must be misunderstanding something very fundamental to be unable to get it working. I was wondering if someone could point out where I'm going wrong?
At the moment all I'm trying to do is display a list of values in a jsp select.
The model entity is very simple (and I realise needs a little fine tuning)
#Entity
#Table(name = "user")
public class User {
#Id private String userId;
private String userName;
private String passwordCode;
private Date dateOfBirth;
public String getUserId() {
return userId;
}
public String getUserName() {
return userName;
}
public String get PasswordCode () {
return passwordCode;
}
public Date getDateofBirth() {
return dateOfBirth;
}
}
My controller is also currently very simple
#Controller
#RequestMapping("/")
public class HomeController extends JFrame {
#Autowired private MeetingDAO meetingDAO;
#RequestMapping(method = RequestMethod.GET)
public String HomePage(Model model) {
List<User> userlist = userDAO.ListAll();
model.addAttribute("userlist ", userlist);
model.addAttribute("User", new User());
return "Home";
}
I got this far by using another q/a on stack overflow and I wonder if this is where my misunderstanding originates. I am passing in the list of values as an attribute as I have all along. I got a lot of errors doing this until I realised that I also needed to 'pass' the User model so that the spring tags in the jsp would understand the structure of the User object. This means I am now passing 2 attributes - User, as I understand it just so the form understands the structure (although maybe if I was cleverer this would also return the selected object?) and the userlist which is the actual data.
In the jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%# taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<html>
<body>
<h2>Users List Page</h2>
<form:form action="/home" method="Post" modelAttribute="User" >
<form:select path="userName">
<form:options items="${userlist}"/>
</form:select>
<input type="submit" id="submit" value="View"/>
</form:form>
</body>
</html>
I have paired this back down for simplicity but have tried quite a few things (itemvalue, itemlabel amongst others). This code returns all the data but in a comma separated list of all values (id, name, password, date). Whatever I try to do with path, itemvalue etc it won't just display one field. Ideally I wold like to display - userName (userId) - but at this stage I would probably settle for userName!
From my flapping about my impression is not connecting that User is an item in the userlist. If I feel I'm getting close it complains that userId (etc) is not a method of type array (which I assume means it is looking at the list) or if it seems to have understood the properties it has no data (which I guess means it's not picking up the list!).
could anyone help !?
Just to add this is the closest I think I have got but it complains that User is not valid a property of User ?
<form:form action="home" method="Post" modelAttribute="User" >
<form:select path="User" >
<form:options items="${userlist}" itemValue="userId" itemLabel="userName" />
</form:select>
<input type="submit" id="submit" value="View"/>
</form:form>
<form:form action="home" method="Post" modelAttribute="User" >
<form:select path="userId" >
<form:options items="${userlist}" itemValue="userId" itemLabel="userName" />
</form:select>
<input type="submit" id="submit" value="View"/>
This code will have the selected userId set in User object which you will get when you post the form.
path variable should be bound to backing object property here backing object is User so you should bind its userId property.
You can try the following for setting options (assuming userlist is a list of User objects and has fields userId and userName):
<c:forEach var="u" items="${userlist}">
<form:option item="${u}" itemValue="${u.userId}" itemLabel="$(u.userName}" />
</c:forEach>
Many thanks to all that helped. Actually although this was very useful it also made me realise I've been looking in the wrong place for 1 - 2 days! As I was filling the UserList from a stored proc not directly from hibernate it wasn't actually a list of Meeting objects after all but a list of comma separated values. Apologies I didn't post this bit of code (or look at it again until now). Ouch!
In my Spring Application have one Bean..
public class SampleBean{
private String id;
private String name;
private String marks;
--------
--------
}
And my Another Service bean Crating Property like..
List<SampleBean> list = new Array<SampleBean>();
//Setter and getters
And in my Controller class Set some values to list
And now my jsp i'm getting these values using Jquery..
<c:set var="modalAttributeName"value="MODEL1" />
<c:set var="modalAttribute"value="${requestScope[modalAttributeName]}" />
<form:select class="select_box" path="country" id="country">
<form:options items="${modalAttribute.list}" />
</form:select>
It is showing entire Bean name like SampleBean#0
But i want in this Select box only SampleBean class name only..
So how to Display name using Jquery?
EDIT
i'm trying like this but empty box showing..
<form:select class="select_box" path="country" id="country">
<c:forEach items="${modalAttribute.list}" var="result">
<form:options item="${result.name}" />
</c:forEach>
</form:select>
Add this list to your model attribute map. I assume you already have a model map passed into your controller method. So for example:
model.put("list", list);
If you don't have the model map passed to your method, then just another parameter: Map<String, Object> model.
Then in your JSP:
<form:select class="select_box" path="country" id="country">
<c:forEach items="${list}" var="result">
<form:option value="${result.name}">
<c:out value="${result.name}"/>
</form:option>
</c:forEach>
</form:select>
a possible sulition can be render the list as JSOn from controler and catch it in a java script code making an ajax call , thus its easier to use in jquery
The same EL expression ${taskId} gives two different values in different places.
I am using the Stripes framework, along with MongoDB and Morphia for Object-Mapping (and of course Java/JSP/etc).
Using the following JSP/Expression Language code:
<c:forEach items="${actionBean.tasks}" var="listTask">
<c:set var="taskId" scope="page" value="${listTask.id}"/>
<s:form method="post" beanclass="action.TaskActionBean">
${taskId}<s:hidden name="task.id" value="${taskId}"/>
<s:submit name="promoteTask" value="Up" />
</s:form>
</c:forEach>
Gives the following generated code:
<form method="post" action="/scrumyogi/">
4ef99b730364de7ec70dbd68
<input type="hidden" value="4ef99b6c0364de7ec70dbd67" name="task.id">
<input type="submit" value="Up" name="promoteTask">
<div style="display: none;">
<input type="hidden" value="NPNEJw6tUWfRBXf-vVOLTw==" name="_sourcePage">
<input type="hidden" value="XbfUDiSHGrU=" name="__fp">
</div>
</form>
As you can see ${taskId} is printing out 4ef99b730364de7ec70dbd68 and then 4ef99b6c0364de7ec70dbd67, which makes no sense to me, I need ${taskId} to print out the same value 4ef99b730364de7ec70dbd68 is the correct one.
Is there some known issue that could cause this.
EDIT: the real problem is that the ${taskId} within the hidden form tag is incorrect, I printed the other value to see what the expression contains, and then found that it's different in the different locations - which make things seriously confusing.
ActionBean code:
#UrlBinding("/")
public class TaskActionBean extends BaseActionBean{
String taskId;
Task task = new Task();
List<Task> tasks;
public final static String DISPLAY = "/index.jsp";
#DefaultHandler
public Resolution listTasks(){
tasks = Dao.datastore().find(Task.class).order("rank").asList();
return new ForwardResolution(DISPLAY);
}
public Resolution promoteTask(){
task.promoteTask();
tasks = Dao.datastore().find(Task.class).order("rank").asList();
return new ForwardResolution(DISPLAY);
}
// ... getters and setters
You have a taskId field in you action bean, and according to stripes taglib documentation:
The hidden tag assigns the value attribute by scanning in the following order:
for one or more values with the same name in the HttpServletRequest
for a field on the ActionBean with the same name (if a bean instance is present)
by collapsing the body content to a String, if a body is present
referring to the result of the EL expression contained in the value attribute of the tag.
So it probably finds the field in your action bean and takes the value from there.
The other (jsp el) ${taskId} is assigned from task list element.
Change the taskId to some name that doesn't coincide with your action bean field and it should work.