Spring MVC + jQuery Datatables + JSON-Antwort

Ich habe eine Spring-MVC-Anwendung mit jQuery-Datatables zum Rendern der Daten.
Hier sind meine Klassen-und javascript-code

Employee.java

public class Employee {
    private int empId ;
    private String firstName;
    private String lastName ;

    //getters and setters
}

EmployeeResponse.java

public class EmployeeResponse {

    private List<Employee> empList ; 

    //getters and setters

}

EmployeeController.java

@Controller
@RequestMapping("/admin")
public class EmployeeController {

    @RequestMapping(value = "/get-all-employee", method = RequestMethod.POST, consumes = { MediaType.APPLICATION_JSON_VALUE })
    @ResponseBody
    public EmployeeResponse getAllEmployee(HttpServletRequest request) {
        EmployeeResponse response = new EmployeeResponse();
        try {

            response = getAllEmployeeList(); //returns response object

        } catch (Exception e) {
            logger.error("DCConsoleController::createNewStream exception: " + com.priceline.utils.StringUtils.getStackTrace(e));
            return null;
        }
        return response ; 
    }
}

emp.jsp

<div class="row">
    <table id="ldapStreamTable" class="table">
        <thead>
            <tr>
                <th>Emp Id</th>
                <th>First Name</th>
                <th>Last Name</th>
            </tr>
        </thead>
    </table>
</div>

emp.js

$(document).ready(function() {
    var table = $('#appTable').DataTable( {
        "sDom" : domSetting,
        "oLanguage" : {
            "sLengthMenu" : "_MENU_ records per page",
            "sSearch" : "<span class='add-on'><i class='icon-search'></i></span>Search Application:",
            "sZeroRecords" : "No matching records found",
        },
        "iDisplayLength" : 10,
        "aLengthMenu" : [
                [ 5, 10, 20, 25, 50, -1 ],
                [ 5, 10, 20, 25, 50, "All" ] ],
        "aaSorting" : [ [ 0, 'asc' ] ],
        'sAjaxSource': '{context}/admin/get-all-employee',
        [Pending here]
    });
});

Was sollte ich tun, um die Antwort als json und anwenden/verwenden render-Methoden für jede Spalte, die zum Rendern der Daten in die Tabelle?

[Anmerkung: wie es aussieht kann ich nicht verwenden aoColumns' mit mData für jede Spalte, da mein json-Antwort ist eine Liste von employee-Objekten]

Update - 1: Beispiel json unten

{"empList":[{"empId":3,"firstName":"Kiran","lastName":"Kumar"},{"empId":1,"firstName":"John","lastName":"Smith"},{"empId":0,"firstName":"Sachin","lastName":"Kumar"}]}
  • Haben Sie sah in mit Gson oder eine ähnliche Bibliothek?
  • Meine Antwort vom controller ist bereits im json-format mit Jackson-API. Was ich bin nicht in der Lage, herauszufinden, ist, wie zu binden, die json-Antwort, die eine Liste<Mitarbeiter - > Objekte, die in den Spalten auf der Benutzeroberfläche
  • Könntest du deinen post json Antwort?
  • aktualisiert die Frage mit Beispiel-json-Antwort.
InformationsquelleAutor jagamot | 2016-02-09
Schreibe einen Kommentar