如何整理以下Play Framework路由?

时间:2012-01-13 02:09:19

标签: playframework

我正在尝试按照“介绍Play框架”中的示例进行操作。我已经在这里部署了工作

http://vivid-beach-8523.herokuapp.com/

当我搜索示例IPad时,它会出现“杂乱的”网址

http://vivid-beach-8523.herokuapp.com/search?search=ipad&submit=Search

我认为我的路由设置正确(在conf / routes中),因为当我使用'clean'URL时,它会得到正确的结果

http://vivid-beach-8523.herokuapp.com/search/IPad

我不知道如何让我的表单显示干净的URL。原始HTML是

<div id="searchdiv">
    <form action="@{Application.search()}" method="GET">
        <input type="text" id="search" name="search" />
        <input type="submit" id="submit" name="submit" value="Search" />
    </form>
</div>

Application.search如下

public static void search(String search, Integer page) {
    if (page == null) page = 1;
    SearchResults results = AuctionItem.search(search, page);
    render(results, page, search);
}

对我需要改变的任何帮助都非常感激。如果需要,路由文件如下所示

GET     /listing/create           Application.createAuctionItem
POST    /listing/create           Application.doCreateItem
GET     /listing/show/{id}             Application.show
GET     /listing/show             Application.show
GET     /search/{search}     Application.search
GET     /search     Application.search
GET     /                                       Application.index

1 个答案:

答案 0 :(得分:0)

您的提交按钮中不需要名称。否则,它将作为表单的参数发送。 只需加上

<input type="submit" id="submit" value="Search" />
我想,

应该没问题。 您需要将表单中的URL与您在路由中指定的URL匹配。 使用jQuery的一个可能的解决方案可能如下所示:

<script type="text/javascript>
$(document).ready(function(){
  $('#searchdiv form').submit(function(e){
    e.preventDefault();
    // not a good design though, once you change the route to Application.search, 
    // this will failed
    this.attr('action', '/search/'+$('input#search').val());
    this.submit();
  });
});
</script>