我是 JSP 新手,我似乎不知道如何仅在用户单击按钮时运行代码。这是我的代码:
$
<form action="list_computers.jsp" method="post">
Search:
<input type="text" name="FromTextBox1"/>
<input type="submit" value="Search it!" >
<%
String TheSearch = (String)request.getParameter("FromTextBox1");
String GetIt = Searcher(TheSearch);
out.println(GetIt);
%>
</form>
Searcher() 是我在上面声明的函数。任何帮助将不胜感激。
最佳答案
你需要做类似的事情
if (request.getParameter("submit") != null) {
// do work here
}
您还需要为按钮命名
<input type="submit" value="Search it!" name="submit">
当用户点击(或按 Enter 键)时,request['submit']
将等于 “搜索它!”
我强烈建议将此逻辑移动到页面顶部,甚至更好地移动到 controller 。
关于java - 仅在 JSP 中单击按钮时运行脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10489132/