【发布时间】:2014-09-05 01:27:22
【问题描述】:
我有一个 int 变量,它从 java 类传递到 jsp 文件中。我现在想将这个 int 值从 jsp 传递给我拥有的 javascript 文件。我坚持想出一个解决方案。我尝试了以下方法,但仍然无法正常工作:
javascript:
jQuery(document).ready(function() {
jQuery('div.awsmp-hero-featureSwap').each(function() {
jQuery(this).find('div.hero-featureSwap').each(function() {
var width = 0;
var height = 0;
var count = 1;
var perpage = "${products_per_page}"; // ***** this is the value I want from the jsp*****
var totalcarousel = jQuery('.hero').length;
jQuery(this).find('div').each(function() {
width += jQuery(this).width();
count = count + 1;
if (jQuery(this).height() > height) {
height = jQuery(this).height();
}
});
jQuery(this).width(((width / count) * perpage + 1) + (perpage * 12) + 60 + 'px');
jQuery(this).height(height + 'px');
});
});
JSP:
<c:set var="products_per_page" value="3" />
<c:if test="${not null model.listSize}">
<c:set var="products_per_page" value="${model.listSize}" />
</c:if>
我只想将“products_per_page”值传递给 javascript。应该很简单……
【问题讨论】:
-
该 Javascript 文件/sn-p 是否正在被 JSP 引擎解析?您可以检查源并确保实际设置了该值。
-
如果 javascript 文件是外部文件(未嵌入页面中),则 JSP 引擎不会对其进行解析。同时,如果变量没有定义,即使嵌入到页面中也不会插入。
标签: java javascript jquery jsp