问题描述
我正在尝试发出一个 ajax 请求,该请求将一个值发送到我的 Spring Boot.但是,一旦我合并了 ajax 调用并且它成功地将值传递给 java,它就不会查看 jsp
页面.我相信问题出在方法本身,但我不完全确定.我再次传递了值,但是每当我使用 ModelAndView
或 Model
时,它似乎不会相应地更改页面.
JSP:
var 章节测试 = ${ 章节 };var totalChapters = ChaptersTest.length;无功代码块;for (var i = 0; i < totalChapters; i++) {代码块 ='<div class="col-md-4">'+' <a class="chapter_link" data-link="' + ChaptersTest[i] + '" style="text-decoration: none; color: white"><div class="box warning">'+' <h3>'+ ChaptersTest[i] + '</h3>'+'</div></a></div>';$("#chapterArea").append(codeBlock);}//点击的链接$('.chapter_link').click(function () {console.log("你的功能被点击了");做某事();});函数 doSomething() {var search2 = {章节":莱利"}$.ajax({类型:POST",contentType: '应用程序/json;字符集=utf-8',数据类型:'json',网址:/ajax",数据:JSON.stringify(search2),成功:功能(结果){console.log("有点效果");}});}
Java:
@RequestMapping(value = "/ajax", method = RequestMethod.POST)public @ResponseBody String sectionView(@RequestBody BEntity benny, HttpServletRequest request) {String temp = benny.getChapter();ModelAndView secView = new ModelAndView();试试{secView.setViewName("viewsections.jsp");//不是发送jsp来改变页面} 捕获(异常 e){secView.setViewName("error.jsp");}System.out.println("测试1");返回秒视图;}
对象:
公共类 BEntity {私人字符串搜索;私人字符串章节;私人字符串部分;公共字符串 getSection() {返回部分;}公共字符串 getChapter() {返回章节;}公共字符串 getSearch() {返回搜索;}@覆盖公共字符串 toString() {return "这是搜索过的:" + search;}在您的控制器中,移除 @ResponseBody
注释并将返回类型更改为 ModelAndView
.将 ajax 代码中的 dataType
更改为 html
.
如果您没有正确配置视图,请在您的 application.properties
中将前缀和后缀设置为:
spring.mvc.view.prefix:/views/spring.mvc.view.suffix: .jsp
spring.mvc.view.prefix 是您的 jsp 文件所在文件夹的路径.
@RequestMapping(value = "/ajax", method = RequestMethod.POST)public ModelAndView sectionView(@RequestBody BEntity benny, HttpServletRequest request) {String temp = benny.getChapter();ModelAndView secView = new ModelAndView();试试{secView.setViewName("viewsections");//不是发送jsp来改变页面} 捕获(异常 e){secView.setViewName("错误");}System.out.println("测试1");返回秒视图;}
I am trying to make an ajax request that sends a value to my spring boot. However, once I incorporate the ajax call and it successfully passes the value to java it does not view the jsp
page. I believe the issue is from the method itself, but I am not entirely sure. Again I have the values being passed, but whenever I use ModelAndView
or Model
it seems to not change the page accordingly.
JSP:
<script>
var chaptersTest = ${ chapters };
var totalChapters = chaptersTest.length;
var codeBlock;
for (var i = 0; i < totalChapters; i++) {
codeBlock =
'<div class="col-md-4">' +
' <a class="chapter_link" data-link="' + chaptersTest[i] + '" style="text-decoration: none; color: white"><div class="box warning">' +
' <h3>' + chaptersTest[i] + '</h3>' +
' </div></a></div>';
$("#chapterArea").append(codeBlock);
}
//clicked links
$('.chapter_link').click(function () {
console.log("YoUR fUNCtION IS cLICKED");
doSomething();
});
function doSomething() {
var search2 = {
"chapter": "riley"
}
$.ajax({
type: "POST",
contentType: 'application/json; charset=utf-8',
dataType: 'json',
url: "/ajax",
data: JSON.stringify(search2),
success: function (result) {
console.log("It somewhat worked");
}
});
}
</script>
Java:
@RequestMapping(value = "/ajax", method = RequestMethod.POST)
public @ResponseBody String sectionView(@RequestBody BEntity benny, HttpServletRequest request) {
String temp = benny.getChapter();
ModelAndView secView = new ModelAndView();
try {
secView.setViewName("viewsections.jsp");
//It is not sending the jsp to change the page
} catch (Exception e) {
secView.setViewName("error.jsp");
}
System.out.println("Test 1");
return secView;
}
Object:
public class BEntity {
private String search;
private String chapter;
private String section;
public String getSection() {
return section;
}
public String getChapter() {
return chapter;
}
public String getSearch() {
return search;
}
@Override
public String toString() {
return "This was searched: " + search;
}
In your controller,remove @ResponseBody
annotation and change the return type to ModelAndView
. Change dataType
to html
in your ajax code.
If you have not configured view properly then, in your application.properties
set the prefix and suffix as:
spring.mvc.view.prefix: /views/
spring.mvc.view.suffix: .jsp
spring.mvc.view.prefix is the path of the folder where your jsp files reside.
@RequestMapping(value = "/ajax", method = RequestMethod.POST)
public ModelAndView sectionView(@RequestBody BEntity benny, HttpServletRequest request) {
String temp = benny.getChapter();
ModelAndView secView = new ModelAndView();
try {
secView.setViewName("viewsections");
//It is not sending the jsp to change the page
} catch (Exception e) {
secView.setViewName("error");
}
System.out.println("Test 1");
return secView;
}
这篇关于Jsp 视图页面不会在 Spring Boot 中呈现.如何解决?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!