Struts1+JQuery+JSON/XML的例子
1.Struts+JQuery+XML
struts-config.xml如下:
- <action
- attribute="testForm"
- input="/indexxml.jsp"
- name="testForm"
- path="/testxml"
- scope="request"
- type="action.TestXmlAction"
- validate="false"/>
TestxmlAction.java如下:
- /*
- *GeneratedbyMyEclipseStruts
- *Templatepath:templates/java/JavaClass.vtl
- */
- packageaction;
- importjava.io.IOException;
- importjava.io.PrintWriter;
- importjava.util.ArrayList;
- importjava.util.List;
- importjavax.servlet.http.HttpServletRequest;
- importjavax.servlet.http.HttpServletResponse;
- importorg.apache.struts.action.Action;
- importorg.apache.struts.action.ActionForm;
- importorg.apache.struts.action.ActionForward;
- importorg.apache.struts.action.ActionMapping;
- importform.TestForm;
- /**
- *MyEclipseStruts
- *Creationdate:03-05-2010
- *
- *XDocletdefinition:
- *@struts.actionpath="/test"name="testForm"input="index.jsp"scope="request"
- */
- publicclassTestXmlActionextendsAction{
- /*
- *GeneratedMethods
- */
- /**
- *Methodexecute
- *@parammapping
- *@paramform
- *@paramrequest
- *@paramresponse
- *@returnActionForward
- *@throwsIOException
- */
- publicActionForwardexecute(ActionMappingmapping,ActionFormform,
- HttpServletRequestrequest,HttpServletResponseresponse)throwsIOException{
- TestFormtestForm=(TestForm)form;//TODOAuto-generatedmethodstub
- response.setContentType("text/xml;charset=utf-8");//传输xml时要用html
- response.setCharacterEncoding("utf-8");
- System.out.println(testForm.getTestvalue());
- PrintWriterpw=response.getWriter();
- //1.传递单个参数,注意应将text/xml改为text/html
- /*inti=9;
- pw.print(i);
- pw.flush();*/
- //2.生成xml文件返回给html页面,此时list里面为单个String
- /*StringBuilderxml=newStringBuilder();
- List<String>list=newArrayList<String>();
- list.add("aaa");
- list.add("bbb");
- list.add("ccc");
- xml.append("<items>");
- for(Objecto:list){
- xml.append("<item>").append(o).append("</item>");
- }
- xml.append("</items>");
- System.out.println(xml);
- pw.print(xml.toString());
- pw.flush();*/
- //3.生成xml文件返回给html页面,此时list里面为对象类型
- /*response.setContentType("text/xml;charset=utf-8");
- StringBuilderxml=newStringBuilder();
- List<User>list=newArrayList<User>();
- Useruser1=newUser();
- Useruser2=newUser();
- Useruser3=newUser();
- user1.setUsername("username1");
- user1.setPassword(1);
- user2.setUsername("username2");
- user2.setPassword(2);
- user3.setUsername("username3");
- user3.setPassword(3);
- list.add(user1);
- list.add(user2);
- list.add(user3);
- xml.append("<items>");
- for(inti=0;i<list.size();i++){
- xml.append("<itemslist>");
- xml.append("<username>").append(list.get(i).getUsername()).append("</username>");
- xml.append("<password>").append(list.get(i).getPassword()).append("</password>");
- xml.append("</itemslist>");
- }
- xml.append("</items>");
- System.out.println(xml);
- pw.print(xml.toString());*/
- returnnull;
- }
- }
辅助类User:
- packageaction;
- publicclassUser{
- privateStringusername;
- privateintpassword;
- publicStringgetUsername(){
- returnusername;
- }
- publicvoidsetUsername(Stringusername){
- this.username=username;
- }
- publicintgetPassword(){
- returnpassword;
- }
- publicvoidsetPassword(intpassword){
- this.password=password;
- }
- }
indexxml.jsp如下:
- <%@pagelanguage="java"import="java.util.*"pageEncoding="UTF-8"%>
- <%
- Stringpath=request.getContextPath();
- StringbasePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
- %>
- <!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">
- <html>
- <head>
- <basehref="<%=basePath%>">
- <title>MyStrutsJQueryJson</title>
- <metahttp-equiv="pragma"content="no-cache">
- <metahttp-equiv="cache-control"content="no-cache">
- <metahttp-equiv="expires"content="0">
- <metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">
- <metahttp-equiv="description"content="Thisismypage">
- <!--
- <linkrel="stylesheet"type="text/css"href="styles.css">
- -->
- <scripttype="text/javascript"src="/S1.2SHJQuery/js/jquery-1.4.2.js"></script>
- <scripttype="text/javascript">
- //1.jquery单独传递单个参数
- /*$(function(){
- alert("start");
- $.post("testxml.do",{testvalue:$('#test').val()},function(data){
- alert(data);
- })
- })*/
- //2.jquery单独传递含单个参数的list,采用的是xml形式
- $(function(){
- $.post("testxml.do",{testvalue:$('#test').val()},function(data){
- varitems=data.getElementsByTagName("item");
- alert(items.length);
- for(vari=0;i<items.length;i++){
- alert(items[i].childNodes[0].nodeValue);
- }
- })
- })
- //3.jquery单独传递含对象类型的list,,采用的是xml形式
- /*$(function(){
- $.post("testxml.do",{testvalue:$('#test').val()},function(data){
- varitems=data.getElementsByTagName("itemslist");
- //循环输出username和password
- for(vari=0;i<items.length;i++){
- varchilds=items[i].childNodes;
- for(varj=0;j<childs.length;j++){
- alert(childs[j].firstChild.nodeValue);
- }
- }
- })
- })
- */
- </script>
- </head>
- <body>
- <formaction="/testxml.do">
- <inputtype="button"value="JQuery"id="jquerytest"><br>
- <inputtype="text"value="JQuerytest"name="test"id="test">
- <fontid="userlogin">登陆</font><spanid="loading"></span>
- <pid="result"></p>
- </form>
- </body>
- </html>
2.Struts+JQuery+JSON
TestAction如下:
- /*
- *GeneratedbyMyEclipseStruts
- *Templatepath:templates/java/JavaClass.vtl
- */
- packageaction;
- importjava.io.IOException;
- importjava.io.PrintWriter;
- importjava.io.UnsupportedEncodingException;
- importjava.util.ArrayList;
- importjava.util.List;
- importjavax.servlet.http.HttpServletRequest;
- importjavax.servlet.http.HttpServletResponse;
- importnet.sf.json.JSONArray;
- importorg.apache.struts.action.Action;
- importorg.apache.struts.action.ActionForm;
- importorg.apache.struts.action.ActionForward;
- importorg.apache.struts.action.ActionMapping;
- importform.TestForm;
- /**
- *MyEclipseStruts
- *Creationdate:03-05-2010
- *
- *XDocletdefinition:
- *@struts.actionpath="/test"name="testForm"input="index.jsp"scope="request"
- */
- publicclassTestActionextendsAction{
- /*
- *GeneratedMethods
- */
- /**
- *Methodexecute
- *@parammapping
- *@paramform
- *@paramrequest
- *@paramresponse
- *@returnActionForward
- *@throwsUnsupportedEncodingException
- */
- publicActionForwardexecute(ActionMappingmapping,ActionFormform,
- HttpServletRequestrequest,HttpServletResponseresponse)throwsUnsupportedEncodingException{
- TestFormtestForm=(TestForm)form;//TODOAuto-generatedmethodstub
- response.setContentType("text/json;charset=utf-8");//注意设置为json,如果为xml,则设为xml
- /*response.setContentType("text/xml;charset=utf-8");传输xml时要用xml
- *response.setCharacterEncoding("utf-8");
- **/
- System.out.println(testForm.getTestvalue());
- //1.struts1.2+JQuery+Json传递list参数,此时list的类型为String
- /*List<String>list=newArrayList<String>();
- list.add("string1");
- list.add("string2");
- list.add("string3");
- JSONArrayjson=JSONArray.fromObject(list);
- try{
- PrintWriterout=response.getWriter();
- System.out.println(json);
- out.print(json);
- out.flush();
- }catch(IOExceptione){
- //TODOAuto-generatedcatchblock
- e.printStackTrace();
- }*/
- //2.struts1.2+JQuery+Json传递Map参数
- /*Map<String,String>map=newHashMap<String,String>();
- map.put("name1","string1");
- map.put("name2","string2");
- map.put("name3","string3");
- JSONArrayjson=JSONArray.fromObject(map);
- try{
- PrintWriterout=response.getWriter();
- System.out.println(json);
- out.print(json);
- out.flush();
- }catch(IOExceptione){
- //TODOAuto-generatedcatchblock
- e.printStackTrace();
- }*/
- //3.传递单个String值
- /*Stringsinglepara="{/"name/":'中国'}";//要注意格式
- JSONObjectjson=JSONObject.fromObject(test);
- try{
- PrintWriterout=response.getWriter();
- System.out.println(json);
- out.print(json);
- out.flush();
- }catch(IOExceptione){
- //TODOAuto-generatedcatchblock
- e.printStackTrace();
- }*/
- //4.struts1.2+JQuery+Json传递User参数
- /*Useruser1=newUser();
- user1.setPassword(1);
- Stringusername="你好";
- user1.setUsername(username);
- //user1.setUsername(newString(username.getBytes("utf-8"),"iso8859-1"));
- JSONObjectjson=JSONObject.fromObject(user1);
- try{
- PrintWriterout=response.getWriter();
- System.out.println(json);
- out.print(json);
- out.flush();
- }catch(IOExceptione){
- //TODOAuto-generatedcatchblock
- e.printStackTrace();
- }*/
- //5.struts1.2+JQuery+Json传递list参数,此时list的类型为String
- List<User>list=newArrayList<User>();
- Useruser1=newUser();
- user1.setPassword(1);
- user1.setUsername("u1");
- Useruser2=newUser();
- user2.setPassword(2);
- user2.setUsername("u2");
- Useruser3=newUser();
- user3.setPassword(3);
- user3.setUsername("u3");
- list.add(user1);
- list.add(user2);
- list.add(user3);
- JSONArrayjson=JSONArray.fromObject(list);
- try{
- PrintWriterout=response.getWriter();
- System.out.println(json);
- out.print(json);
- out.flush();
- }catch(IOExceptione){
- //TODOAuto-generatedcatchblock
- e.printStackTrace();
- }
- returnnull;
- }
- }
struts-config.xml如下:
- <action
- attribute="testForm"
- input="/index.jsp"
- name="testForm"
- path="/test"
- scope="request"
- type="action.TestAction"
- validate="false"/>
index.jsp如下:
- <%@pagelanguage="java"import="java.util.*"pageEncoding="UTF-8"%>
- <%
- Stringpath=request.getContextPath();
- StringbasePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
- %>
- <!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">
- <html>
- <head>
- <basehref="<%=basePath%>">
- <title>struts+jquery+json</title>
- <metahttp-equiv="pragma"content="no-cache">
- <metahttp-equiv="cache-control"content="no-cache">
- <metahttp-equiv="expires"content="0">
- <metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">
- <metahttp-equiv="description"content="Thisismypage">
- <!--
- <linkrel="stylesheet"type="text/css"href="styles.css">
- -->
- <scripttype="text/javascript"src="/MyStrutsJQueryJson/js/jquery-1.4.2.js"></script>
- <scripttype="text/javascript">
- /*1.struts1.2+JQuery+Json传递list参数,此时list的类型为String
- $(function(){
- $("#submitbutton").click(function(){
- varparams={testvalue:$('#test').val()};
- $.ajax({
- url:"test.do",
- data:params,
- type:'post',
- dataType:'json',
- success:function(data){
- //data值:["string1","string2","string3"]
- alert("成功");
- alert(data.length);
- for(varj=0;j<data.length;j++)
- {
- alert(data[j]);
- }
- },
- error:function(){
- alert("失败");
- }
- })
- })
- })*/
- //2.struts1.2+JQuery+Json传递Map参数
- /*$(function(){
- $("#submitbutton").click(function(){
- alert("start");
- varparams={testvalue:$('#test').val()};
- $.ajax({
- url:"test.do",
- data:params,
- type:'post',
- dataType:'json',
- success:function(data){
- //alert(data.name1);//缺点是须知道属性名,在JSONArray下
- //迭代循环输出
- //data值为:[{"name3":"string3","name1":"string1","name2":"string2"}]
- $.each(data[0],function(key,value){
- alert(key+""+value);
- })
- },
- error:function(){
- alert("失败");
- }
- })
- })
- })*/
- //3.struts1.2+JQuery+Json传递String参数
- /*
- $(function(){
- $("#submitbutton").click(function(){
- varparams={testvalue:$('#test').val()};
- alert("start");
- $.ajax({
- url:"test.do",
- data:params,
- type:'post',
- dataType:'json',
- success:function(data){
- alert("成功");
- alert(data.name);
- },
- error:function(){
- alert("失败");
- }
- })
- })
- })*/
- //4.struts1.2+JQuery+Json传递User参数
- /*$(function(){
- $("#submitbutton").click(function(){
- varparams={testvalue:$('#test').val()};
- $.ajax({
- url:"test.do",
- data:params,
- type:'post',
- dataType:'json',
- success:function(data){
- //data值为:{"password":1,"username":"你好"}
- alert("成功");
- alert(data.username);
- alert(data.password);
- },
- error:function(){
- alert("失败");
- }
- })
- })
- })*/
- //5.struts1.2+JQuery+Json传递list参数,此时list的类型为User
- $(function(){
- $("#submitbutton").click(function(){
- varparams={testvalue:$('#test').val()};
- $.ajax({
- url:"test.do",
- data:params,
- type:'post',
- dataType:'json',
- success:function(data){
- alert("成功");
- //data值:[{"password":1,"username":"u1"},{"password":2,"username":"u2"},{"password":3,"username":"u3"}]
- /*for(varj=0;j<data.length;j++)
- {
- alert(data[j].username);
- alert(data[j].password);
- }*/
- $.each(data,function(i){
- $.each(data[i],function(key,value){
- alert(key+""+value);
- })
- })
- },
- error:function(){
- alert("失败");
- }
- })
- })
- })
- </script>
- </head>
- <body>
- 测试输入框:<inputtype="text"id="test"name="hello"><br>
- <inputtype="button"name="submitbutton"id="submitbutton"value="提交">
- </body>
- </html>
Struts1+JQuery+JSON/XML的例子
1.Struts+JQuery+XML
struts-config.xml如下:
- <action
- attribute="testForm"
- input="/indexxml.jsp"
- name="testForm"
- path="/testxml"
- scope="request"
- type="action.TestXmlAction"
- validate="false"/>
TestxmlAction.java如下:
- /*
- *GeneratedbyMyEclipseStruts
- *Templatepath:templates/java/JavaClass.vtl
- */
- packageaction;
- importjava.io.IOException;
- importjava.io.PrintWriter;
- importjava.util.ArrayList;
- importjava.util.List;
- importjavax.servlet.http.HttpServletRequest;
- importjavax.servlet.http.HttpServletResponse;
- importorg.apache.struts.action.Action;
- importorg.apache.struts.action.ActionForm;
- importorg.apache.struts.action.ActionForward;
- importorg.apache.struts.action.ActionMapping;
- importform.TestForm;
- /**
- *MyEclipseStruts
- *Creationdate:03-05-2010
- *
- *XDocletdefinition:
- *@struts.actionpath="/test"name="testForm"input="index.jsp"scope="request"
- */
- publicclassTestXmlActionextendsAction{
- /*
- *GeneratedMethods
- */
- /**
- *Methodexecute
- *@parammapping
- *@paramform
- *@paramrequest
- *@paramresponse
- *@returnActionForward
- *@throwsIOException
- */
- publicActionForwardexecute(ActionMappingmapping,ActionFormform,
- HttpServletRequestrequest,HttpServletResponseresponse)throwsIOException{
- TestFormtestForm=(TestForm)form;//TODOAuto-generatedmethodstub
- response.setContentType("text/xml;charset=utf-8");//传输xml时要用html
- response.setCharacterEncoding("utf-8");
- System.out.println(testForm.getTestvalue());
- PrintWriterpw=response.getWriter();
- //1.传递单个参数,注意应将text/xml改为text/html
- /*inti=9;
- pw.print(i);
- pw.flush();*/
- //2.生成xml文件返回给html页面,此时list里面为单个String
- /*StringBuilderxml=newStringBuilder();
- List<String>list=newArrayList<String>();
- list.add("aaa");
- list.add("bbb");
- list.add("ccc");
- xml.append("<items>");
- for(Objecto:list){
- xml.append("<item>").append(o).append("</item>");
- }
- xml.append("</items>");
- System.out.println(xml);
- pw.print(xml.toString());
- pw.flush();*/
- //3.生成xml文件返回给html页面,此时list里面为对象类型
- /*response.setContentType("text/xml;charset=utf-8");
- StringBuilderxml=newStringBuilder();
- List<User>list=newArrayList<User>();
- Useruser1=newUser();
- Useruser2=newUser();
- Useruser3=newUser();
- user1.setUsername("username1");
- user1.setPassword(1);
- user2.setUsername("username2");
- user2.setPassword(2);
- user3.setUsername("username3");
- user3.setPassword(3);
- list.add(user1);
- list.add(user2);
- list.add(user3);
- xml.append("<items>");
- for(inti=0;i<list.size();i++){
- xml.append("<itemslist>");
- xml.append("<username>").append(list.get(i).getUsername()).append("</username>");
- xml.append("<password>").append(list.get(i).getPassword()).append("</password>");
- xml.append("</itemslist>");
- }
- xml.append("</items>");
- System.out.println(xml);
- pw.print(xml.toString());*/
- returnnull;
- }
- }
辅助类User:
- packageaction;
- publicclassUser{
- privateStringusername;
- privateintpassword;
- publicStringgetUsername(){
- returnusername;
- }
- publicvoidsetUsername(Stringusername){
- this.username=username;
- }
- publicintgetPassword(){
- returnpassword;
- }
- publicvoidsetPassword(intpassword){
- this.password=password;
- }
- }
indexxml.jsp如下:
- <%@pagelanguage="java"import="java.util.*"pageEncoding="UTF-8"%>
- <%
- Stringpath=request.getContextPath();
- StringbasePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
- %>
- <!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">
- <html>
- <head>
- <basehref="<%=basePath%>">
- <title>MyStrutsJQueryJson</title>
- <metahttp-equiv="pragma"content="no-cache">
- <metahttp-equiv="cache-control"content="no-cache">
- <metahttp-equiv="expires"content="0">
- <metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">
- <metahttp-equiv="description"content="Thisismypage">
- <!--
- <linkrel="stylesheet"type="text/css"href="styles.css">
- -->
- <scripttype="text/javascript"src="/S1.2SHJQuery/js/jquery-1.4.2.js"></script>
- <scripttype="text/javascript">
- //1.jquery单独传递单个参数
- /*$(function(){
- alert("start");
- $.post("testxml.do",{testvalue:$('#test').val()},function(data){
- alert(data);
- })
- })*/
- //2.jquery单独传递含单个参数的list,采用的是xml形式
- $(function(){
- $.post("testxml.do",{testvalue:$('#test').val()},function(data){
- varitems=data.getElementsByTagName("item");
- alert(items.length);
- for(vari=0;i<items.length;i++){
- alert(items[i].childNodes[0].nodeValue);
- }
- })
- })
- //3.jquery单独传递含对象类型的list,,采用的是xml形式
- /*$(function(){
- $.post("testxml.do",{testvalue:$('#test').val()},function(data){
- varitems=data.getElementsByTagName("itemslist");
- //循环输出username和password
- for(vari=0;i<items.length;i++){
- varchilds=items[i].childNodes;
- for(varj=0;j<childs.length;j++){
- alert(childs[j].firstChild.nodeValue);
- }
- }
- })
- })
- */
- </script>
- </head>
- <body>
- <formaction="/testxml.do">
- <inputtype="button"value="JQuery"id="jquerytest"><br>
- <inputtype="text"value="JQuerytest"name="test"id="test">
- <fontid="userlogin">登陆</font><spanid="loading"></span>
- <pid="result"></p>
- </form>
- </body>
- </html>
2.Struts+JQuery+JSON
TestAction如下:
- /*
- *GeneratedbyMyEclipseStruts
- *Templatepath:templates/java/JavaClass.vtl
- */
- packageaction;
- importjava.io.IOException;
- importjava.io.PrintWriter;
- importjava.io.UnsupportedEncodingException;
- importjava.util.ArrayList;
- importjava.util.List;
- importjavax.servlet.http.HttpServletRequest;
- importjavax.servlet.http.HttpServletResponse;
- importnet.sf.json.JSONArray;
- importorg.apache.struts.action.Action;
- importorg.apache.struts.action.ActionForm;
- importorg.apache.struts.action.ActionForward;
- importorg.apache.struts.action.ActionMapping;
- importform.TestForm;
- /**
- *MyEclipseStruts
- *Creationdate:03-05-2010
- *
- *XDocletdefinition:
- *@struts.actionpath="/test"name="testForm"input="index.jsp"scope="request"
- */
- publicclassTestActionextendsAction{
- /*
- *GeneratedMethods
- */
- /**
- *Methodexecute
- *@parammapping
- *@paramform
- *@paramrequest
- *@paramresponse
- *@returnActionForward
- *@throwsUnsupportedEncodingException
- */
- publicActionForwardexecute(ActionMappingmapping,ActionFormform,
- HttpServletRequestrequest,HttpServletResponseresponse)throwsUnsupportedEncodingException{
- TestFormtestForm=(TestForm)form;//TODOAuto-generatedmethodstub
- response.setContentType("text/json;charset=utf-8");//注意设置为json,如果为xml,则设为xml
- /*response.setContentType("text/xml;charset=utf-8");传输xml时要用xml
- *response.setCharacterEncoding("utf-8");
- **/
- System.out.println(testForm.getTestvalue());
- //1.struts1.2+JQuery+Json传递list参数,此时list的类型为String
- /*List<String>list=newArrayList<String>();
- list.add("string1");
- list.add("string2");
- list.add("string3");
- JSONArrayjson=JSONArray.fromObject(list);
- try{
- PrintWriterout=response.getWriter();
- System.out.println(json);
- out.print(json);
- out.flush();
- }catch(IOExceptione){
- //TODOAuto-generatedcatchblock
- e.printStackTrace();
- }*/
- //2.struts1.2+JQuery+Json传递Map参数
- /*Map<String,String>map=newHashMap<String,String>();
- map.put("name1","string1");
- map.put("name2","string2");
- map.put("name3","string3");
- JSONArrayjson=JSONArray.fromObject(map);
- try{
- PrintWriterout=response.getWriter();
- System.out.println(json);
- out.print(json);
- out.flush();
- }catch(IOExceptione){
- //TODOAuto-generatedcatchblock
- e.printStackTrace();
- }*/
- //3.传递单个String值
- /*Stringsinglepara="{/"name/":'中国'}";//要注意格式
- JSONObjectjson=JSONObject.fromObject(test);
- try{
- PrintWriterout=response.getWriter();
- System.out.println(json);
- out.print(json);
- out.flush();
- }catch(IOExceptione){
- //TODOAuto-generatedcatchblock
- e.printStackTrace();
- }*/
- //4.struts1.2+JQuery+Json传递User参数
- /*Useruser1=newUser();
- user1.setPassword(1);
- Stringusername="你好";
- user1.setUsername(username);
- //user1.setUsername(newString(username.getBytes("utf-8"),"iso8859-1"));
- JSONObjectjson=JSONObject.fromObject(user1);
- try{
- PrintWriterout=response.getWriter();
- System.out.println(json);
- out.print(json);
- out.flush();
- }catch(IOExceptione){
- //TODOAuto-generatedcatchblock
- e.printStackTrace();
- }*/
- //5.struts1.2+JQuery+Json传递list参数,此时list的类型为String
- List<User>list=newArrayList<User>();
- Useruser1=newUser();
- user1.setPassword(1);
- user1.setUsername("u1");
- Useruser2=newUser();
- user2.setPassword(2);
- user2.setUsername("u2");
- Useruser3=newUser();
- user3.setPassword(3);
- user3.setUsername("u3");
- list.add(user1);
- list.add(user2);
- list.add(user3);
- JSONArrayjson=JSONArray.fromObject(list);
- try{
- PrintWriterout=response.getWriter();
- System.out.println(json);
- out.print(json);
- out.flush();
- }catch(IOExceptione){
- //TODOAuto-generatedcatchblock
- e.printStackTrace();
- }
- returnnull;
- }
- }
struts-config.xml如下:
- <action
- attribute="testForm"
- input="/index.jsp"
- name="testForm"
- path="/test"
- scope="request"
- type="action.TestAction"
- validate="false"/>
index.jsp如下:
- <%@pagelanguage="java"import="java.util.*"pageEncoding="UTF-8"%>
- <%
- Stringpath=request.getContextPath();
- StringbasePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
- %>
- <!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">
- <html>
- <head>
- <basehref="<%=basePath%>">
- <title>struts+jquery+json</title>
- <metahttp-equiv="pragma"content="no-cache">
- <metahttp-equiv="cache-control"content="no-cache">
- <metahttp-equiv="expires"content="0">
- <metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">
- <metahttp-equiv="description"content="Thisismypage">
- <!--
- <linkrel="stylesheet"type="text/css"href="styles.css">
- -->
- <scripttype="text/javascript"src="/MyStrutsJQueryJson/js/jquery-1.4.2.js"></script>
- <scripttype="text/javascript">
- /*1.struts1.2+JQuery+Json传递list参数,此时list的类型为String
- $(function(){
- $("#submitbutton").click(function(){
- varparams={testvalue:$('#test').val()};
- $.ajax({
- url:"test.do",
- data:params,
- type:'post',
- dataType:'json',
- success:function(data){
- //data值:["string1","string2","string3"]
- alert("成功");
- alert(data.length);
- for(varj=0;j<data.length;j++)
- {
- alert(data[j]);
- }
- },
- error:function(){
- alert("失败");
- }
- })
- })
- })*/
- //2.struts1.2+JQuery+Json传递Map参数
- /*$(function(){
- $("#submitbutton").click(function(){
- alert("start");
- varparams={testvalue:$('#test').val()};
- $.ajax({
- url:"test.do",
- data:params,
- type:'post',
- dataType:'json',
- success:function(data){
- //alert(data.name1);//缺点是须知道属性名,在JSONArray下
- //迭代循环输出
- //data值为:[{"name3":"string3","name1":"string1","name2":"string2"}]
- $.each(data[0],function(key,value){
- alert(key+""+value);
- })
- },
- error:function(){
- alert("失败");
- }
- })
- })
- })*/
- //3.struts1.2+JQuery+Json传递String参数
- /*
- $(function(){
- $("#submitbutton").click(function(){
- varparams={testvalue:$('#test').val()};
- alert("start");
- $.ajax({
- url:"test.do",
- data:params,
- type:'post',
- dataType:'json',
- success:function(data){
- alert("成功");
- alert(data.name);
- },
- error:function(){
- alert("失败");
- }
- })
- })
- })*/
- //4.struts1.2+JQuery+Json传递User参数
- /*$(function(){
- $("#submitbutton").click(function(){
- varparams={testvalue:$('#test').val()};
- $.ajax({
- url:"test.do",
- data:params,
- type:'post',
- dataType:'json',
- success:function(data){
- //data值为:{"password":1,"username":"你好"}
- alert("成功");
- alert(data.username);
- alert(data.password);
- },
- error:function(){
- alert("失败");
- }
- })
- })
- })*/
- //5.struts1.2+JQuery+Json传递list参数,此时list的类型为User
- $(function(){
- $("#submitbutton").click(function(){
- varparams={testvalue:$('#test').val()};
- $.ajax({
- url:"test.do",
- data:params,
- type:'post',
- dataType:'json',
- success:function(data){
- alert("成功");
- //data值:[{"password":1,"username":"u1"},{"password":2,"username":"u2"},{"password":3,"username":"u3"}]
- /*for(varj=0;j<data.length;j++)
- {
- alert(data[j].username);
- alert(data[j].password);
- }*/
- $.each(data,function(i){
- $.each(data[i],function(key,value){
- alert(key+""+value);
- })
- })
- },
- error:function(){
- alert("失败");
- }
- })
- })
- })
- </script>
- </head>
- <body>
- 测试输入框:<inputtype="text"id="test"name="hello"><br>
- <inputtype="button"name="submitbutton"id="submitbutton"value="提交">
- </body>
- </html>