Java 为什么ajax解析不允许SpringMVC控制器重定向jsp页面?
我一直在使用Java 为什么ajax解析不允许SpringMVC控制器重定向jsp页面?,java,json,spring,jsp,spring-mvc,Java,Json,Spring,Jsp,Spring Mvc,我一直在使用ModelAndView作为控制器中的对象类型来处理方法,以便与前端html表单进行数据绑定。例如: @RequestMapping(value = "/postSth", method = RequestMethod.POST) public ModelAndView postSomething(@RequestParam("name") String name){ ModelAndView model = new ModelAndView("displayPage"
ModelAndView
作为控制器中的对象类型来处理方法,以便与前端html表单进行数据绑定。例如:
@RequestMapping(value = "/postSth", method = RequestMethod.POST)
public ModelAndView postSomething(@RequestParam("name") String name){
ModelAndView model = new ModelAndView("displayPage");
model.addObject("name", name);
return model;
}
此方法将允许我绑定名称
,并且我可以在POST
方法之后将名称
显示到displayPage.jsp
displayPage
是JSP
页面名称,使用Spring框架中的InternalResourceViewResolver
最近我一直在使用jQueryajax
函数将数据传递给我的控制器,我正在研究如下方法:
@RequestMapping(headers = "Content-Type=application/json", value = "/postSth", method = RequestMethod.POST)
@ResponseStatus(value = HttpStatus.OK)
public ModelAndView postSomething(@RequestBody String name){
ModelAndView model = new ModelAndView("displayPage");
model.addObject("name", name);
return model;
}
我注意到我可以用这个控制器成功地抓取JSON
字符串,但是这个方法不会将我的网页重定向到displayPage
,使用.addObject
的数据绑定不再起作用
为什么它不起作用?如何将其更改为仍然允许我指向
displayPage.jsp
?我知道我可以在前端使用javascript进行重定向,但这不是我想要做的。我理解您在触发ajax调用时的需求,但不是将数据输出加载到当前jsp,而是需要在新jsp中填充它
我也有类似的需求,我使用jquery load()来实现它。对我来说效果很好。基本上它就像一个三步的过程
@RequestMapping(headers = "Content-Type=application/json", value = "/postSth", method = RequestMethod.POST)
@ResponseStatus(value = HttpStatus.OK)
public String postSomething(@RequestBody String name){
return model;
}
<div id='currentContent'>
<!--Existing content -->
</div>
var ajaxResult = '';
$.ajax({
type : POST,
url: "postSth",
async:false}).then(function(data){
//DIV ID here would be the ID which you need to have in your page to append the HTML content.
//if you want to completely reload the page then use the id where your current contents are displayed
//Inside your somenewjsp.jsp you can write your jquery codes to access the variable ajaxResult on document ready
ajaxResult = data;
$('#currentContent').load('/somenewjsp.jsp');
});
});
@RequestMapping(headers=“Content Type=application/json”,value=“/postsh”,method=RequestMethod.POST)
@ResponseStatus(值=HttpStatus.OK)
公共字符串postSomething(@RequestBody String name){
收益模型;
}
var ajaxResult='';
$.ajax({
类型:POST,,
网址:“postsh”,
async:false})。然后(函数(数据){
//这里的divid是页面中附加HTML内容所需的ID。
//如果要完全重新加载页面,请使用显示当前内容的id
//在somenewjsp.jsp中,您可以编写jquery代码来访问变量ajaxResult on document ready
ajaxResult=数据;
$('#currentContent').load('/somenewjsp.jsp');
});
});
请分享您对这种方法的结果。我不理解您的问题。如果您使用ajax发出请求,那么ajax请求的响应将包含由
displayPage
呈现的html。您认为这种方法不起作用是什么意思?输出是否与displayPage
的内容不同?您是否收到错误响应?您是否收到了其他类型的响应?@SotiriosDelimanolisdisplayPage
是我在POST
之后试图重定向到的jsp
页面,换句话说,当使用ajax
时,它会停止重定向到displayPage.jsp
,该重定向通常在没有ajax的情况下工作。您一直使用术语redirect,但该术语的含义完全不同。您的意思是一个ServletRequestDispatcher
forward,它只是呈现JSP的HTML并将其写入响应。如果您打印从ajax中发出的请求中收到的响应内容,您将看到相同的内容。@SotiriosDelimanolis我认为redirect不是正确的词,您的回答是正确的,我理解我正在获得响应的内容。我想知道的是,为什么在执行ajax时,RequestDispatcher
不再呈现displayPage.jsp
?