【问题标题】:getting value from a link in JSP从 JSP 中的链接获取价值
【发布时间】:2013-01-29 04:42:25
【问题描述】:

我在 jsp 页面中有锚链接,如下表中 <td> 所示。

<td>
    <span>
        <a href="AddDescriptionForEvent.jsp?" name="count"><%=(cnt)%></a>
    <span>
</td>

这里 cnt 里面的 scriplet 是一个整数。标签位于&lt;form&gt;&lt;form&gt;action 属性中,指向正确的下一页。
我必须在下一页中取那个整数的值。

我正在使用如下,

int day = nullIntconv(request.getParameter("count"));

这里nullIntconv 会将string 转换为integer

但我没有得到我选择的值。它总是给我 0。

请推荐。

【问题讨论】:

    标签: html jsp anchor forms http-request-parameters


    【解决方案1】:

    您需要对您的 href 进行一些更改, a href 不作为表单元素提交(例如,文本框、文本区域等)

    尝试这样使用..

    <td><span> <a href="AddDescriptionForEvent.jsp?count=<%=(cnt)%>">Click to get count</a><span></td>
    

    超过计数将作为查询字符串发送
    在下一页从请求中读取计数...

    String c= request.getParameter("count");
    if(c!=null)
    {
    int count=Integer.parseInt(c);//converting back into integer
    }
    

    --你的自定义代码在这里----------

    【讨论】:

      【解决方案2】:

      &lt;a&gt; 不能像您认为的那样使用。它不是依赖于 &lt;form&gt; 提交的 HTML 元素之一,例如 &lt;input&gt;&lt;textarea&gt;&lt;select&gt; 等。

      您可以阅读有关使用 &lt;a&gt; here 以及如何在 URL here 中传递请求参数的更多信息。还有一些关于HTML form and its elements

      所以如果你的代码是这样的:

      <form action="/AddDescriptionForEvent.jsp" name="myForm">
          <td>
              <input type="text" name="someText" value="some Value" />
          </td>
          <td>
              <span>
                  <a href="AddDescriptionForEvent.jsp?" name="count"><%=(cnt)%></a>
              <span>
          </td>
      
          <input type="submit" value="Press me to Submit" />
      </form>
      

      然后单击submit 按钮,您只会发送输入someText 的值,而不是count
      要将count 的值与其他值一起发送,请使用以下格式:

      <form action="/AddDescriptionForEvent.jsp" name="myForm">
          <td>
              <input type="text" name="someText" value="some Value" />
          </td>
          <td>
              <span>
                  <!-- changed the <a> tag to <input> -->
                  <input type="text" name="count" value="<%=(cnt)%>" />
              <span>
          </td>
      
          <input type="submit" value="Press me to Submit" />
      </form>
      

      或者您可以使用以下不带&lt;form&gt;

      <td>
          <span>
              <a href="AddDescriptionForEvent.jsp?count=<%=cnt%>">Click this link to Add</a>
          <span>
      </td>
      <!-- Notice the placement of the "cnt" variable of JSP -->
      

      若要在单击此&lt;a&gt; 链接时也传递其他参数,请将href 修改为href="AddDescriptionForEvent.jsp?count=&lt;%=cnt%&gt;&amp;someText=some value"

      您可以通过以下两种方式获得所需的结果。您获取请求参数的 java 代码很好。

      【讨论】:

        【解决方案3】:

        打算

        使用

        " name="count">

        【讨论】:

          【解决方案4】:
             thanks for all your replies.
          
               I did like this in the main page, added id
               <td align="center" height="35" id="day_<%=(cnt)%>">
               <span><a href="AddDescriptionForEvent.jsp?id=<%=(cnt)%>"><%=(cnt)%></a></span></td>
          
               And in the next page i got the required output as
          
               int d=nullIntconv(request.getParameter("id"));
          
                  Where nullIntconv is the string to integer converter.
          

          【讨论】:

            猜你喜欢
            • 2019-10-21
            • 2017-05-12
            • 1970-01-01
            • 2010-12-13
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多