Spring MVC、Rest Ajax调用和会话范围对象
我想解决以下问题。我有一个带Thymeleaf的Spring MVC应用程序,通过一个post请求(通过表单发送)触发一个模拟任务,这可能需要几分钟的时间。该任务处理大量数据,我们希望通过JavaScript有一个进度条。如果有两个会话,则应单独触发模拟,每个浏览器显示其进度状态 目前,我们有一个解决方案,什么不是真的一直运作良好 MVC控制器获取Post请求:Spring MVC、Rest Ajax调用和会话范围对象,ajax,rest,spring-mvc,session,thymeleaf,Ajax,Rest,Spring Mvc,Session,Thymeleaf,我想解决以下问题。我有一个带Thymeleaf的Spring MVC应用程序,通过一个post请求(通过表单发送)触发一个模拟任务,这可能需要几分钟的时间。该任务处理大量数据,我们希望通过JavaScript有一个进度条。如果有两个会话,则应单独触发模拟,每个浏览器显示其进度状态 目前,我们有一个解决方案,什么不是真的一直运作良好 MVC控制器获取Post请求: @Autowired SimulatorView view; // SESSION SCOPE @PostMapping("/vie
@Autowired SimulatorView view; // SESSION SCOPE
@PostMapping("/view")
public String run(@ModelAttribute(CHECKS) ChecksDto checksWrapper, Model model) throws InterruptedException, ExecutionException {
view.setStatisticDto(simulate(checksWrapper)); // Can take several minutes
return "simulation/result :: simulated";
}
当我在WebGUI上触发模拟时,会显示一个进度条,通过JavaScript我会频繁调用Rest方法来询问进度状态
RestController
@RequestMapping("simulation/api")
public class SimulatorApi {
@Autowired SimulatorView view; // SESSION SCOPE
@RequestMapping("/progressStream")
public double progressStream() {
return view.getProgress().progressStream();
}
@RequestMapping("/progressInvoice")
public double progressInvoice() {
return view.getProgress().progressInvoice();
}
}
我的JavaScript代码片段如下所示:
function registerSimulationRunEvent() {
// this is the id of the form
$("#simulatorForm").submit(function(e) {
handleSimulationStarted();
var url = location.protocol + "//" + location.host + "/fdsclient/simulation/view";
$.ajax({
type: "POST",
url: url,
data: $("#simulatorForm").serialize(), // serializes the form's elements.
success: function(data) { handleSimulationFinished(); },
error: function(xhr, error) { handleSimulationError(); }
});
e.preventDefault(); // avoid to execute the actual submit of the form.
});
}
function handleSimulationStarted() {
replaceResultPanelRunning(); // THYMELEAF FRAGMENT EXCHANGE
}
function handleSimulationFinished() {
stopResultPanelAnimation(); // STOP PROGRESS BAR ANIMATION
replaceResultPanelSimulated(); // EXCHANGE THYMELEAF FRAGMENT
}
function handleSimulationError() {
stopResultPanelAnimation();
replaceResultPanelError();
}
function replaceResultPanelRunning() {
var url = // URL;
$("#resultDiv").load(url);
startResultPanelAnimation();
}
// ANIMATION
var animationInterval = null;
function startResultPanelAnimation() {
animationInterval = setInterval(animateResultPanel,4000);
}
function stopResultPanelAnimation() {
clearInterval(animationInterval); // stop the interval
}
function animateResultPanel() {
$("#simulatorProgressLabel").animate({opacity: '0.4'}, "slow");
$("#simulatorProgressLabel").animate({opacity: '1.0'}, "slow");
}
我知道在rest服务中使用session scope是一件坏事,但我还不知道什么是好的、简单的解决方案。另一方面,目前不同的浏览器可以独立模拟,但进度条并不总是起作用(特别是当第一次触发大多不起作用时)。IE11只有在开发者工具被激活时才起作用。在进行过程中停用工具时,进度条停止增长
我想知道的是,当使用带有springmvc和Thymeleaf的模板引擎来触发流程并通过Javascript(如JQUery)显示进度状态时,一个好的解决方案是什么样子的。先谢谢你 我使用Jquery AJAX POST提交做了类似的事情。你可以这样做。这将以JSON格式向控制器提交POST请求,并等待响应。在此等待期间,可以显示进度UI组件
//Start Progress display
function setStatistic(){
var data = JSON.stringify(//build your ChecksDto)
if (data) {
$.ajax({
url : '/view',
headers : {
'Content-Type' : 'application/json'
},
method : 'POST',
dataType : 'json',
data : data,
success : function(data) {
if (data.status == 200) {
// Stop Progress display
// Handle success status
}
},
error : function(xhr, status, error) {
// Stop Progress display
// Handle errors here
}
});
}
}
您还需要更改控制器方法以检索ajax请求,如下所示:
@ResponseBody
@PostMapping("/view")
public String run(@RequestBody ChecksDto checksWrapper, Model model) throws InterruptedException, ExecutionException
至少我在另一页找到了解决方案。神奇的是将ajax缓存设置为false
$.ajaxSetup ({
// Disable caching of AJAX responses */
cache: false
});
谢谢你的回复,我对我的问题进行了编辑以使其更清楚。我的控制器用进度条html片段替换thymeleaf片段。在片段上有由JavaScript控制的进度条,这些进度条经常通过ajax询问后端的状态。后端应该只提供会话对象的状态,这意味着如果必须创建会话(两个浏览器窗口),它们应该独立工作。在FireFox和Chrome上,它主要起作用,但IE只有在开发者工具打开时才起作用。我的问题是如何通过Rest访问会话对象(如果有的话)。