将参数传递给动作是正确的方法吗

Is it right way to pass parameters to action

我正在 S2 中实施出勤 sheet。我只需要 select 三个复选框(P、A、L)中的一个。如果需要,用户可以一次 select。最后他们将出席。所有这些东西都完成了。完全理解请看下图。

点击按钮后,我拨打了 Ajax 电话。因此 selected 值将被发送到操作 class,例如 P##1,L##2,P##3,L##4,P##5, P##6,A##7,P##8,A##9,P##10。在行动 class 中,我将在处理请求时拆分它。这是传递参数的正确方法吗?你能告诉我还有其他解决方案吗?

在JSP

  <s:iterator value="listOfEmployees">
  <s:property value="%{empCode1}" />
  <s:checkbox name="somename%{empCode}" fieldValue="P##%{empCode}" theme="simple" cssClass="first"/>
  <s:checkbox name="somename%{empCode}" fieldValue="A##%{empCode}" theme="simple" cssClass="second"/>
  <s:checkbox name="somename%{empCode}" fieldValue="L##%{empCode}" theme="simple" cssClass="third"/>
  </s:iterator>

在 JS 中,点击按钮后所有选中的值都会出现。通过下面的行,我得到了上面的参数。 1、2 等是 ID(假设)和 P - 现在等

  values+=$(this).val()+",";//now values=P##1,L##2,P##3,L##4,P##5,P##6,A##7,P##8,A##9,P##10

  xmlhttp.open("GET","actionname.action?ids="+values,true);//call action

明智的做法是在将查询字符串发送到服务器之前对其进行编码,尤其是因为您的哈希符号 (#) 会被误解为片段。

这是一种使用 encodeURIComponent() 的可能解决方案(它将对哈希符号进行编码):

xmlhttp.open("GET","actionname.action?ids="+encodeURIComponent(values), true);

然后您将像这样发送您的参数:

actionname.action?ids=P%23%231%2CL%23%232%2CP%23%233%2CL%23%234%2CP%23%235%2CP%23%236%2CA%23%237%2CP%23%238%2CA%23%239%2CP%23%2310

并且在服务器端你可以像这样 urldecode() 它们(在 PHP 中):

<?php
echo urldecode( $_GET["ids"] );

或(在JSP中):

URLDecoder.decode(Request.getQueryString(), 'UTF-8')

回来

P##1,L##2,P##3,L##4,P##5,P##6,A##7,P##8,A##9,P##10

(注意:我不熟悉JSP,所以这里有更多关于解码查询字符串的详细信息:How do I correctly decode unicode parameters passed to a servlet