现在的位置: 首页 > 综合 > 正文

Struts2中Json格式异步数据交…

2019年03月08日 ⁄ 综合 ⁄ 共 4447字 ⁄ 字号 评论关闭
注意Struts <wbr>2 <wbr>中 <wbr>Json <wbr>格式异步数据交互博客所有源码在爱源码,爱编程QQ群群共享中公开,需要请加群下载!群号175551460
Json格式数据在Ajax数据交互中具有举足轻重的位置,在数据的可操作性上是:安全的,灵活的,易控的。
下面我们在Struts2.1 中来操作Json实现异步处理。----->>
1.首先导入json相关的.jar文件,可加载struts2.1自动加载.jar文件,也可从网上获得--->
图片
实现 json异步交互主要需要-------------->红箭头上的.jar文件。 
取得.jar文件后,我们只需将它们拷贝到项目的WEB-INF/lib目录下:
2.之后,我们要取得Struts 对json的支持:只需在struts.xml中写入(相关支持数据)即可:
2.1:获取相关支持数据:在引用库中找到上struts-plugin.xml文件
 图片
2.2:打开struts-plugin.xml文件,拷贝红色矩形框中这些数据。
图片
2.3:现在将数据复制到struts.xml文件中:
图片
最好指定一下默认编码集
图片
3:我们写好个UserAction的类文件,里面拥有如下字段,并封装属性:
图片
3.1:写个封装对象集合的方法,并return“list”;(注意:本例只对 LIST 集合进行操作,你可举一反三)
图片
3.2:在struts.xml定义如下数据,根据动态方法取得相关json数据:(注意:映射类为之前新建的UserAction类)
图片
4:现在我们新建JSP页面进行测试了。
 

 
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    
    <title>Struts2+JQuery+JSON</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">
    <script type="text/javascript" src="js/jquery-1.8.3.js"></script>
    <script type="text/javascript" src="js/json.js"></script>
  </head>
  
  <body>
    <input id="getMessage" type="button" value=""/>&nbsp;&nbsp;
    <input id="getUserInfo" type="button" value="UserInfo"/>&nbsp;&nbsp;
    <input id="getList" type="button" value="List"/>&nbsp;&nbsp;
    <input id="getMap" type="button" value="Map"/>&nbsp;&nbsp;
    <br>
    <br>
    <br>
    <!--  -->
    <div id="message"></div>
   <form>
        ID<input name="userInfo.userId" type="text"/><br/>
        <input name="userInfo.userName" type="text"/><br/>
        &nbsp;&nbsp;&nbsp;<input name="userInfo.password" type="text"/><br>
        <input id="regRe" type="button" value=""/>
    </form>
  
  </body>
</html>
 
5:导入JQuery.js文件,并新建json.js文件添加如下代码:
 
$(document).ready(function(){
 //
 $("#getMessage").click(function(){
  $.getJSON("user_returnMessage.action",function(data){
   //.data.messageActionmessage
   $("#message").html("<font color='red'>"+data.message+"</font>");
  });
 });
 //UserInfo
 $("#getUserInfo").click(function(){
  $.getJSON("user_returnUserInfo.action",function(data){
   //
   $("#message").html("");
   //
   //data.userInfo.
   $("#message").append("<div><font color='red'>ID"+data.userInfo.userId+"</font></div>")
          .append("<div><font color='red'>"+data.userInfo.userName+"</font></div>")
          .append("<div><font color='red'>"+data.userInfo.password+"</font></div>")
  });
 });
 //List
 $("#getList").click(function(){
  $.getJSON("user_returnList.action",function(data){
   //
   $("#message").html("");
   //使jQueryeach(data,function(){});
   //data.userInfosListUserInfovalue
   $.each(data.userInfosList,function(i,value){
    $("#message").append("<div>"+(i+1)+"</div>")
       .append("<div><font color='red'>ID"+value.userId+"</font></div>")
       .append("<div><font color='red'>"+value.userName+"</font></div>")
       .append("<div><font color='red'>"+value.password+"</font></div>");
   });
  });
 });
 //Map
 $("#getMap").click(function(){
  $.getJSON("user_returnMap.action",function(data){
   //
   $("#message").html("");
   //使jQueryeach(data,function(){});
   //data.userInfosListUserInfovalue
   //keyMap
   $.each(data.userInfosMap,function(key,value){
    $("#message").append("<div><font color='red'>"+key+"ID"+value.userId+"</font></div>")
          .append("<div><font color='red'>"+key+""+value.userName+"</font></div>")
          .append("<div><font color='red'>"+key+""+value.password+"</font></div>");
   });
  });
 });
 //
 $("#regRe").click(function(){
  //
  var params = $("form").serialize();
  //使jQuery$.ajax({});Ajax
  $.ajax({
   url:"user_gainUserInfo.action",
   type:"POST",
   data:params,
   dataType:"json",
   success:function(data){
    //
   $("#message").html("");
   //
   //data.userInfo.
   $("#message").append("<div><font color='red'>ID"+data.userInfo.userId+"</font></div>")
          .append("<div><font color='red'>"+data.userInfo.userName+"</font></div>")
          .append("<div><font color='red'>"+data.userInfo.password+"</font></div>")
   }
  });
 });
});
 

6.运行效果:
图片
图片
本例就到这里,我们团队来自奥斯科技


抱歉!评论已关闭.