如何在java Web应用程序中创建一个没有提交按钮的搜索字段的文本字段

时间:2016-12-02 14:09:13

标签: java spring forms spring-mvc

我在spring mvc中编写了一个小的java webapplication。我有一个网页,列出了mysql db中的一个表。

webpage displaying information from db

现在我想要一个测试盒,它可以在没有按钮的情况下点击键盘进行搜索操作。

代码如下

clientlist.jsp

<div class="container-fluid">
      <div class="row">
        <div class="col-sm-3 col-md-2 sidebar">
          <ul class="nav nav-sidebar">
            <li class="active"><a href="#">Enterprise Applications <span class="sr-only">(current)</span></a></li>
            <li><a href="#">Application Servers</a></li>
            <li><a href="#">WebServers</a></li>
            <li><a href="#">Clusters</a></li>
          </ul>

        </div>
        <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
          <h1 class="page-header">Clients</h1>

          <div class="table-responsive">

            <table class="table table-striped">
               <tr>
                <td><%@ include file="/WEB-INF/jsp/globalfilter.jsp" %></td>
               </tr>
               <thead>
                 <tr>
                    <th>Application Name</th>
                    <th>Client Type</th>
                    <th>Client Name</th>
                    <th>Hostname</th>
                    <th>Environment</th>
                 </tr>
               </thead>
               <tbody>
                 <c:forEach items="${clientList}" var="client">
                    <tr>
                        <td>${client.applicationname}</td>
                        <td>${client.clienttype}</td>
                        <td>${client.clientname}</td>
                        <td>${client.hostname}</td>
                        <td>${client.envtype}</td>
                    </tr>
                 </c:forEach>
               </tbody>

            </table>
          </div>
        </div>
      </div>
    </div>

globalfilter.jsp,其中包含搜索表

<body>
    <form:form action="client.do" method="POST" commandName="client">
      <table>
        <tr>
            <td><input type="text" name="clientname" /></td>
            <td colspan="2">
                <input type="submit" name="action" value="Search" />
            </td>
        </tr>
    </table>
    </form:form>
</body>

ClientController.java

@RequestMapping("/clientlist")
    public String clientPage(Map<String, Object> map){
        Client client = new Client();
        map.put("client", client);
        map.put("clientList", clientService.getAllClient());
        return "clientlist";
    }

@RequestMapping(value="/client.do", method=RequestMethod.POST)
    public String doActions(@ModelAttribute Client client, BindingResult result, @RequestParam String action, Map<String, Object> map){
        Client clientResult = new Client();
        boolean isSearch = false;
        switch(action.toLowerCase()){
        case "add":
            clientService.add(client);
            clientResult = client;
            break;
        case "edit":
            clientService.edit(client);
            clientResult = client;
            break;
        case "delete":
            clientService.delete(client.getClientname());
            clientResult = new Client();
            break;
        case "search":
            Client searchedClient = clientService.getClient(client.getClientname());
            clientResult = searchedClient!=null ? searchedClient : new Client();
            isSearch = true;
            break;
        }

        if(isSearch) {
            List<Client> list = new ArrayList<Client>();
            list.add(clientResult);
            map.put("clientList", list);
        } else {
            map.put("clientList", clientService.getAllClient());
            map.put("client", clientResult);
        }

        return "clientlist";
    }

现在我不需要提交按钮“搜索”来执行操作,如何通过按Enter键来实现搜索结果。

1 个答案:

答案 0 :(得分:0)

这实际上与Java无关,只与标准的浏览器行为有关。

只需删除提交按钮即可。默认情况下,表单在Enter上提交。