struts2下,利用jquery實現ajax

【代碼環境】

1.jdk1.8.0_131+eclipse node3(4.6.3)+struts2.3.32+jquery-2.1.4-min.js

2.所需要的struts2 jar包:

1)commons-beanutils-1.8.0.jar
2)commons-collections-3.2.2.jar
3)commons-fileupload-1.3.2.jar
4)commons-io-2.2.jar
5)commons-lang-2.4.jar
6)commons-lang3-3.2.jar
7)commons-logging-1.1.3.jar
8)ezmorph-1.0.6.jar
9)freemarker-2.3.22.jar
10)javassist-3.11.0.GA.jar
11)json-lib-2.3-jdk15.jar
12)ognl-3.0.19.jar
13)struts-core-1.3.10.jar
14)struts2-core-2.3.32.jar
15)struts2-json-plugin-2.3.32.jar
16)xwork-core-2.3.32.jar

3.所用到的js文件

1)jquery-2.1.4.min.js

[部分文件可能不需要,但多的話不會報錯,少的話會無法運行]

【結構圖片】

web.xml配置-圖片


web.xml配置-代碼

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	id="WebApp_ID" version="3.0">
	<display-name>ajax-test</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.html</welcome-file>
	</welcome-file-list>
</web-app>

【struts.xml配置-圖片】


【struts.xml配置-文字】

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

	<constant name="struts.enable.DynamicMethodInvocation" value="false" />
	<constant name="struts.devMode" value="true" />

	<package name="load" namespace="/" extends="struts-default">
		<action name="go" class="econ.scu.edu.cn.GoAction">
			<result>/WEB-INF/ajax-test/test.jsp</result>
		</action>
	</package>
	
	<package name="test" namespace="/test" extends="struts-default,json-default">
		<action name="loadUserInfo" class="econ.scu.edu.cn.AjaxAction">
			<result name="ajaxtest" type="json"></result>
		</action>
	</package>

</struts>


【進入測試界面-在webcontent下,會自動跳轉到go.action,也就是WEB-INF/ajax-test/test.jsp】

【測試界面-ajax請求-圖片】


【測試界面-ajax請求-文字】

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="js/jquery-2.1.4.min.js"></script>
<title>Insert title here</title>
<script type="text/javascript">
	$(document).ready(
			function() {
				$("#test").click(
						function() {

							$.ajax({
								url : 'test/loadUserInfo',
								type : 'post',
								beforeSend : function() {
									alert("檢測是否執行到這裏!");
								},
								data : "{}",
								dataType : 'json',
								success : function(data) {
									$("#testJson").append(
											"<div>返回了:id:" + data.personInfo.id
													+ ", name: "
													+ data.personInfo.name
													+ ",age:"
													+ data.personInfo.age
													+ ",adress:"
													+ data.personInfo.address
													+ "</div>");
								}
							});
						});
			});
</script>
</head>
<body>
	<h4>這裏是ajax的測試!</h4>
	<input type="button" id="test" value="從後臺獲取JSON數據" />
	<div id="testJson"></div>
</body>
</html>

【處理go.action請求的GoAction.java】


【處理loadUserInfo.action的AjaxAction.java】


【PersonInfo實體類,包含四個字段id(string),name(string),age(int),address(string)】


【運行效果圖】



【錯誤與解決辦法】

1.The request rescourse is not avaiable

出現這個問題很頭疼,如果確定自己建的沒問題,那就重啓服務器,然後運行。[我的話,就是經常報這個錯誤,這樣就解決了];加入不確信自己的工程有沒有問題,那麼首先去控制檯查看日誌,可能是缺少jar包。最有可能的是你的struts.xml文件寫錯了。

2.jquery文件引入的問題

假如的js文件是WebContent下的js文件夾下,那麼引用的路徑是src="js/jquery.2.1.4-min.js",這個與jsp頁面和html頁面所在的位置無關。也就是說在WEB-INF目錄下的頁面也是這麼引用的。

3.返回類型的變更

返回JSON需要假如json-lib jar包,這是<result type="json"></result>纔有效。同時reslut之間不需要加返回的頁面什麼的。另外,包的繼承要修改成json-default。這一點很重要。

【感謝】

感謝前輩們的資料!



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章