本文将为您提供关于json+struts2的详细介绍,同时,我们还将为您提供关于json数据传到前台报错:org.apache.struts2.json.JSONException、JSON格式输出S
本文将为您提供关于json+struts2的详细介绍,同时,我们还将为您提供关于json数据传到前台报错:org.apache.struts2.json.JSONException、JSON格式输出Struts2、Struts2 + Json _ 配置,异常解决及深入了解Struts2返回JSON数据的原理及具体应用范例、struts2 + json 包冲突问题的实用信息。
本文目录一览:- json+struts2
- json数据传到前台报错:org.apache.struts2.json.JSONException
- JSON格式输出Struts2
- Struts2 + Json _ 配置,异常解决及深入了解Struts2返回JSON数据的原理及具体应用范例
- struts2 + json 包冲突问题
json+struts2
------第一种方式:未extends="json-default"-------------
-------------------------action类-----------------------------------
public void getJson(){
try {HttpServletRequest request=ServletActionContext.getRequest();
HttpServletResponse response=ServletActionContext.getResponse();
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
List list=new ArrayList();
list.add("ly");
list.add("ly1");
list.add("ly2");
list.add("ly3");
list.add("ly4");
list.add("ly5");
PrintWriter out=response.getWriter();
JSONArray JList=JSONArray.fromObject(list);
String jsonS=JList.toString();
out.write(jsonS);
out.flush();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
----------------------------------配置文件-------------------------------------
<action name="testJsonAction_getJson"method="getJson"></action>
---------------第二种方式:使用内置的json extends=“json-defalut”
我的其他博客 已写
json数据传到前台报错:org.apache.struts2.json.JSONException
错误描述:json数据无法从action传到前台 ,报以下错误:
错误信息:
HTTP Status 500 - org.apache.struts2.json.JSONException:
org.apache.struts2.json.JSONException:
org.apache.struts2.json.JSONException:
org.apache.struts2.json.JSONException:
org.apache.struts2.json.JSONException:
org.apache.struts2.json.JSONException:
org.apache.struts2.json.JSONException: java.lang.IllegalAccessException:
Class org.apache.struts2.json.JSONWriter can not access a member of
class
org.hibernate.bytecode.internal.javassist.BytecodeProviderImpl$EntityInstrumentationMetadataImpl
with modifiers "public"
exception
org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: java.lang.IllegalAccessException: Class org.apache.struts2.json.JSONWriter can not access a member of class org.hibernate.bytecode.internal.javassist.BytecodeProviderImpl$EntityInstrumentationMetadataImpl with modifiers "public"
org.apache.struts2.json.JSONWriter.bean(JSONWriter.java:230)
org.apache.struts2.json.JSONWriter.process(JSONWriter.java:160)
org.apache.struts2.json.JSONWriter.value(JSONWriter.java:126)
org.apache.struts2.json.JSONWriter.write(JSONWriter.java:94)
org.apache.struts2.json.JSONUtil.serialize(JSONUtil.java:115)
org.apache.struts2.json.JSONResult.createJSONString(JSONResult.java:200)
org.apache.struts2.json.JSONResult.execute(JSONResult.java:172)
com.opensymphony.xwork2.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:371)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:275)
org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:256)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:167)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:265)
org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:138)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:239)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:239)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:191)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:73)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:91)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:252)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:100)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:141)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:145)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:171)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:161)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:193)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:189)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:54)
org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:563)
org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:99)
错误原因:配置文件写错,少写了<param name="root">jsonData</param>,导致前台接收不到东西,弱智错误
<result name="success" type="json">
<param name="root">jsonData</param>
</result>
JSON格式输出Struts2
public void JSON(Object object,String charset){
getHttpResponse().setCharacterEncoding(charset);
getHttpResponse().setContentType("text/html;charset="+charset);
try {
JSONUtil.serialize(getHttpResponse().getWriter(), object);
getHttpResponse().getWriter().flush();
getHttpResponse().getWriter().close();
} catch (Exception e) {
e.printStackTrace();
}
}
Struts2 + Json _ 配置,异常解决及深入了解Struts2返回JSON数据的原理及具体应用范例
在struts2的基础上 配置 struts2还需要的包:
commons-lang-2.4.jar;
json-lib-2.3-jdk13.jar;
jsonplugin-0[1].32.jar;
ezmorph-1.0.2.jar;
commons-beanutils-1.7.0.jar;
另外,可以通过: @JSON(name="mymessage") 来给 action 中的get属性设 json节点 别名
通过给 属性设置: @JSON(serialize=false) 来给控制 哪些属性 不序列化输出.
我的异常就是: 因为我的 action 中有 service 接口对象属性,所以导致 序列化成 json 失败 而异常了
多了去了,
root:只包含xx对象
excludeProperties :除了xx之外的集合
includeProperties : 包含xx的集合
wrapPrefix :json结果开头添加xx(比如<paramname="wrapPrefix">[</param>)
wrapSuffix :同上,只不过是结尾
ignoreHierarchy :转化时是否忽略继承关系true/false
enumAsBean : 是否将枚举当做一个bean处理
enableGZIP :是否支持gzip压缩输出
noCache :是否缓存
excludeNullProperties : 是否转化输出 null值
statusCode :设置响应代号比如 304
callbackParameter : JSONP跨域访问的回调设置
contentType : 输出类型
struts2 + json 包冲突问题
在用 strtus2 的 json 插件读取数据时,出现了包冲突的问题,搞了老半天才解决,
严重: Servlet.service () for servlet default threw exception
java.lang.NoClassDefFoundError: com/opensymphony/xwork2/util/TextUtils
at com.googlecode.jsonplugin.JSONUtil.writeJSONToResponse(JSONUtil.java:197)
at com.googlecode.jsonplugin.JSONResult.writeToResponse(JSONResult.java:192)
at com.googlecode.jsonplugin.JSONResult.execute(JSONResult.java:182)
我看了下 xwork-core-2.1.6.jar 下是没有 com.opensymphony.xwork2.util.TextUtils 这个类的,只有 struts2.0 的 xwork 才有,可是我的工程师 struts2.1 的,直接把以前的 xwork2 包弄进来是会报错的。百度看了下大概原因就是 jar 包不兼容。
CSDN 案例:http://topic.csdn.net/u/20110422/17/a1440f07-f0c0-40d1-a572-f81e776ab180.html
但是没解决我的问题,然后在百度找到我解决方案:http://hi.baidu.com/ryan_lz/blog/item/fdc0340be00a958cd0581b19.html
在 struts2 的官方上面说 struts2.1.x 要配合 jsonplugin-0.33.jar 使用
struts2.0.x 配合 jsonplugin-0.32.jar
我用的是 struts2.1.8 和 jsonplugin-0.33.jar 但是还是出现上面那个错误,后来经过高人的指点终于解决了这个问题解决方案:将 jsonplugin-0.33.jar 替换成 json-lib-2.1.jar、struts2-json-plugin-2.1.8.1.jar、struts2-junit-plugin-2.1.8.1.jar 使用这三个包就不会出现冲突了
今天关于json+struts2的讲解已经结束,谢谢您的阅读,如果想了解更多关于json数据传到前台报错:org.apache.struts2.json.JSONException、JSON格式输出Struts2、Struts2 + Json _ 配置,异常解决及深入了解Struts2返回JSON数据的原理及具体应用范例、struts2 + json 包冲突问题的相关知识,请在本站搜索。
本文标签: