Java 是否可以使用ajax request to controller方法而不使用ResponseBy注释来返回重新编译的jsp?
我想在不重新加载页面的情况下重新加载部分页面 我将使用ajax请求来完成它 我找到的所有示例都使用Java 是否可以使用ajax request to controller方法而不使用ResponseBy注释来返回重新编译的jsp?,java,ajax,jsp,spring-mvc,Java,Ajax,Jsp,Spring Mvc,我想在不重新加载页面的情况下重新加载部分页面 我将使用ajax请求来完成它 我找到的所有示例都使用@ResponseBody注释,并且在客户端javaScript上处理xml或json 我的原因-获取htm片段并替换我页面的一部分 结构: bigJsp.jsp ... incude small.jsp ... 我想重新阅读small.jsp,并在我的页面上替换它,而无需重新加载页面 我找不到这样的例子。你能帮我吗?试着做以下事情: 1) 将include small.jsp替换为某个div &
@ResponseBody
注释,并且在客户端javaScript上处理xml或json
我的原因-获取htm片段并替换我页面的一部分
结构:
bigJsp.jsp
...
incude small.jsp
...
我想重新阅读small.jsp
,并在我的页面上替换它,而无需重新加载页面
我找不到这样的例子。你能帮我吗?试着做以下事情:
1) 将include small.jsp替换为某个div
<div id="smallContent"></div>
3) 创建小视图(small.jsp):
我的消息:${small_message}
4) 使用ajax获取div的html内容:
<html>
<body>
<script src="http://code.jquery.com/jquery-1.11.1.min.js" type="text/javascript"></script>
<script>
function reloadSmall() {
$.get("/small", function(response) {
$("#smallContent").html(response);
});
}
</script>
<div id="smallContent" style="background: green"></div>
<button onclick="reloadSmall()">Click me</button>
</body>
</html>
函数重载小(){
$.get(“/small”,函数(响应){
$(“#smallContent”).html(回复);
});
}
点击我
您应该有一个控制器方法返回model和view,例如
控制器将搜索名为small的视图,该视图位于默认位置,并带有默认后缀(取决于您的配置,例如/WEB-INF/views/small.jsp
)。然后,只需通过适当的ajax调用(例如通过jquery)调用该方法
$.ajax({
url:"/small",
dataType: "html",
type: "GET",
success: function (msg) {
$(".responseDiv").html(msg);// place your response to some div
}
});
msg是一个编译过的html吗?是的,它是设置响应的变量,添加了一个编辑
<html>
<body>
<script src="http://code.jquery.com/jquery-1.11.1.min.js" type="text/javascript"></script>
<script>
function reloadSmall() {
$.get("/small", function(response) {
$("#smallContent").html(response);
});
}
</script>
<div id="smallContent" style="background: green"></div>
<button onclick="reloadSmall()">Click me</button>
</body>
</html>
@RequestMapping(value = "/small", method = RequestMethod.GET)
public ModelAndView smallJSP() {
ModelAndView modelAndView = new ModelAndView("small");
return modelAndView;
}
$.ajax({
url:"/small",
dataType: "html",
type: "GET",
success: function (msg) {
$(".responseDiv").html(msg);// place your response to some div
}
});