我試圖在jsp中設置一個簡單的表單,該表單會將提交的URL中的搜索查詢。需要jsp表單向特定路徑提交查詢
這是我到目前爲止有:
<form action="search/" method="get" onsubmit="this.action+=this.q.value.trim();return true">
<input type="text" id="q">
<input type="submit" value="go">
</form>
這在隨機配備我的web應用程序的每一頁上一個jsp和它的作品,當我在應用程序的根目錄。例如,如果我最初加載「http:// localhost:8080/MyApp /」,然後在搜索表單中輸入「123456」,它會帶我到「http:// localhost:8080/MyApp/search/123456」,它是我想要的。但是,從該頁面,如果我再次搜索(例如「654321」),它將轉到「http:// localhost:8080/MyApp/search/search/654321」,這對我不起作用。我需要將搜索/ {query}附加到應用路徑的根目錄,而不管我當時可能在哪個頁面上。
我想避免使用JavaScript,如果可能的話,JSTL雖然我很好。
任何建議將不勝感激!
爲什麼使用+ =?你可以嘗試this.action + this.q.value.trim();返回true – Don
如果我把「=」取出不起作用。它只是把我帶到「../search」而不把查詢放在URL中。 –