[英]Jsp view page is not rendered in Spring boot. How to solve it?
我正在嘗試發出一個 ajax 請求,該請求向我的 Spring Boot 發送一個值。 但是,一旦我合並了 ajax 調用並且它成功地將值傳遞給 java,它就不會查看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;
}
在您的控制器中,刪除@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();
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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.