【问题标题】:Java - Spring - portlet API @Controller how to read ajax parametersJava - Spring - portlet API @Controller 如何读取 ajax 参数
【发布时间】:2013-07-17 18:35:50
【问题描述】:

我有以下最新的带有多个参数的 jquery v aajx 调用。

在 java 代码中,我只能获取第一个参数值,其余的为空。

$.ajax({
        url : '<portlet:resourceURL id="entitledRequest"/>',
        data: '<c:out value="pfx= ${account.accountNumber}-${outletUI.outlet}-${count}&acc=${account.accountNumber}" />',
        cache: false,
        success : function(result) {

            //targetElem.html(result); update uI

        }
    });

以下是我的 java 代码,只有第一个参数不为空,之后所有参数都为空,我有调试 http 请求,所有参数都存在于请求对象中,任何线索这里有什么问题吗?

Controller("ajaxRequestController")
@RequestMapping(value = "VIEW")
public class AjaxRequestController implements PortletConfigAware  {


@ResourceMapping("entitledRequest")
    public void getServiceAutoComplete(ResourceRequest request, ResourceResponse response) throws IOException {


            String elemPrefix = request.getParameter("pfx");
            String acc = request.getParameter("acc"); // found null
            String mac = request.getParameter("mac"); // found null

}...

【问题讨论】:

  • elemPrefix 的值是多少?它包含完整的c:out 还是仅包含pfx 参数?看起来您没有 mac 参数。
  • 正如您在问题中看到的那样,所有参数只有一个 c:out 并为请求参数创建了一个字符串。
  • request.getParameter("pfx"); 返回的字符串值究竟是什么?
  • 它是唯一编号,例如 456565-44-1
  • 尝试在javascript中将值设置为vars,并将它们作为key:value对传递给data

标签: java spring portlet spring-portlet-mvc


【解决方案1】:

使用@RequestParam注解读取参数。

@ResourceMapping("entitledRequest")
public void getServiceAutoComplete(@RequestParam("pfx")  String elemPrefix, @RequestParam  String acc, @RequestParam String mac, ResourceRequest request, ResourceResponse response) throws IOException {

}

原因:

Ajax 发送的Parameters 不属于PortletNamespace。因此,它们不是ResourceRequest 的一部分,但可以在普通HTTPServletRequest 中找到。 SpringHTTPServletRequest 中搜索@RequestParam 名称。

您可以使用我建议的方法 - 或者 - 您可以将 &lt;portlet:namespace/&gt; 添加到 Ajax 调用中的参数中,保持您的 Controller 代码不变。

【讨论】:

  • Rite 现在我从 ajax 调用中恢复过来,但是对于下一个功能,我必须在 ajax 上工作,然后我将能够对此进行测试,我将更新我的响应。
  • @Faisalkhan 谢谢,你已经跟进了这篇文章。如果可行,请接受它。
  • $.get("test.cgi", { name: "John", time: "2pm" }) .done(function(data) { alert("Data Loaded: " + data) ; });
猜你喜欢
  • 2015-10-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-29
  • 1970-01-01
  • 1970-01-01
  • 2015-08-06
  • 1970-01-01
相关资源
最近更新 更多