spring mvc中的搜索页面不起作用

时间:2016-10-02 11:46:39

标签: spring jsp spring-mvc

我创建了简单的mvc CRUD。除搜索页面外,一切正常。

我得到的错误如下:

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查询:

public String searchname(String name){

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

不知道出了什么问题。 帮助我。

1 个答案:

答案 0 :(得分:0)

为了让jsp使用bean,你的控制器方法应该在你试图利用的jsp中返回数据     永远不会从控制器方法{String.id}传递的{emp.id}search1。因此您需要将bean添加到模型中     像这样返回页面。

 @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 bean。     {emp.id}     删除{String.id}部分。在searchemp.jsp内部无效。