2014-02-21 39 views
0

我收到以下錯誤並想知道如何解決這個不能接受和JSP呈現視圖從數據庫調用的結果:Spring MVC的 - - 從JDBC JSON響應根據要求「接受」頭

控制器:

@Controller 
public class JsonController { 

    private static final String EMPLOYEE_DROP_DOWN = "employeeRequest"; 
    @Autowired 
    EmployeeDAO employeeDAO; 
    @Autowired 
    EmployeeDAOImpl employeeDAOImpl; 
    @Autowired 
    ApplicationContext context; 

    @RequestMapping(value = "/employees/json/dropdown.htm", method = RequestMethod.GET, headers = "Accept=application/xml, application/json", produces = { 
      "application/json", "application/xml" }) 
    public String getEmployeeDropDown(Map<String, Object> model) { 
     Employee employees = new Employee(); 
     model.put("employees", employees); 

     List<String> employeeList = new ArrayList<String>(); 

     List<Employee> details = employeeDAO.getEmployeeRelationshipList(); 
     for (Employee record : details) { 
      employeeList.add(record.getLastName()); 
     } 
     model.put("employeeList", employeeList); 

     return EMPLOYEE_DROP_DOWN; 
    } 


    @RequestMapping(value="/employees/json/data.htm", method = RequestMethod.GET, produces = "application/json") 
    public @ResponseBody Employee post(@ModelAttribute Employee employees) { 
     System.out.println(employees.getFirstName() + " " + employees.getLastName()); 
     return employees; 
    } 

} 

JSP視圖頁面:

<%@ include file="/WEB-INF/views/includes/taglibs.jsp" %> 
<html> 
<head> 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <%-- for responsive side --%> 
    <script src="<c:url value="../../resources/js/bootstrap.js" />"></script> 

    <title>Employee Information</title> 
    <!--[if IE]> 
    <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script><![endif]--> 
</head> 

<body> 
<div class="container"> 
    <p align="center">Please choose the last name and provide the corresponding first name...</p> 
    <form:form method="GET" action="/spring-jdbc/employees/json/data.htm" modelAttribute="employees"> 
     <table align="center" cellpadding="10"> 
      <tr> 
       <td>Employee Last Name:</td> 
       <td> 
        <form:select path="lastName"> 
         <form:option value="NONE" label=" select the employee last name "/> 
         <form:options items="${employeeList}"/> 
        </form:select> 
       </td> 
      </tr> 
      <tr> 
       <td>Employee First Name:</td> 
       <td> 
        <form:input type="text" path="firstName"/> 
       </td> 
      </tr> 
      <td> 
       <input type="submit" value="Save Changes"/> 
      </td> 
     </table> 
    </form:form> 
</div> 

<script> 
    $(document).ready(function(){ 
     sendAjax(); 
    }); 

    function sendAjax(){ 
     $.ajax({ 
      url: "/spring-jdbc/employees/json/data.htm", 
      type: 'POST', 
      dataType: 'json', 
      data: "{\"First Name\": \""+lastName+"\", \"Last Name\": \""+firstName+"\"}", 
      mimeTypes: 'application/json', 
      contentType: 'application/json', 
      success: function(data){ 
       alert(data.firstName + " " + data.lastName); 
      }, 
      error:function(data, status, er){ 
       alert("error: "+data+" status: "+status+" er:"+er); 
      } 
     }); 
    } 
</script> 
</body> 
</html> 

如何在JSON響應中獲得結果? 我有Jackson-mapper-asl,以及pom.xml文件中的其他必需的依賴關係。

結果我想獲得:

data: { 
"First Name": name, 
"Last Name": name 
} 

例如:我把表單域的形成是給我的錯誤。

+0

您如何提出要求?你是否正確指定accept頭文件? –

+0

你正在做一個'POST',這對錶單提交是正確的,但是控制器只響應一個'GET'。另外,你的api會搞砸了 - 文件擴展名意味着一個html響應,但你只想返回json或者xml。 –

回答

0

您正在做POST,這是正確的表單提交,但控制器只響應GET。另外,你的api會搞砸了 - 文件擴展名意味着一個html響應,但你只想返回json或者xml。其餘的代碼也真的搞砸了 - 但你沒有問過這個問題。

+0

不確定代碼混亂。但getEmployeeDropDown方法從數據庫中獲取數據的下拉列表。並且在表單字段中,如您所見,我有一個輸入字段,可以在下拉菜單時手動輸入。所以我現在的問題是:我怎樣才能改進這段代碼,並獲得JSON響應,從那個下拉列表中獲取lastName,並且手動輸入firstName?感謝您的答覆。 – Thunder