【发布时间】:2015-05-27 16:00:23
【问题描述】:
我正在构建一个 Java EE 应用程序(仍在学习),但我不知道如何按照良好做法简单地将按钮与功能相关联。
我正在使用 MVC 设计,应用程序搜索 ldap (AD) 并返回属于组成员的用户。
我使用 GET 表单将用户名发送到我的 servlet,然后将其发送到表单进行搜索,然后我将信息返回到我的 vue。 (我选择 GET 而不是 POST,因为有些用户喜欢直接在 URL 中填写 ?userName=)。
我创建了一个将结果导出到 excel 文件的函数,并且我想将一个按钮与该函数相关联。到目前为止,我所知道的就是我所做的:
JSP:
<!-- Send the userName input to the servlet to perform the search-->
<form method="get" action="<c:url value="/GroupVerification"/>">
<table>
<tr>
<td><label for="userCip">Search by CIP : </label></td>
<td><input type="text" id="userCip" name="userCip" /></td>
<td><input type="submit" value="Search" /></td>
</tr>
</table>
</form>
...<!-- html code for displaying results -->
<!-- Button to create Excel file with results-->
<form method="post" action="<c:url value="/GroupVerification"/>">
<input type="submit" name="excelExport" value="Export in Excel" />
</form>
小服务程序:
public void doPost( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {
if (request.getParameter("excelExport") != null) {
GroupVerificationForm.excelExport(user.getGroupList());
}
this.getServletContext().getRequestDispatcher( VUE ).forward( request, response );
public void doGet( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {
...
<!-- Code to send the userName to the form to perform search and setAttributes -->
this.getServletContext().getRequestDispatcher( VUE ).forward( request, response );
我不确定这是否是不好的做法,但 user 是我的 servlet 中的私有全局变量,在进行搜索时它与 doGet() 方法中的 User 相关联...
无论如何,当我单击“导出到 excel”按钮时,会调用 doPost() 并将我的结果导出到 excel,但是,由于 URL 不再包含 ?userName=foo,我的搜索消失了。我的问题是我想留在同一页面上。有没有办法只调用带有按钮的方法而不调用 doPost 所以我不会丢失我的 URL?或者我应该在我的 doPost 中复制 doGet 代码,以便重新进行搜索并重新显示?
【问题讨论】: