Answer the question
In order to leave comments, you need to log in
How to arrange page refresh in jsp?
Hello. Help, please, to solve a problem.
There is a jsp page - index.jsp . It consists of two (via jsp include) header.jsp and the direct content of index.jsp. Some number is displayed in header.jsp
<p class="list-group-item-text text-overflow">I spend ${userPayment} RUB per month</p>
<div class="popover-content">
<div class="list-group">
<a href="userTariffs" class="list-group-item">
<h4 class="list-group-item-heading text-overflow">
<i class="fa fa-fw fa-envelope"></i> My scope
</h4>
<p class="list-group-item-text text-overflow">I spend ${userPayment} RUB per month</p>
</a>
</div>
<div style="padding:10px"><a class="btn btn-success btn-block" href="userTariffs">Show me
more...</a></div>
</div>
function popBox() {
x = confirm('Are you sure? ');
if (x == true) {
var xhr = new XMLHttpRequest();
xhr.open("POST", "userChangeTariff?sum=" + par1, false);
xhr.send();
@RequestMapping(value = "/userChangeTariff", method = RequestMethod.POST)
public String changeTariff(HttpServletRequest req, Locale locale, Model model,
@RequestParam(value = "sum") String sum) {
req.getSession().setAttribute("userPayment", ++sum);
return "user/userTariffs";
}
Answer the question
In order to leave comments, you need to log in
return "user/userTariffs";
It means to send the user to this page, you make an ajax request and the controller does not send the user to any page. In order for the information to be updated on the page as you want, you need to pass this information from the controller on the server to the client browser in the response, the @ResponseBody annotation and on the client use js to update the html page.
You can pass a piece of html, xml, json in the response, you can simply pass a String, the main thing is that the js code that will process the response understands what to expect from the server.
The simplest option for your case:
@RequestMapping(value = "/userChangeTariff", method = RequestMethod.POST)
@ResponseBody
public Integer changeTariff(HttpServletRequest req,
@RequestParam(value = "sum") Integer sum) {
final Integer newSum = new Integer(sum+1);
req.getSession().setAttribute("userPayment", );
return newSum;
}
<div class="popover-content">
<div class="list-group">
<a href="userTariffs" class="list-group-item">
<h4 class="list-group-item-heading text-overflow">
<i class="fa fa-fw fa-envelope"></i> My scope
</h4>
<p class="list-group-item-text text-overflow">I spend <span id="user_payment_sum">${userPayment}</span> RUB per month</p>
</a>
</div>
<div style="padding:10px"><a class="btn btn-success btn-block" href="userTariffs">Show me
more...</a></div>
</div>
function popBox() {
x = confirm('Are you sure? ');
if (x == true) {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState == XMLHttpRequest.DONE) {
alert(xhr.responseText); //TODO test alert
document.getElementById('user_payment_sum').innerHTML =xhr.responseText;
}
}
xhr.open("POST", "userChangeTariff?sum=" + par1, false);
xhr.send();
<span id="user_payment_sum">
, which contains the data that we want to dynamically change on an ajax request to the server, and a section if (xhr.readyState == XMLHttpRequest.DONE) {
that waits for a response and performs some actions when the response arrives. Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question