Struts2+JQuery+JSON集成
細節部分我就不多講了,因為我也不會,就講講我是如何調試出來我的第一個JSON使用的吧
采用的框架有:Struts2 、 JQuery 、 JSON
按著步驟來吧:
1.新建一個Web工程
導入包列表:
目錄結構如圖:
2.建立實體類User
package model;
public class User
private String name;
private int age;
//省略相應的get和set方法
3.建立Action JsonAction
public class JsonAction extends ActionSupport{
private static final long serialVersionUID =
7044325217725864312L;
private User user;
//用于記錄返回結果
private String result;
//省略相應的get和set方法
@SuppressWarnings("static-access")
public String execute() throws Exception {
//將要返回的user實體對象進行json處理
JSONObject jo = JSONObject.fromObject(this.user);
//打印一下,格式如下
//{"name":"風達","age":23}
System.out.println(jo);
//調用json對象的toString方法轉換為字符串然后賦值給result
this.result = jo.toString();
return this.SUCCESS;
}
}
4.建立struts.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.i18n.encoding" value="UTF-8"></constant>
<package name="ttttt" extends="json-default">
<action name="jsonAction" class="action.JsonAction">
<result type="json" >
<!-- 因為要將reslut的值返回給客戶端,所以這樣設置 -->
<!-- root的值對應要返回的值的屬性 -->
<param name="root">
result
</param>
</result>
</action>
</package>
</struts>
5.編寫index.jsp文件
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%
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%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!-- basePath用來獲取js文件的絕對路徑 -->
<script type="text/javascript" src="<%=basePath%>js/jquery.js"></script>
<script type="text/javascript" src="<%=basePath%>js/index.js"></script>
<s:head theme="ajax" />
</head>
<body>
<div id="result">
</div>
<s:form name="userForm" action="" method="post">
<s:textfield label="用戶名" name="user.name" />
<s:textfield label="年齡" name="user.age" />
<button>
提交
</button>
</s:form>
</body>
</html>
6.在WebRoot目錄下建立js文件件,將jquery.js文件放到文件夾下,然后再建立文件index.js
$(document).ready(function() {
// 直接把onclick事件寫在了JS中
$("button").click(function() {
// 序列化表單的值
var params = $("input").serialize();
$.ajax({
// 后臺處理程序
url : "jsonAction.action",
// 數據發送方式
type : "post",
// 接受數據格式
dataType : "json",
// 要傳遞的數據
data : params,
// 回傳函數
success : update_page
});
});
});
function update_page(result) {
var json = eval( "("+result+")" );
var str = "姓名:" + json.name + "<br />"; str += "年齡:"
+ json.age + "<br />";
$("#result").html(str);
}
7.運行前效果:
要的是效果,布局就不整了
運行后效果:
網上相關的信息太少了,很多Struts2+JQuery+JSON的教程,點開鏈接之后都是那幾篇文章轉了又轉,遇到問題真的很想找到有用的信息,或許是我太笨了,找不到,或許就是網上相關的信息就很少。這個實例很簡單是不是,但是為了調試出這個程序,我費了一天的時間。
上面的實例成功了,但是問題又出來了
視圖類型僅僅設置了json
那么輸入校驗出錯的時候怎么顯示?
本文來自CSDN博客,轉載請標明出處:http://blog.csdn.net/fengda2870/archive/2009/04/06/4052527.aspx