【问题标题】:How to create a button in a JSP file that calls a Java method如何在调用 Java 方法的 JSP 文件中创建按钮
【发布时间】: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 代码,以便重新进行搜索并重新显示?

【问题讨论】:

    标签: jsp servlets


    【解决方案1】:

    如何在 JSP 文件中创建一个调用 Java 方法的按钮

    使用 ajax。另见How to use Servlets and Ajax?


    但是,我的搜索消失了,因为 URL 不再包含 ?userName=foo

    只需让表单提交到所需的 URL。

    <c:url value="/GroupVerification" var="groupVerificationURL">
        <c:param name="userName" value="${param.userName}" />
    </c:url>
    ...
    <form method="post" action="${groupVerificationURL}">
    

    【讨论】:

    • 我想知道如果没有 javascript 这是否可能?我尝试提交 URL,但遇到 2 个问题:1) 项目名称在 url localhost:8080/VerificationAD/VerificationAD... 中重复出现 2) 参数特殊字符不会转换为 % 表单
    • 没有JS?只需使用 HTML,但您已经知道如何做到这一点。至于双重上下文路径,这很奇怪。也许您修改了答案以手动包含上下文路径?你不应该那样做。 &lt;c:url&gt; 已经自动包含它。至于参数的 URL 编码,这很奇怪。 &lt;c:param&gt; 已经对其进行了自动编码。或者您是在谈论您在浏览器地址栏中看到的内容,而不是在实际的 HTML 源代码中看到的内容?
    • 1) 双重上下文是我的错误,我曾写过:
      ">,
    • %20 仅适用于 URI 路径中的空格,+ 适用于查询字符串中的空格。因此,负责在查询字符串中插入%20 的人做错了。但是,服务器端在解析查询字符串参数时应该没有问题 +。所以很难理解你的“不起作用”的说法。也许您是手动摆弄getQueryString() 而不是使用getParameter() 左右?
    • 我在 doGet() 中使用 request.getParameter 来获取 groupName,然后我只创建一个表单 (groupVerificationForm) ,一个 bean (group) 并使用表单中的函数设置 bean。然后我与我的小组一起 request.setAttributes。然后我在我的jsp中显示元素。 doPost 仅包含此线程中的代码,我使用 doPost 仅调用一个函数,并尝试与我保持在同一页面上。
    猜你喜欢
    • 2014-02-20
    • 1970-01-01
    • 1970-01-01
    • 2020-12-26
    • 1970-01-01
    • 2013-01-21
    • 1970-01-01
    • 2012-03-22
    • 2011-01-22
    相关资源
    最近更新 更多