2016-04-28 81 views
0

我正在嘗試使用Spring JDBC搜索記錄並將其顯示在JSP中。 我現在在瀏覽器上面臨下面的錯誤。如何使用Spring JDBC檢索記錄

異常消息: page

我的JSP: search.jsp的:

<div class="container"> 
 
    <div class="col-xs-12 col-sm-8 col-md-6 col-sm-offset-2 col-md-offset-3"> 
 
     <div class="row"> 
 
      <div class="col-xs-12 col-sm-6 col-md-6"> 
 
       <div class="form-group"> 
 
        <form action="/SpringMail/searchresults" method="get"> 
 
         <input type="text" name="searchRecord" id="search_record" class="form-control input-lg" placeholder="Enter email to search" tabindex="1" required="required"> 
 
         <div class="col-xs-12 col-md-6"><input type="submit" value="Search" class="btn btn-primary btn-block btn-lg search" tabindex="4"></div> 
 
        </form> 
 
       </div> 
 
      </div> 
 
     </div> 
 
    </div> 
 
</div>

Searchresult.jsp:它有一個表。所以我只是包括那個代碼。

<table class="table"> 
 
    <thead> 
 
     <tr> 
 
      <th>#</th> 
 
      <th>Firstname</th> 
 
      <th>Lastname</th> 
 
      <th>DisplayName</th> 
 
      <th>DateOfBirth</th> 
 
      <th>Email</th> 
 
      <th>Password</th> 
 
    \t  <th>Contact</th> 
 
    \t  <th>Skills</th> 
 
     </tr> 
 
    </thead> 
 
    <tbody> 
 
    <% 
 
     List students = (List)request.getAttribute("searchresult"); 
 
     Iterator stdIter = students.iterator(); 
 
     
 
     while(stdIter.hasNext()) { 
 
      Student s = (Student) stdIter.next(); 
 
    %> 
 
     <tr> 
 
      <td><% s.getFirstName(); %></td> 
 
      <td><% s.getLastName(); %></td> 
 
      <td><% s.getDisplayName(); %></td> 
 
      <td><% s.getDateOfBirth(); %></td> 
 
      <td><% s.getEmail(); %></td> 
 
      <td><% s.getPassword(); %></td> 
 
      <td><% s.getContact(); %></td> 
 
      <td><% s.getStudentSkills(); %></td> 
 
     </tr> 
 
    <% } %> 
 
    </tbody> 
 
</table>

我的控制器:這個RegistrationController:

` // For Search 
@RequestMapping(value="/search", method=RequestMethod.GET) 
public ModelAndView getSearchForm() { 
    ModelAndView searchmv = new ModelAndView("search"); 
    return searchmv; 
} 

@RequestMapping(value="/searchresults/{email}", method=RequestMethod.GET) 
public ModelAndView searchResults(@PathVariable("email") String email,HttpServletRequest request) { 
    System.out.println("Inside Search method"); 
    ModelAndView searchResult = new ModelAndView("searchresult"); 
    request.setAttribute("searchresult", st.getStudent(email)); 
    return searchResult; 
} 
// For Search` 

getStudent(): 


    public List<Student> getStudent(String email) { 
    System.out.println("Inside getStudent() method"); 
    System.out.println("Entered email: " + email); 
    List studentList = new ArrayList(); 
    String sql = "select * from student where email = " + email; 
    studentList = jt.query(sql, new StudentRowMapper()); 
    return studentList; 
} 

我做這個動手上的第一次。請引導我採取正確的做法。

+0

我正確地得到search.jsp的,我給電子郵件在網頁中搜索和檢索從DB相同的記錄。但點擊輸入後我得到404。 – Bobby

+0

404意味着該頁面不存在於服務器上...檢查您的請求映射URL – Pras

+0

可能'ViewResolver'找不到您的JSP文件。檢查它們是否與'ModelAnd'View'中使用的名稱完全相同,並且它們位於正確的位置。 –

回答

0

我得到了答案。 方法:RegistrationController中的searchResults格式錯誤。 我修正了這個問題。

@RequestMapping(value="/searchresults", method=RequestMethod.GET) 
public ModelAndView searchResults(@RequestParam("searchRecord") String email, Map model) { 
    ModelAndView searchResult = new ModelAndView("searchresult"); 
    model.put("searchresult", st.getStudent(email)); 
    return searchResult; 
} 

感謝您的輸入