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.
Related
I am using javascript innerHTML to post some data from javascript. Please find the below code:
var innerHTML='<% SomeClass.getSomething %>';
document.getElementById("something")=innerHTML;
HTML:
<div id="something"></div>
Now i am able to see the output as <% SomeClass.getSomething %>. But how do i render it to show the value of getSomething. Please Help.
The SomeClass is defined in jsp where the html is there.
Assuming your code is in a JSP file I think you want the following
var innerHTML='<%= SomeClass.getSomething %>';
The <%= tells the JSP to output the expression in place.
Try
var innerHTML='${SomeClass.getSomething}';
If you are passing it througn model.
I want to create a web widget that can be embedded multiple times on the same page but with different data attribute values so I can display different data according to the data attribute value.
For example, I want to embed mywidget.js file multiple times as follows:
<body>
<div>
<script src="script/mywidget.js" data-sport="soccer" id="widget-soccer">
</script>
</div>
<div>
<script src="script/mywidget.js" data-sport="tennis" id="widget-tennis">
</script>
</div>
</body>
My question is, inside the code in mywidget.js, how do I determine the correct script tag reference and read it's data attribute so I can use that value to fetch the corresponding data from a web service. I am using only jquery and javascript.
I want the widget to be embeddable on other users sites as well so all they do is embed using only the script tag and passing in the desired data attribute value without adding anything extra anywhere they need on their website.
This is not really a very good approach, as it is very inflexible. But given that <script> tags, when not deferred, halt parsing of the document while they execute, the current script tag will be the last in the DOM; so you can get the current sport inside your script by using this:
var sport = $('script').last().data('sport');
However, it would be much better to define a function in your external JavaScript file, and then invoke it when you need to instantiate your widget (EDIT: like in Lee Taylor's answer).
Why don't you do something like:
<head>
<script src="script/mywidget.js"></script>
</head>
<body>
<div><script>createMyWidget({sport : "soccer"} );</div>
<div><script>createMyWidget({sport : "tennis"} );</div>
</body>
I don't think you can. I know it's not that nice, but I would try:
<div><script>sport = "soccer";</script><script src="script/mywidget.js" id="widget-soccer"></script></div>
<div><script>sport = "tennis";</script><script src="script/mywidget.js" id="widget-tennis"></script></div>
and use sport in mywidget.js
Another approach could be that myscript.js is actually a dynamic "page", let's say with php, then you could use src="script/mywidget.js?sport=swimming", and in the php you would print:
sport = "<?php echo addcslashes($_GET['sport'], '"'); ?>";
But even better would be:
<script src="script/mywidget.js"></script>
<div><script>showWidget("soccer");</script></div>
<div><script>showWidget("basketball");</script></div>
I think you can use jQuery to find all script tags with src="script/mywidget.js" or something
$('script[src="script/mywidget.js"]')
And then you'll have an array of scripts tags that you can loop through and access the data property using jQuery's .data() method.
I need to include a js file in my views.
But in this js file i need to interpret somes PHP variable.
Actually i do this :
#section('javascript')
<script>
alert("{{{test}}}");
</script>
#stop
But i REALLY need to do this :
#section('javascript')
{!! Html::script('js/test.js') !!}
#stop
test.js :
alert("{{{test}}}");
I need to declare a lot o variable. So my JS file will be very huge. And i don't want to show this directly in the source code.
How can i do ?
Thank you !
You can only pass the variable to the javascript like so:
#section('javascript')
<script>
var test = {{{$test}}};
</script>
#stop
then in your javascript file included at the bottom you can use it:
alert(test);
Let me just mention that this is not a great way of handling the passing variables from php to javascript.
When I need to do something like this, I usually create a meta tag on the page which would contain the alert information.
<meta name="someAlertValue" content="{{{ $test }}}" />
Then you can very easily grab that via jQuery.
var alert_text = $('meta[name=someAlertValue]').attr('content');
I find this approach to be much cleaner and maintainable than trying to drop php variables directly into your javascript.
I had the same problem, and wanted to have a stand alone js which will have bunch of variables taken from config() or even from database, multi-language, and will be configurable, or even will work with query parameters.
Maybe it's a hard way, but i've created a route:
Route::get('/js-modules/test.js',function(){ return view('js-modules.test');})->name('my_js);
So in the view resources/views/js-modules/test.blade.php you can put your js code together with your PHP stuff.
Or you can route it to a controller and have even more background work. it looks a bit slow (in the browser) on the first run , but second request it'll be cashed and retrieved as the regular js file
And now you can link it to any of your pages with
<script src="{{route('my_js')}}"></script>
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
can anybody tell me what am I doing wrong?
I want to retrieve all my alfresco sites with this code (this should work):
model.sites = siteService.listSites(null, null, 0); // return type= List<Site>
And now i want to retrieve this list model.sites in my HTML freemarker template like this:
<body> ${sites} </body>
How can I view this sites list. I know i am getting it wrong in my ftl, but can't find a solution how to retrieve it.
You'll need to loop over the sites in your freemarker. Assuming you wanted a list of the site names, with commas between them, then your freemarker would instead want to look something like:
<body>
<#list sites as site>
${site.shortName}<#if site_has_next>,</#if>
</#list>
</body>