【发布时间】:2018-04-02 19:11:11
【问题描述】:
我正在使用 maven 创建日历 Web 应用程序,并且我试图使用 JQuery .ajax 来更新站点而不重新加载页面。但是我在更新正确值时遇到问题。
这是我的 servlet 中的 doGet 方法:
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
int monthChanged = 10;
String action = req.getParameter("action");
String jsp = "/jsp/unischeduleshow.jsp";
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(jsp);
if(action != null){
monthChanged--;
req.setAttribute("monthChanged", monthChanged);
dispatcher.forward(req, resp);
System.out.println(monthChanged);
}
else{
req.setAttribute("monthChanged", monthChanged);
dispatcher.forward(req, resp);
}
}
这里是 JSP 中的 .ajax:
$.ajax({
type: "GET",
data : { action: "backMonth"},
url : "/unischedule",
success: function(){
console.log("${monthChanged}");
}
我也试过这个,但效果一样:
$(document).ready(function(){
$(document).on("click", "#forward", function() {
$.get("/unischedule",{action:"backMonth"}, function(responseText) {
console.log("${monthChanged}");
});
});
});
我简化了代码以更好地显示问题。我正在尝试减少monthChanged 值并在单击按钮时将其发送到站点。问题是System.out.println("monthChanged"); 正在打印递减的值,但是当我在网站上尝试console.log() 它时,它显示第一个值10。我试图以多种方式做到这一点,但我找不到解决方案。 else 块中的第二个调度程序是否覆盖第一个调度程序?
【问题讨论】: