request.getParameter(“pgIndex”) 总是在 servlet 中返回 null
request.getParameter("pgIndex") always returns null in the servlet
我正在尝试使用休眠对 servlet 中的表行进行分页。但是一旦我点击页面的欲望索引,它总是只给我表格的第一行。所以我在每个主要部分都放了System.out.print()
,最后发现request.getParameter("pgIndex")
总是返回 null。
我的servlet代码:
int pageIndex = 0;
int totalNumberOfRecords = 0;
int numberOfRecordsPerPage = 5;
String sPageIndex = request.getParameter("pgIndex");
//whether pgIndex=1 or pgIndex=2 in the url, always returns null as the output.
System.out.println("pg - " + sPageIndex);
pageIndex = sPageIndex == null ? 1 : Integer.parseInt(sPageIndex);
int s = (pageIndex * numberOfRecordsPerPage) - numberOfRecordsPerPage;
List<ProductHasSize> phs = ses.createCriteria(ProductHasSize.class)
.setFirstResult(s)
.setMaxResults(numberOfRecordsPerPage)
.list();
for (ProductHasSize pro : phs) {... some html content here...}
Criteria criteriaCount = ses.createCriteria(ProductHasSize.class);
criteriaCount.setProjection(Projections.rowCount());
totalNumberOfRecords = (int) (long) (Long) criteriaCount.uniqueResult();
int noOfPages = totalNumberOfRecords / numberOfRecordsPerPage;
if (totalNumberOfRecords > (noOfPages * numberOfRecordsPerPage)) {
noOfPages = noOfPages + 1;
}
for (int j = 1; j <= noOfPages; j++) {
String myurl = "products.jsp?pgIndex=" + j;
String active = j == pageIndex ? "active" : "";
s2 = s2 + "<li class='" + active + "'><a href=" + myurl + ">" + j + "</a></li>";
}
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().write("[{'"d1'":'"" + s1 + "'",'"d2'":'"" + s2 + "'"}]");
产品.jsp
<div class="row">
<div class="col-md-12">
<ul class="pagination" id="pagId"></ul>
</div>
</div>
JavaScript
$(document).ready(function () {
$.ajax({
url: 'AdimnProductFilterAction',
dataType: 'json',
cache: false,
success: function (data) {
$.each(data, function (key, value) {
$('#proFilterTab').html(value.d1);
$('#pagId').html(value.d2);
});
},
error: function () {
alert('error');
}
});
});
更新:
$(document).on("click", "#pagId a", function (event) {
//tried with adding another function . But still returns null.
event.preventDefault();
var para = $(this).attr('href').match(/'d+/);
$.ajax({
url: 'AdimnProductFilterAction',
dataType: 'json',
data: {pgIndex: para},
cache: false,
success: function (data) {
$.each(data, function (key, value) {
$('#proFilterTab').html(value.d1);
$('#pagId').html(value.d2);
});
},
error: function () {
alert('error');
}
});
});
提前谢谢。
在发送 JSON 数据时,您不会简单地将其作为请求参数接收。相反,只需添加"normal"参数:
以 HTTP POST
发送
$.ajax({
url: 'AdimnProductFilterAction',
type: 'POST',
data: {
'pgIndex': para
},
cache: false,
success: function (data) {
$.each(data, function (key, value) {
$('#proFilterTab').html(value.d1);
$('#pagId').html(value.d2);
});
},
error: function () {
alert('error');
}
});
或作为 HTTP GET
$.ajax({
url: 'AdimnProductFilterAction?pgIndex='+para,
cache: false,
success: function (data) {
$.each(data, function (key, value) {
$('#proFilterTab').html(value.d1);
$('#pagId').html(value.d2);
});
},
error: function () {
alert('error');
}
});
将参数添加到 servlet 调用中。
相关文章:
- 数组在递归方法中设置为null
- 字符串在将其传递给另一个活动Android JavaScript时读取Null
- 使用angularjs向浏览器发送servlet响应(下载功能)
- 当值更改时,在servlet中自动获取textarea值,无需单击按钮
- 无法获取属性'selectedIndex'的未定义引用或null引用
- Meteor方法在客户端返回null,在客户端运行的相同方法返回正确的值
- 来自文本输入null的html javascript变量
- delete在Object上效率低下,但在DOM Element's的数据属性,与null out相比
- php代码在textbox更改事件上显示null
- servlet中的请求对象,而不是从jsp接收参数值
- JSON分析错误:Java中AJAX、Javascript和Servlet的意外EOF
- 相同的RegExp返回不同的结果-第一次是正确的结果,第二次是null
- 在php中提交数据时,如果某些值为null,而某些值为非null,如何进行查询
- 未捕获的类型错误:无法读取属性'addEventListener'的null chrome扩展名
- 为什么localStorage[“..”]未定义,而localStorage.getItem(“..”)为null
- 当请求时,我如何从文本区域删除null.GetAttribute为null,以及如何从servlet修改标签的可见性
- request.getParameter(“pgIndex”) 总是在 servlet 中返回 null
- servlet中的数组为null
- null JSON in servlet
- 当尝试使用javaScript调用Servlet动作时得到Null