新增至angularjs。我相信我錯過了一些東西。 如果我使用angularjs的$http
指令訪問服務器,我無法呈現服務器在jsp頁面上發送的響應。SpingMVC + Angularjs + POST與jsp
angularcode
// Read button Handler
$scope.readAll=function(){
readTable($scope.clnfamilyArray);
//send data to server
testAddItem=function(){
$http({
'url' : 'http://localhost:9090/QuantumM/orbital/sendstatement/',
'method' : 'POST',
'headers': {'Content-Type' : 'application/json'},
'data' : $scope.clnfamilyArray
}).success(function(data){
console.log(data);
})
}//end of function
testAddItem();
}////end of readAll
彈簧代碼
@RequestMapping(value = "/sendstatement/", method = RequestMethod.POST)
public ModelAndView welcome(@RequestBody String map) {
ModelAndView model = new ModelAndView();
// BL
model.addObject("name",hTable.toHtml());
model.setViewName("test");
return model;
}
test.jsp的
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<h1>Gradle - Spring MVC Hello World</h1>
<h2>Hello ${name}</h2>
</body>
</html>
相反渲染test.jsp的的。我登陸同一頁面。但在控制檯上,我可以看到預期的輸出..
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<h1>Gradle - Spring MVC Hello World</h1>
<h2>Hello <table border=1><tr><td colspan=2>s</td></tr><tr><td colspan=1>f</td><td colspan=1>d</td></tr></table></h2>
</body>
</html>
有什麼我失蹤? 我想降落在test.jsp
不在我目前的呼叫頁面上...
哦。 但是在這種情況下,ModelAndView中的視圖在使用angularjs時將無用嗎? 如果沒有選項,那麼我必須打開單個頁面...... – sailor
就是這樣。你當然可以嘗試製作一堆單獨的單頁應用程序,但這不是AngularJS的真正意義。這將是很多額外的工作和資源,沒有任何好處。來自Java自己,我明白異步的Javascript/Angular的思維方式有多不同。下面是一個Plunk演示可能會有所幫助的更多信息:http://plnkr.co/edit/lv8Kyu?p=info –
@sailor我們一直使用SpringMVC將AngularJS添加到現有的基於Java/JSP的Web應用程序中。我們將Spring MVC控制器添加到提供JSON數據的Web應用程序中,然後使用它將交互式方面添加到應用程序中。該公式工作得很好。 –