设计搜索框实现模糊查询

ssm项目
如何在jsp文件中获取搜索框的值作为模糊查询的参数进行搜索。

解决没有使用post传参,是利用url和@RequestParam注解进行前端向后端传参

模糊查询我一直都是在sql中使用,搜索框的值肯定要给到后台查库吧?如果是的话建议使用sql,如 select * from xx where xx like '%搜索框的值%'

post传参

在JSP文件中获取搜索框的值可以使用表单的方式提交数据到后台,然后在后台使用Java代码进行模糊查询。
以下是一个简单的示例:

  1. 在JSP页面中,创建一个表单,包含一个输入框和一个提交按钮:
    <form action="search.jsp" method="post">
     <input type="text" name="keyword">
     <button type="submit">搜索</button>
    </form>
    
  2. 在后台的servlet中,接收表单提交的数据,获取搜索框的值,并将其作为参数进行模糊查询:
    String keyword = request.getParameter("keyword");
    List<User> userList = userService.search(keyword);
    request.setAttribute("userList", userList);
    request.getRequestDispatcher("result.jsp").forward(request, response);
    
    在上面的代码中,request.getParameter("keyword")用于获取表单提交的数据,userService.search(keyword)则是进行模糊查询,最后将查询结果保存到request中并转发到result.jsp页面进行展示。
  3. 在result.jsp中展示查询结果:
    <c:forEach items="${userList}" var="user">
     <tr>
         <td>${user.id}</td>
         <td>${user.name}</td>
         <td>${user.age}</td>
     </tr>
    </c:forEach>