简体   繁体   English

从JSP到Servlet读取不同的参数

[英]Reading Different Parameters from JSP to Servlet

In my JSP, I have one drop down box and a table of items (using for loop) with each item being its own form all going to the same servlet. 在我的JSP中,我有一个下拉框和一个项目表(使用for循环),每个项目都是其自己的形式,都传递到同一servlet。 My issue is where to put the drop down box selection. 我的问题是在哪里放置下拉框选择。 I don't want to put it in the item form because it will have a drop down for each item. 我不想将其放在项目表单中,因为每个项目都有一个下拉列表。 But if I make a separate form, I can't get the drop down parameter from the servlet when I submit the item form. 但是,如果我创建单独的表单,则在提交项目表单时无法从servlet获取下拉参数。 I need to grab the drop down parameter and the item parameters. 我需要获取下拉参数和item参数。 Any ideas on approaching this? 有什么想法可以解决吗? Is there a way to have a hidden input in item form that stores the selected values from the drop down form? 有没有一种方法可以在项目表单中有一个隐藏的输入来存储从下拉表单中选择的值? I'm not using any javascript. 我没有使用任何JavaScript。

Put it in some other form. 用其他形式表示。 On every item form make one hidden field. 在每个项目表单上创建一个隐藏字段。

<select id="DD">
  <option value="1">1</option>
  <option value="2">2</option>
  <!-- so on ... -->
</select>

<form id="i1">
<input type="hidden" id="dropDownValue" value="">
<!-- your code -->
</form>
<form id="i2">
<input type="hidden" id="dropDownValue" value="">
<!-- your code -->
</form>
<form id="i3">
<input type="hidden" id="dropDownValue" value="">
<!-- your code -->
</form>
<form id="i3">
<input type="hidden" id="dropDownValue" value="">
<!-- your code -->
</form>

Now , before submit set the value of hiddenfield "dropDownValue" by JS. 现在,在提交之前,由JS设置hiddenfield“ dropDownValue”的值。

I would reconsider to have one form for each item. 我会重新考虑为每个项目使用一种表格。 One form for all items, including select box, will be enough. 所有项目(包括选择框)的一种形式就足够了。 So instead of: 所以代替:

<select>...</select>
<form><input type="hidden" name="id" value="1"/><input type="submit" value="Edit"/></form>
<form><input type="hidden" name="id" value="2"/><input type="submit" value="Edit"/></form>

You can have 你可以有

<form>
    <select>...</select>
    <input type="submit" name="edit:1" value="Edit"/>
    <input type="submit" name="edit:2" value="Edit"/>
</form>

In your servlet, you'll iterate over all request parameters ( request.getParameterNames() ) and look for parameter which starts with edit: prefix. 在您的Servlet中,您将遍历所有请求参数( request.getParameterNames() ),并查找以edit:前缀开头的参数。 If you find one, just subtrack the prefix and you'll get identifier of your item, which should be otherwise sent in some hidden field. 如果找到一个,只需对前缀进行子跟踪,您将获得商品的标识符,否则应在某个隐藏字段中发送该标识符。 The value of your select box will be sent as well. 选择框的值也将被发送。 If you'll need another action, say "delete", you can simply add more submit inputs with different prefix within the same HTML form. 如果您需要采取其他操作,例如说“删除”,则可以在同一HTML表单中简单地添加更多带有不同前缀的提交输入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM