使用Spring MVC Controller将Java对象传递给AngularJS

时间:2015-06-23 14:08:27

标签: java angularjs spring spring-mvc

我正在使用AngularJSSpring MVC开发一个应用程序。一切正常,但我的对象nullSpring Controller,因此它返回null。我只是不知道如何使AngularJS填充对象。

我会在这里发布代码。

  

AngularJS

  <%@taglib prefix='c' uri='http://java.sun.com/jsp/jstl/core'%>
<%@taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<!doctype html>
<html>
<head>
<title>Settings</title>
<link rel="stylesheet" href="css/main.css">
<link rel="stylesheet" href="css/workflow.css">
<link rel="stylesheet" href="css/upload.css">
<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css"> 
<link rel="shortcut icon" href="images/logo-small.png" />

</head>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<script type="text/javascript">
    var app = angular.module('formSubmit', []);

    app.controller('FormSubmitController', function($scope, $http) {

            $scope.formData = {};
            $scope.headerText = 'AngularJS Post Form Spring MVC example: Submit below form';
            $scope.submit = function() {

                var formData = {
                        "name1" : "nome 1",
                        "name2" : "nome 2",
                        "name3" : "nome 3",
                };

                var response = $http.post('http://localhost:8080/fire-fatca-web/SubmitMock.html', formData); //passing mockForm
                response.success(function(data, status, headers, config) {      
                    /* alert('Success!' + JSON.stringify({
                         data: $scope.formData //used formData model here 
                    }) ); */
                    $scope.mockForm = data;
                    $scope.formData.push(data);
                });
                response.error(function(data, status, headers, config) {
                    alert("Exception details: " + JSON.stringify({
                        data: $scope.formData //used formData model here
                    }));
                })

            };
        });
</script>
<style>        
input.ng-invalid {    
    border: 2px red solid;
    }
</style>
<body data-ng-app="formSubmit">

    <div class="container">
    <div class="col-sm-8 col-sm-offset-2">


    <form data-ng-submit="submit()" name="myForm" data-ng-controller="FormSubmitController">
        <!-- novalidate prevents HTML5 validation since we will be validating ourselves -->
        <table border="1">
            <tr>
                <td colspan="2">
                    <label>Name Line 1:</label>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <div class="form-group">
                        <input type="text" name="name1" class="form-control" data-ng-model="formData.name1" required ng-Maxlength="40">
                        <p ng-show="myForm.name1.$error.required" class="help-block">Name is required.</p>
                        <p ng-show="myForm.name1.$error.maxlength" class="help-block">Maximum 40 characters</p>
                    </div>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <label>Name Line 2:</label>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <div class="form-group">
                        <input type="text" name="name2" class="form-control" data-ng-model="formData.name2" ng-Maxlength="40">
                        <p ng-show="myForm.name2.$error.maxlength" class="help-block">Maximum 40 characters</p>
                    </div>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <label>Name Line 3:</label>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <div class="form-group">
                        <input type="text" name="name3" class="form-control" data-ng-model="formData.name3" ng-Maxlength="40">
                        <p ng-show="myForm.name3.$error.maxlength" class="help-block">Maximum 40 characters</p>
                    </div>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <h4>You submitted below data through post:</h4>
                    <pre>Form data ={{formData}}</pre>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <p>Response:  {{mockForm}}</p>      
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <!-- SUBMIT BUTTON -->
                    <button type="submit" class="btn btn-primary" ng-disabled="myForm.$invalid">Submit</button>
                </td>
            </tr>
        </table>
    </form>
    </div>
    </div>

</body>
</html>
  

Spring Controller

  @Controller
public class MockController {

    private Logger log = LoggerFactory.getLogger(MockController.class);

     @RequestMapping("/mock")
        public String getSettingsPage(){
            return "mock";
        }   

      @RequestMapping(value = "/SubmitMock", method = RequestMethod.POST)
        public @ResponseBody String getMock(@RequestBody MockForm mockForm){ 

          StringBuilder reponseData = new StringBuilder();
            reponseData.append("Name1: "+ mockForm.getName1()+" ");
            reponseData.append("Name2: "+ mockForm.getName2()+" ");
            reponseData.append("Name3: "+ mockForm.getName3());

            log.debug(reponseData.toString());

            return reponseData.toString();

        }

回归远离:Name1: null, Name2: null and Name3: null

我知道我需要使用mockForm填充formData。有人建议我这样做:

var response = $http.post('http://localhost:8080/fire-fatca-web/SubmitMock.html', {mockform: formData});

但是当我在Controller Stops响应之上使用那段代码时,我不知道为什么,也许是Sintaxe Error,我不知道。有什么帮助吗?

编辑:

  

MockForm.java

package com.opessoftware.beans;

public class MockForm {

    private String name1;
    private String name2;
    private String name3;

    public String getName1() {
        return name1;
    }
    public void setName1(String name1) {
        this.name1 = name1;
    }
    public String getName2() {
        return name2;
    }
    public void setName2(String name2) {
        this.name2 = name2;
    }
    public String getName3() {
        return name3;
    }
    public void setName3(String name3) {
        this.name3 = name3;
    }

}

编辑2:

最后我解决了这个问题。我的ng-data-model是formData.name1,formData.name2和formData.name3。我更改了name1,name2和name3。问题解决了。谢谢大家!

0 个答案:

没有答案