2016-10-02 27 views
0

我創建了簡單的mvc CRUD。除搜索頁面外,所有的工作都正常在春季mvc的搜索頁面不起作用

即時得到errorr如下:

HTTP狀態500 - 請求處理失敗;嵌套異常是org.springframework.dao.EmptyResultDataAccessException:不正確的結果的大小:預期的1,實際0

控制器頁是:

@RequestMapping(value="/search") 
    public ModelAndView search(){ 
    return new ModelAndView("search","command", new Emp()); 
     } 

    @RequestMapping(value="/jsp/searchemp",method = RequestMethod.POST)  
    public ModelAndView search1(@ModelAttribute("name") Emp emp){  
     String name = null; 
     name = dao.searchname(name); 
    return new ModelAndView("searchemp","name",name); 
    } 

search.jsp的:

<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>  
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>z  
<html> 
<head> 

    <meta charset="utf-8"> 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> 
    <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> 

    <style> 

    label { 
    text-align: right; 
    clear: both; 
    float:left; 
    margin-right:15px; 
} 
.box{background-color: #e1e8f0;} 
body{background:#f0eceb;} 
</style> 
</head> 
<body> 
<div style=" left: 30%; top: 25%; position: absolute;"> 
    <div class="col-sm-12 col-sm-offset-3 box " > 

     <center> <h1>Add New Employee</h1> </center> 
     <form method="post" action="jsp/searchemp/" > 

      <div class="form-group"> <div class="col-xs-7">  
      <label ><h5>Name :</h5></label>  
      <input name="name" class="form-control" placeholder="Enter Name of the employee" /> 
      <button type="submit" value="Save" class="btn btn-primary btn-lg">Search</button> 
     </div></div> 


     </form> 
    </div> 
</div> 
    </body> 
    </head> 

Searchemp.jsp :

<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>  
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>  

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <title>Bootstrap Example</title> 
    <meta charset="utf-8"> 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> 
    <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> 
</head> 
    <body> 

<div class="container"> 
    <h1>Employees List</h1> 
<table class="table table-hover" border="1" width="70%" cellpadding="2"> 
    <thead> 
     <tr><th>Id</th><th>Name</th><th>Salary</th><th>Designation</th><th>Edit</th><th>Delete</th></tr></thead> 

    <tbody><tr> 
    <td>${String.id}</td> 
    <td>${String.name}</td> 
    <td>${emp.salary}</td> 
    <td>${emp.designation}</td> 
    <td><a href="editemp/${emp.id}">Edit</a></td> 
    <td><a href="deleteemp/${emp.id}">Delete</a></td> 
    </tr> </tbody> 

    </table> 
    <br/> 
    <a href="empform">Add New Employee</a> 

jdbctemplate查詢:

公共字符串searchname(字符串名稱){

String query = "select * from Emp99 where name=?"; 
    Object[] inputs = new Object[] {name}; 
    String empName = template.queryForObject(query, inputs, String.class); 
    return empName; 
} 

不知道哪裏出了問題。 幫助我。

回答

0

爲了JSP消費的豆,你的控制器方法應該返回數據,在你的JSP你正在試圖利用 {String.id}{emp.id}這是從來沒有從控制器方法search1。所以,你需要你的豆子添加到傳遞一個模型和 這樣返回頁面。

@RequestMapping(value="/jsp/searchemp",method = RequestMethod.POST)  
public String search1(@ModelAttribute("name") Emp emp,Model model){  
    model.addAttribute("emp",emp); //passing the model name and the bean , 
    //model.addAttribute("name",anotherBean); //you can add many attributes as you wish. 
    return "searchemp"; 
} 

現在searchemp.jsp裏面,你可以利用EMP豆這樣的。 {emp.id} 刪除{String.id}部分。在searchemp.jsp裏面不起作用。

+0

謝謝你的幫助。它的工作原理。現在搜索頁面已連接,但即時獲取數據庫中的所有記錄。我可以只搜索特定的記錄。 – Tom

+0

我在searchemp.jsp中將我的內容更改爲並在控制器中更改爲@RequestMapping(value =「/ jsp/searchemp/{id}」) 公共ModelAndView搜索(@PathVariable int id){ 返回新的ModelAndView(「searchemp」,「id」,id); }但它顯示空記錄。 – Tom

+0

你不能遍歷'$ {id}'。它不是一個集合,它只是一個bean。移除每個part.in'searchemp.jsp'您應該使用一個列表而不是單個對象。看看它返回一個'list'的搜索方法。 – Priyamal