struts2+ajax+json使用实例
生活随笔
收集整理的這篇文章主要介紹了
struts2+ajax+json使用实例
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
本文主要包含一個struts2+ajax+json的使用實例
步驟如下
1.準備工作
①ajax使用Jquery:jquery-1.4.2.min.js
②struts2與json的依賴包:struts2-json-plugin-2.2.3.jar,json-lib
PS:版本可自己控制!~
2.過程
①引入json依賴包
②編寫action類
③配置struts.xml
④編寫頁面
⑤測試
參考鏈接
struts2 + ajax + json的結合使用,實例講解 - tfy1332的專欄 - 博客頻道 - CSDN.NET
Struts2 和 Ajax 交互 - - ITeye技術網站
Java中:struts2+jQuery+ajax調用演示 - anyx的專欄 - 博客頻道 - CSDN.NET
實例
action類
package com.zxt.action;import java.util.ArrayList; import java.util.List;import com.opensymphony.xwork2.ActionSupport;public class JsonAction extends ActionSupport {/*** */private static final long serialVersionUID = 7443363719737618408L;/*** 姓名*/private String name;/*** 身高*/private String inch;/*** ajax返回結果,也可是其他類型的,這里以String類型為例*/private List<PersonBean> result;@Overridepublic String execute() throws Exception {// TODO Auto-generated method stubList<PersonBean> pList = new ArrayList<PersonBean>();if ("張三".equals(name)) {PersonBean p1 = new PersonBean();PersonBean p2 = new PersonBean();p1.setId(1);p1.setName("a");p2.setId(2);p2.setName("b");pList.add(p1);pList.add(p2);result = pList;} else {PersonBean p3 = new PersonBean();PersonBean p4 = new PersonBean();p3.setId(3);p3.setName("c");p4.setId(4);p4.setName("d");pList.add(p3);pList.add(p4);result = pList;}return SUCCESS;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getInch() {return inch;}public void setInch(String inch) {this.inch = inch;}/*** * @Title: getResult* @Description:json調取結果* @param @return* @return String* @throws*/public List<PersonBean> getResult() {return result;} }struts.xml配置文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN""http://struts.apache.org/dtds/struts-2.1.7.dtd"> <struts><constant name="struts.devMode" value="true"/><package name="ajax" extends="json-default"><action name="jsonAjax" class="com.zxt.action.JsonAction"><!-- 將返回類型設置為json --><result type="json"></result></action></package> </struts>web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"><display-name></display-name> <filter><filter-name>struts2</filter-name><filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class></filter><filter-mapping><filter-name>struts2</filter-name><url-pattern>/*</url-pattern></filter-mapping><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list> </web-app>index.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> <base href="<%=basePath%>"> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>測試</title> <script type="text/javascript" src="js/jquery-1.4.2.js"></script> <script type="text/javascript">$(function() {$("#tj").click(function() {//提交的參數,name和inch是和struts action中對應的接收變量/*var data = {"list" : [ {"id" : 1,"content" : "測試信息1111"}, {"id" : 2,"content" : "測試信息2222"} ]};$.each(data.list, function(i, item) {alert(item.id);alert(item.content);});*/var params = {name : $("#xm").val(),inch : $("#sg").val()};$.ajax({type : "POST",url : "jsonAjax.action",data : params,dataType : "text", //ajax返回值設置為text(json格式也可用它返回,可打印出結果,也可設置成json)success : function(json) {var obj = $.parseJSON(json); //使用這個方法解析jsonvar state_value = obj.result; //result是和action中定義的result變量的get方法對應的$.each(state_value, function(i, item) {alert(item.id);alert(item.name);});//alert(state_value);},error : function(json) {alert("json=" + json);return false;}});});}); </script> </head> <body><span>姓名:</span><input id="xm" type="text"><br /><span>身高:</span><input id="sg" type="text"><br /><input type="button" value="提交" id="tj"> </body> </html>本例中的jsp解析了從服務器傳來的list,解析list具體方法如下
var data = {"list":[{"id":1,"content":"測試信息1111"},{"id":2,"content":"測試信息2222"}]}$.each(data.list, function(i, item) {alert(item.id);alert(item.content);});完成
總結
以上是生活随笔為你收集整理的struts2+ajax+json使用实例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ListView滑动删除效果实现
- 下一篇: Android之jni入门