struts2+ajax+json 实例

1、struts2 要支持json,需要先引入包,包的下载地址如下:

http://code.google.com/p/jsonplugin/downloads/list

2、根据struts2的版本,选择jar包。

3、配置struts.xml文件:

Xml代码 收藏代码

  1. <?xml version="1.0" encoding="UTF-8"?>  

  2. <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd">  

  3. <struts>  

  4.     <package name="TestJson" extends="json-default,struts-default" namespace="/bbs">  

  5.         <action name="GoView" class="com.json.action.JsonAction" method="goView">  

  6.             <result name="success">/GOView.jsp</result>  

  7.         </action>  

  8.           

  9.         <action name="getUserName" class="com.json.action.JsonAction" method="getUserName">  

  10.             <result type="json"/>  

  11.         </action>  

  12.     </package>      

  13. </struts>  

 

4、JAVA类信息如下:

Java代码  收藏代码

  1. public class JsonAction extends ActionSupport {  

  2.   

  3.     private static final long serialVersionUID = 105155412743741566L;  

  4.       

  5.     private User        user;  

  6.       

  7.     public String goView() throws Exception {  

  8.         return SUCCESS;  

  9.     }  

  10.   

  11.     public String getUserName() throws Exception {  

  12.         return SUCCESS;  

  13.     }  

  14.       

  15.     public User getUser() {  

  16.         return user;  

  17.     }  

  18.   

  19.     public void setUser(User user) {  

  20.         this.user = user;  

  21.     }  

  22. }  

 

5、JSP页面如下:

 

Java代码  收藏代码

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  

  2. <html>  

  3.     <head>  

  4.         <base href="<%=basePath%>">   

  5.         <meta http-equiv="pragma" content="no-cache">  

  6.         <meta http-equiv="cache-control" content="no-cache">  

  7.         <meta http-equiv="expires" content="0">             

  8.         <title>JSON学习</title>  

  9.         <script language="javascript" type="text/javascript" src="<%=basePath%>JQuery/jquery-1.6.js"></script>  

  10.         <script type="text/javascript">  

  11.             function testJSON() {  

  12.                 var username = "isoftstone";  

  13.                 $.ajax({  

  14.                     type:'post',  

  15.                     url:'bbs/getUserName.action',  

  16.                     dataType:'json',  

  17.                     data:{  

  18.                         "user.username":username  

  19.                     },  

  20.                     success:function(msg){  

  21.                         alert(msg.user.username);  

  22.                     }  

  23.                 });  

  24.             }  

  25.         </script>  

  26.     </head>  

  27.     <body>  

  28.         <input type="button" value="测试JSON" onClick="JavaScript:testJSON()"/>  

  29.     </body>  

  30. </html>  

 

6、运行程序及可。

7、注意事项:

(1) 引入的包要正确;

(2) struts.xml的package定义要正确:

Xml代码 < 收藏代码

  1. <package name="TestJson" extends="json-default,struts-default" namespace="/bbs"> 

转载于:https://my.oschina.net/fuluS/blog/308794

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值