The return form of Ajax request data from Struts2

Source: Internet
Author: User

This essay is an answer to the data request returned, the previous article on paging with the return data form is json,http://www.cnblogs.com/tele-share/p/7192206.html this time mainly explore Ajax from struts receive HTML and IO stream data form.

Way one: Receive data in HTML format

JSP page

1<!--Ajax from Struts request stitching completed HTML-2<script type= "Text/javascript" >3               var url = "${pagecontext.request.contextpath}/test?time=" + New Date (). GetTime ();4 $ ("#findAjax"). Click (function () {5 $.ajax ({6 Url:url,7 type: "GET",8 dataType: "html",9 Data:null,Ten Aysnc:false, One success:function (backdata) { A //alert (backdata); - $ ("Body"). Append (backdata); -                         }, the Error:function (Ajax) { - alert (ajax.readystate); -                         } -                     }) +               })  -</script> +           

Back-end Struts method

1  PublicString Execute ()throwsException {2             System.out.println ("Testaction.execute ()");3StringBuffer SB =NewStringBuffer ();4Sb.append ("<table border= ' 2px ' >"). Append ("<tr><td> number </td><td> name </td></tr > ");5Sb.append ("<tr><td>1</td><td>wyc</td></tr>");6Sb.append ("<tr><td>2</td><td>tele</td></tr>");7Sb.append ("<tr><td>3</td><td>fifth</td></tr>");8Sb.append ("</table>");9HttpServletResponse response =servletactioncontext.getresponse ();TenResponse.setcontenttype ("Text/html;charset=utf-8"); OnePrintWriter PW =Response.getwriter (); A Pw.write (sb.tostring ()); - Pw.flush (); - pw.close (); the System.out.println (SB); -         return NULL; -}

Way two: In the way of the stream

JSP page

1<script type= "Text/javascript" >2               varurl = "${pagecontext.request.contextpath}/test2?time=" +NewDate (). GetTime ();3$ ("#findAjax"). Click (function() {4 $.ajax ({5 Url:url,6Type: "GET",7DataType: "HTML",8DataNULL,9AYSNC:false,TenSuccessfunction(backdata) { One alert (backdata); A$ ("Body"). Append (backdata); -                         }, -Errorfunction(Ajax) { the alert (ajax.readystate); -                         } -                     }) -               }) +</script> -   

Back-up Struts method, note Chinese garbled problem

1 PrivateInputStream is;2      PublicInputStream Getis () {3         returnis ;4     }5     6      PublicString Execute2 () {7StringBuffer SB =NewStringBuffer ();8Sb.append ("<table border= ' 2px ' >"). Append ("<tr><td> number </td><td> name </td></tr > ");9Sb.append ("<tr><td>1</td><td>wyc</td></tr>");TenSb.append ("<tr><td>2</td><td>tele</td></tr>"); OneSb.append ("<tr><td>3</td><td>fifth</td></tr>"); ASb.append ("</table>"); -String string =sb.tostring (); -         Try { theis =NewBytearrayinputstream (String.getbytes ("Utf-8")); -}Catch(unsupportedencodingexception e) { -             //TODO auto-generated Catch block - e.printstacktrace (); +         } -          +         return"OK"; A}

The configuration file (which could have been written in the form of a wildcard, but in order to be clear in this way, though there is only one action)

1<!--test-2< PackageName= "XXX"extends= "Struts-default" >3<!--rely on the servlet API to return data-4<action name= "Test"class= "Com.itcast.action.TestAction" method= "execute" >5</action>6         7<!--return data using streams, note type=stream8<action name= "Test2"class= "Com.itcast.action.TestAction" method= "Execute2" >9<result name= "OK" type= "stream" >Ten<param name= "ContentType" >text/html;charset = utf-8</param> One<param name= "InputName" >is</param> A</result> -</action> -</ Package>

Summary: The first way to rely on the servlet API, but all on the server side to complete the stitching, the client to get the data directly inserted. The second is the way Java streams can be used to generate images to the client, and of course, the JSON form, see the choice of which way to use

The return form of Ajax request data from Struts2

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.