㈠ 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>