Java Jsp视图页未在Spring引导中呈现。如何解决?
我试图发出一个ajax请求,向我的spring boot发送一个值。但是,一旦我合并了ajax调用并成功地将值传递给java,它就不会查看Java Jsp视图页未在Spring引导中呈现。如何解决?,java,ajax,spring,spring-boot,Java,Ajax,Spring,Spring Boot,我试图发出一个ajax请求,向我的spring boot发送一个值。但是,一旦我合并了ajax调用并成功地将值传递给java,它就不会查看jsp页面。我相信问题来自于方法本身,但我不能完全确定。同样,我有传递的值,但是每当我使用ModelAndView或Model时,它似乎不会相应地更改页面 JSP: <script> var chaptersTest = ${ chapters }; var totalChapters = chaptersTest
jsp
页面。我相信问题来自于方法本身,但我不能完全确定。同样,我有传递的值,但是每当我使用ModelAndView
或Model
时,它似乎不会相应地更改页面
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>
@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;
}
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;
}
对象:
<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>
@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;
}
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;
}
在控制器中,删除
@ResponseBody
注释,并将返回类型更改为ModelAndView
。在ajax代码中将dataType
更改为html
如果未正确配置视图,则在应用程序.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();
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;
}
不,我们按照您指定的方式进行了操作,但仍然无法查看该页面。它只显示
System.out.println()
您是否正确配置了视图?例如,设置视图的前缀和后缀。Gautum否,即使我尝试了您建议的方式,它仍然无法呈现。让我编辑我的答案。我还将添加属性文件。Gautum Ok,但我仍然面临相同的问题。