【问题标题】:Search page in spring mvc doesn't workspring mvc中的搜索页面不起作用
【发布时间】:2017-02-10 12:23:30
【问题描述】:

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

我得到如下错误:

HTTP 状态 500 - 请求处理失败;嵌套异常是 org.springframework.dao.EmptyResultDataAccessException: Incorrect result size: expected 1, actual 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>  

jdbc模板查询:

公共字符串搜索名称(字符串名称){

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

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

【问题讨论】:

    标签: spring jsp spring-mvc


    【解决方案1】:

    为了让 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 里面是行不通的。

    【讨论】:

    • 感谢您的帮助。它可以工作。现在搜索页面已连接,但我正在获取数据库中的所有记录。如何仅搜索特定记录。
    • 我将 searchemp.jsp 中的内容更改为 并将控制器中的内容更改为 @RequestMapping(value="/jsp/searchemp /{id}") public ModelAndView search(@PathVariable int id){ return new ModelAndView("searchemp","id",id); } 但它显示空记录。
    • 你不能遍历${id}。它不是一个集合,它只是一个 bean。删除每个部分。在searchemp.jsp 中,您应该使用列表而不是单个对象。看看它返回list 的搜索方法。
    • 为了代表${id}你可以使用${id}或者如果bean包含任何属性然后使用${id.propName}
    • 已编辑帖子请看,纠正了之前的错误,但仍然出现错误。
    猜你喜欢
    • 1970-01-01
    • 2014-05-30
    • 1970-01-01
    • 1970-01-01
    • 2022-07-21
    • 1970-01-01
    • 2011-11-13
    • 2023-03-10
    • 1970-01-01
    相关资源
    最近更新 更多