I have a route object. It contains arrayList of options. I want to know the size of options and store it in some variable. (This thing needs to be done in JSP). I then want to access this variable in jquery. How to do it?
JSP code :
<%!int loopSize = routes.get(0).getOptions().size(); %>
<c:forEach var="route" items="${routes}" varStatus="loopCounter">
<c:forEach var="option" items="${route.options}"
varStatus="loopCounter2">
<---more code -->
</c:forEach>
</c:forEach>
But it is saying routes undefined. I am using struts2 and action class. Routes is coming from the action class
One solution may be to store the length of array in hidden field and access it in jquery.
<input type="hidden" id="loopSize" name= "loopSize" value= "${fn:length(routes[0].options)}"/>
And using the id you can accesss the value in jquery
Related
I am working on Struts2 application. In my jsp, I have a javascript variable whose value is being assigned through struts tag s:iterator.
Something like this:
//Javascript
// an object agent with getter setter which is being used through request.getParameter().
<script>
var user = new Array();
// i want to assign value to javascript array variable through this Agent object
<s:if test='agent!=null'>
<s:iterator var='i' value='agent'>
<s:if test='i!=null'>
user[user.length]= {
agentName:'<s:property value='agentName'/>',
agentId: '<s:property value='agentName'/>'
};
</s:if>
</s:iterator>
</s:if>
</script>
This javascript array will be used for the dropdown menu optiion in html, where when a person select an agent name, its relevent data will get populated in the next dropdown options.
But the user variable is not taking values through struts tag.
I am stuck on this. Any help?
I am trying to get input from user and invoke Servlet with that parameter but I can not get changed value. I am getting initial value.
<% int quantity = 1; %>
<input type="text" name="quantity" style="width: 20px;left: 52px;" value="<%= quantity %>" class="form"/>
Add to cart
Note that JSP is a HTML preprocessor, meaning it would process the webpage before it's sent to users' browsers. On the user agent it would just be a simple HTML page, these markups (e.g. <%= quantity %>) are replaced with the value of the variable quantity when the page is generated. To achieve what you're aiming for, use javascript.
function getlink(product_id) {
var link = "addToCart.jsp?id="+product_id+"&quantity="+document.getElementById("quantity").value;
window.location.replace(link);
}
call this function
Add to cart
I am trying to pass a Java list into an included JSP page and am having no luck.
I capture the array in a scriplet on the first JSP page:
<% User user = User.getUser(request); %>
..and I pass it to the included JSP (which is essentially a header) like this:
<jsp:include page="includes/mySubNavigation.jsp">
<jsp:param name="myColl" value="<%=user.getObjs() %>" />
</jsp:include>
The problem comes when I try to iterate over and read the collection to build the subnav:
$(function(){
var myObjs = ${param.myColl}
});
The output from this is a String showing the type of Object, i.e.
[com.myProj.app.MyCustomObject#87eerftte]
Can't I pass an Array into the included JSP via jsp:param? How am I supposed to pass my collection along so it can be read on the included JSP?
Thanks for any helpful tips!
Just using JSP EL to put collection into request scope via:
<c:set var="myObjs" value="<%=user.getObjs()%>" scope="request"/>
And on the included JSP I access the collection using this:
${requestScope.myObjs}
Hope this helps somebody.
I have a JSP page containing a ADD button to add the rows(a HTML code) via Javascript.
I now need to retain the value in my form by replacing the codes in JSP by struts-tags.
How should I then communicate from struts-tags and JS. Since all the HTML code lies in JS, how should it use struts-tags???
Please help!!
Your question is too vague to give an appropriate answer. However, I recently did something similar to this so I will try and give you a few guidelines.
1.) If you are hoping to populate these rows with information from the server this will require an ajax call. Most likely to an action that returns a snippet of jsp containing only a table row.
I suggest avoiding the struts2-jquery plugin for this unless you already are using it in your application. I would just use jQuery - http://api.jquery.com/jQuery.ajax/
2.) If you wish to gather user input in these rows you will just have to make sure to use appropriate naming for your fields.
Eg: In your java action you have a List< String > names.
You would need to generate the following html via js.
<tbody>
<tr>
<input type="text" name="names[0]">
</tr><tr>
<input type="text" name="names[1]">
</tr><tr>
<input type="text" name="names[2]">
</tr>
</tbody>
3.) If you wish to keep track of the number of added rows you could use this in your jsp...
<s:hidden name="rowsCount" id="rowsCount" value="0">
then within your javascript change the value of that input.
Hope one of those 3 helped!
I have a Spring MVC application, use JSP+JQuery for view, and what I need is to, based on combo box selection (which gets me an index of element in list) to populate text field.
listProduct - list of products that is in the model
<form:form method="POST" commandName="productForm" name="insertRacun">
<table>
<tr>
<td class="ui-widget">Product:</td>
<td><form:select path="productListId" id="productCombobox">
<form:options items="${listProduct}"itemLabel="name" itemValue="productId"/>
</form:select>
</td>
<td class="ui-widget">Product price:</td>
<td><form:input path="priceList"
class="ui-widget ui-widget-content" id="priceInput" />
</td>
<script type="text/javascript">
var comboIndex = $("#productCombobox").val();
$("#priceInput").val(${listProduct[comboIndex].price})
});
</script>
My question is: When i put number in listProduct[] i.e. listProduct[0] it works just fine and price field gets populated, but when i want put "comboIndex" in brackets, it does nothing.
If there is another solution (not using JQuery), please post it
You are mixing client and server side code. You cannot pass a JS variable to your server code, as your server code is parsed before the page has been served up to the client.
You will need to pass the price corresponding to a particular product ID to the client by other means.
The relevant code (probably in a change event handler) should reference productCombobox selectedIndex instead:
var comboIndex = $("#productCombobox").attr("selectedIndex"); //
$("#priceInput").val(${listProduct[comboIndex].price});