0

I have a button, which calls ajax POST method on a predefined URL. After the server has done its thing, it simply refreshes the page

The server fetches User object from the database and updates one column.

When the page refreshes then about 50% of the time, the User is still not updated and the refreshed page displays old information

Note - Code is cleaned up to only show the relevant

Ajax post method:

function buttonClick(){
   $.ajax({
      type : "POST",
      url : "?open_class=1",
      success : refresh()
   });
}

function refresh(){
   window.location = contextPath;
}

Spring MVC

@RequestMapping(value = "/manager/listofquestions", method = RequestMethod.POST)
public String listOfQuestionsPost(
        Model model,
        HttpServletRequest request,
        Principal principal) {

    if(request.getParameter("open_class") != null){
        userService.openClass(principal.getName());
    }
    return "success";
}

UserService

@Transactional
public void openClass(String name) {
    User user = userDao.getByName(name);
    user.setCodeword(codeWordService.GetNewCodeWord());
    userDao.update(user);
}

UserDao

public User update(User user) {
    sessionFactory.getCurrentSession().update(user);
    return user;
}

public User getByName(String userName) {
    Query query = sessionFactory.getCurrentSession().createQuery("from User where username=?");
    query.setString(0, userName);
    query.setCacheable(true);
    Object[] list = query.list().toArray();
    if(list.length == 0) {
        return null;
    }
    return (User) list[0];
}
4

1 回答 1

0

原来我没有正确使用 Ajax。ajax post一开始就调用refresh方法,这就是页面刷新太快的原因。你需要这样做:

success: function() { refresh(); }

或者像我最终做的那样使用 Jquery 帖子

完成它 - 问题不在于休眠。不过谢谢你的支持

于 2013-07-21T18:34:30.740 回答