struts2 json
2013-11-03 16:55
281 查看
1. 拷贝struts-2.3.4-all\lib下的
commons-beanutils-1.8.0.jar
commons-collections-3.1.jar
commons-lang3-3.1.jar
comm
4000
ons-logging-1.1.1.jar
ezmorph-1.0.6.jar
json-lib-2.3-jdk15.jar
struts2-json-plugin-2.3.4.jar
到工程web-inf/lib下
2. Action:
publicclass DeptAction {
private IDeptService
deptservice;
private String
result;//用来存放json
public String findAllDept(){
List<Department> depts =
new ArrayList<Department>();
deptservice =
new DeptServiceImpl();
depts =
deptservice.findAllDept();
JSONArray ja = new JSONArray();
for(Department d:depts){
JSONObject jobj=
new JSONObject();
jobj.accumulate("deptno",d.getDeptno());
jobj.accumulate("deptname",d.getDeptname());
ja.add(jobj);
}
///JSONArray.fromObject(depts).toString();第二种方式转json
result =
ja.toString();
return"success";
}
public IDeptService getDeptservice() {
returndeptservice;
}
publicvoid setDeptservice(IDeptService deptservice) {
this.deptservice = deptservice;
}
public String getResult() {
returnresult;
}
publicvoid setResult(String result) {
this.result = result;
}
}
3. STRUTS.XML
<package
name="default"
namespace="/"extends="json-default">
<action
name="deptinfo"
class="com.etc.web.DeptAction"method="findAllDept">
<result type="json"></result>
</action>
</package>
4.
$(document).ready(function(){
$.ajax({
url:"<%=basePath%>deptinfo",
type:"POST",
dataType:"json",
success:function(data){
var dept = eval("("+data.result+")");//将字符串转换到json对象
var
html="";
$.each(dept,function(i,p){
html+="<optionvalue='"+p.deptno+"'
>"+p.deptname+"</option>"
});
$("#deptinfo").html(html);
}
});
});
commons-beanutils-1.8.0.jar
commons-collections-3.1.jar
commons-lang3-3.1.jar
comm
4000
ons-logging-1.1.1.jar
ezmorph-1.0.6.jar
json-lib-2.3-jdk15.jar
struts2-json-plugin-2.3.4.jar
到工程web-inf/lib下
2. Action:
publicclass DeptAction {
private IDeptService
deptservice;
private String
result;//用来存放json
public String findAllDept(){
List<Department> depts =
new ArrayList<Department>();
deptservice =
new DeptServiceImpl();
depts =
deptservice.findAllDept();
JSONArray ja = new JSONArray();
for(Department d:depts){
JSONObject jobj=
new JSONObject();
jobj.accumulate("deptno",d.getDeptno());
jobj.accumulate("deptname",d.getDeptname());
ja.add(jobj);
}
///JSONArray.fromObject(depts).toString();第二种方式转json
result =
ja.toString();
return"success";
}
public IDeptService getDeptservice() {
returndeptservice;
}
publicvoid setDeptservice(IDeptService deptservice) {
this.deptservice = deptservice;
}
public String getResult() {
returnresult;
}
publicvoid setResult(String result) {
this.result = result;
}
}
3. STRUTS.XML
<package
name="default"
namespace="/"extends="json-default">
<action
name="deptinfo"
class="com.etc.web.DeptAction"method="findAllDept">
<result type="json"></result>
</action>
</package>
4.
$(document).ready(function(){
$.ajax({
url:"<%=basePath%>deptinfo",
type:"POST",
dataType:"json",
success:function(data){
var dept = eval("("+data.result+")");//将字符串转换到json对象
var
html="";
$.each(dept,function(i,p){
html+="<optionvalue='"+p.deptno+"'
>"+p.deptname+"</option>"
});
$("#deptinfo").html(html);
}
});
});
相关文章推荐
- struts2 验证框架
- struts2 拦截器
- Java同步注解(Annotation)解释
- Spring事务管理配置
- java类——Color
- j2se之数组的运用
- java泛型(二)、泛型的内部原理:类型擦除以及类型擦除带来的问题
- Hashmap与Trove4j
- java中闭包和回调
- java变量
- Java设计模式—单态模式(Singleton)
- Ubuntu系统下运行Eclipse出现找不到jre的问题的解决方法
- eclipse wifi 连接手机
- Java基础知识整理(一)- Error和Exception
- 简易版基于Java的Servlet容器实现
- java poi 大批量数据处理,2007
- eclipse的debug
- Spring事务隔离级别和事务传播行为
- spring framework源码下载并导入eclipse
- failed to create the java virtual machine