且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用Ajax响应列表作为Struts2的迭代器值?

更新时间:2023-01-07 13:17:08

  

@AndreaLigios请解释第二类型的搜索结果也就是JSP snippet.I不知道如何使用JSP代码片段作为Ajax响应。

main.jsp中(完整)

 <%@ taglib伪preFIX =SURI =支柱 -  tags.tld%>
< HTML>
    < HEAD>
        <脚本>
            $(函数(){
                $('#装载机)。关于(键preSS点击功能(E){
                    $阿贾克斯({
                        网址:< S:URL操作='ajaxAction'/>中,
                    })。完成(功能(结果){
                        $(#目标)HTML(结果);
                    });
                });
            });
        < / SCRIPT>
    < /头>
    <身体GT;
        <输入类型=按钮ID =装载/>
        < D​​IV ID =目标>< / DIV>
    <身体GT;
< / HTML>
 

在struts.xml(相关)

 <作用NAME =ajaxAction级=foo.bar.AjaxAction>
    <结果> Snippet.jsp< /结果>
< /作用>
 

AjaxAction(相关)

 私人字符串的TestString;
/ * getter和setter * /

公共字符串的execute(){
   的TestString =我正在装载了AJAX;
   返回成功;
}
 

Snippet.jsp(完整)

 <%@ taglib伪preFIX =SURI =支柱 -  tags.tld%>

<! - 这是结果页面。您可以使用Struts标签在这里,
生成的HTML将被附加到目标分区。 - >

的TestString:< S:属性值=的TestString/>
 

输出:

 <身体GT;
    <输入类型=按钮ID =装载/>
    < D​​IV ID =目标>的TestString:我装有AJAX和LT; / DIV>
<身体GT;
 

I am using following ajax function :

 $.ajax({
       url: "userhomeonload",
       contentType: 'application/json',
       type: 'POST',
      datatype:'json', 
       async: true,
       success: function (res) {
                 alert(res[i].name+" "+res[i].rollNo);
    }  });

I am getting correct result in alert box. Now I want to use the list returned by this ajax in struts iterator as follows :

<s:iterator value="list">
   <div>
     <s:property value='name'></s:property>
     <s:property value="rollNo"></s:property>
   </div>
</s:iterator> 

But nothing is getting displayed on screen. Can anyone tell me how can I do so?

@AndreaLigios please explain 2nd type of result i.e. JSP snippet.I don't know how to use JSP snippet as ajax response.

Main.jsp (complete)

<%@ taglib prefix="s" uri="struts-tags.tld" %>
<html>
    <head>
        <script>
            $(function(){   
                $('#loader').on("keypress click", function(e) {
                    $.ajax({
                        url: "<s:url action='ajaxAction'/>",
                    }).done(function(result) {
                        $("#target").html(result);
                    });
                });
            });
        </script>   
    </head>
    <body>
        <input type="button" id="loader" />
        <div id="target"></div>
    <body>
</html>

Struts.xml (relevant)

<action name="ajaxAction" class="foo.bar.AjaxAction">
    <result>Snippet.jsp</result>
</action>

AjaxAction (relevant)

private String testString;
/* Getter and Setter */

public String execute(){
   testString = "I'm loaded with AJAX";
   return SUCCESS;
}

Snippet.jsp (complete)

<%@ taglib prefix="s" uri="struts-tags.tld" %>

<!-- This is the result page. You can use Struts tags here, 
the generated HTML will be appended to the target div. -->

TestString: <s:property value="testString" />

Output:

<body>
    <input type="button" id="loader" />
    <div id="target">TestString: I'm loaded with AJAX</div>
<body>