㈠ struts2 配置json,一直報錯。extends="json-default"。如果把這個包注釋掉的話就能啟動
<package name="netctoss" extends="json-default" abstract="true">
這是我以前寫項目時用json的情況,希望能專對你有所幫助!我是菜屬鳥。
㈡ java 後台接收json數據 我想在struts2 的action里,接收ext傳來的json數據 用什麼接收啊
只要從parameter里接收string類型就可以,然後將json字元串轉換成你需要的東西
㈢ struts2 升級遇到的問題
你是不是用到了Struts2的json插件?我搜到的一些可能的錯誤,給你參考:
大概是說那個json 插件的jar需要跟你的struts core的版本按照其官方文檔說明上來用,否則就有可能報錯。
下面是網上的一些資料:
當項目啟動時報如下錯誤時:com.opensymphony.xwork2.ActionContext.get(Ljava/lang/Object;)Ljava/lang/Object
1、導入的包有重復..刪掉其中一個重復的包
;
2、在整合struts2的時候報的一個異常,從我導入jsonplugin (Struts 2 JSON plugin
)的jar文件後出的。後來才知道是因為 版本不對,我用的是struts2.0的核心包,而我用的是支持struts2.1的jsonplugin文件;
3、
經過認真檢查,發現是版本不對.下載頁面上已經很清楚地說明了情況,0.33版本的jar包適用於Struts2.1.x,而0.32的jar包 適
用於Struts2.0.x.而我工程下面的Struts2的jar包都是從其他工程lib裡面直接復制過來的,沒有考慮版本的問題, 真是粗心!!
㈣ struts2-json中註解@JSON是什麼用
處理JSON數據
6.對一些屬性來的特殊處理可在源action代碼中去特殊的序列化,如:
[6.1]默認情況下以get方法開頭的都會序列化,如果不想序列化,在action中的方法要打上註解
@JSON(serialize=false)
[6.2]如果在屬性get方法上面加@JSON(name="newName"),則返回的json數據中名稱是指定的新名稱
[6.3]@JSON(format ="yyyy-MM-dd'T'HH:mm:ss")設置用於格式化輸出、解析日期表單域的格式。
[6.4]@JSON(deserialize=true)設置是否反序列化該屬性
㈤ struts2中,jsp頁面通過ajax訪問了action,action如何返回一個json數據給這個jsp頁面,
後台:
public class pageAction extends ActionSupport{
private String username;
private String password;
private String cmd;
public String execute(){
String result = "";
String message = "";
//創建流
PrintWriter out = null;
//創建json對象
JSONObject json = new JSONObject();
cmd = ServletActionContext.getRequest().getParameter("cmd");
username = ServletActionContext.getRequest().getParameter("username");
password = ServletActionContext.getRequest().getParameter("password");
//System.out.println("username:"+username+",password:"+password);
if("admin".equals(username) && "admin".equals(password)){
json.put("content", "true");
}else{
json.put("content", "輸入的賬號或密碼有誤!");
}
out.write(json.toString());
return SUCCESS;
}
}
前台:
<script type="text/javascript">
function checkAnswer() {
//獲得輸入的賬號和密碼的值
var username= document.getElementById("username").value;
var password= document.getElementById("password").value;
dataStr = {
checkname : username,
checkpass : password
};
jQuery
.ajax( {
type : "POST",
url : "ajax/checkAnswer.action?temp=" + Math.random(),
data : dataStr,
dataType : "json",
success : function(root) {
if ("true" == root.content) {
jQuery.messager.alert("操作提示", "登錄成功!", "info");
} else {
jQuery.messager.alert("操作提示", root.content, "info");
}
}});
}
</script>
試試吧,大體是這樣的格式,可能還會有一些小錯誤,注意下就好!
還要注意下在Structs里配置時:
<action name="seekcardAction" class="seekcardAction" method="returnResult">
<result type="json" /> //注意返回類型
</action>