struts2-json-plugin-2.3.8.jar怎么用
1个回答
2016-01-11 · 知道合伙人数码行家
huanglenzhi
知道合伙人数码行家
向TA提问 私信TA
知道合伙人数码行家
采纳数:117538
获赞数:517193
长期从事计算机组装,维护,网络组建及管理。对计算机硬件、操作系统安装、典型网络设备具有详细认知。
向TA提问 私信TA
关注
展开全部
由于这是一个struts插件,所以必须要有struts才可以。
2、我的java文件:
StudentEntity.java
import java.io.Serializable;
public class StudentEntity implements Serializable {
private String stuName;
private String stuAge;
private String stuSex;
public String getStuAge() {
return stuAge;
}
public void setStuAge(String stuAge) {
this.stuAge = stuAge;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public String getStuSex() {
return stuSex;
}
public void setStuSex(String stuSex) {
this.stuSex = stuSex;
}
}
TeacherEntity.java
import java.io.Serializable;
public class TeacherEntity implements Serializable {
private String teacName;
private String teacAge;
private String teacSex;
public String getTeacAge() {
return teacAge;
}
public void setTeacAge(String teacAge) {
this.teacAge = teacAge;
}
public String getTeacName() {
return teacName;
}
public void setTeacName(String teacName) {
this.teacName = teacName;
}
public String getTeacSex() {
return teacSex;
}
public void setTeacSex(String teacSex) {
this.teacSex = teacSex;
}
}
StrIndex.java(这是一个action,命名不规范)
import java.util.Date;
import org.apache.struts2.json.annotations.JSON;
import com.opensymphony.xwork2.ActionSupport;
public class StrIndex extends ActionSupport {
private TeacherEntity teacher=new TeacherEntity();
private StudentEntity student=new StudentEntity();
private Date nowd=new Date();
public String toIndex(){
teacher.setTeacName("张三");
teacher.setTeacAge("100");
teacher.setTeacSex("男男");
student.setStuName("李老师");
return SUCCESS;
}
// 是否转换该对象
// @JSON(serialize=true)
// @JSON(name="newName")
public StudentEntity getStudent() {
return student;
}
public void setStudent(StudentEntity student) {
this.student = student;
}
public TeacherEntity getTeacher() {
return teacher;
}
public void setTeacher(TeacherEntity teacher) {
this.teacher = teacher;
}
@JSON(format="yyyy-MM-dd")
public Date getNowd() {
return nowd;
}
public void setNowd(Date nowd) {
this.nowd = nowd;
}
}
紧接着是我的struts配置文件:
<?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>
<package name="test-default" extends="json-default">
<action name="ind" class="StrIndex" method="toIndex">
<result name="success" type="json">
</result>
</action>
</package>
</struts>
3、简单配置说明
在struts中使用 struts2-json-plugin-2.2.3.jar 需要将要返回json的action配置在
<package name="test-default" extends="json-default">
...
...
</package>
中,注意 extends="json-default"继承的是“json-default" 我们正常配置struts是使用‘struts-default’,所以需要将返回json的action和普通action分开使用两个package进行配置
<package name="test-default" extends="json-default">
...
...
</package>
<package name="struts-default" extends="struts-default">
...
...
</package>
在json package中的acton配置
<result name="success" type="json"></result>
便可以成功的转化json,结果如
{"nowd":"2012-12-27T22:23:13","student":{"stuAge":null,"stuName":"李老师","stuSex":null},"teacher":{"teacAge":"100","teacName":"张三","teacSex":"男男"}}
2、我的java文件:
StudentEntity.java
import java.io.Serializable;
public class StudentEntity implements Serializable {
private String stuName;
private String stuAge;
private String stuSex;
public String getStuAge() {
return stuAge;
}
public void setStuAge(String stuAge) {
this.stuAge = stuAge;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public String getStuSex() {
return stuSex;
}
public void setStuSex(String stuSex) {
this.stuSex = stuSex;
}
}
TeacherEntity.java
import java.io.Serializable;
public class TeacherEntity implements Serializable {
private String teacName;
private String teacAge;
private String teacSex;
public String getTeacAge() {
return teacAge;
}
public void setTeacAge(String teacAge) {
this.teacAge = teacAge;
}
public String getTeacName() {
return teacName;
}
public void setTeacName(String teacName) {
this.teacName = teacName;
}
public String getTeacSex() {
return teacSex;
}
public void setTeacSex(String teacSex) {
this.teacSex = teacSex;
}
}
StrIndex.java(这是一个action,命名不规范)
import java.util.Date;
import org.apache.struts2.json.annotations.JSON;
import com.opensymphony.xwork2.ActionSupport;
public class StrIndex extends ActionSupport {
private TeacherEntity teacher=new TeacherEntity();
private StudentEntity student=new StudentEntity();
private Date nowd=new Date();
public String toIndex(){
teacher.setTeacName("张三");
teacher.setTeacAge("100");
teacher.setTeacSex("男男");
student.setStuName("李老师");
return SUCCESS;
}
// 是否转换该对象
// @JSON(serialize=true)
// @JSON(name="newName")
public StudentEntity getStudent() {
return student;
}
public void setStudent(StudentEntity student) {
this.student = student;
}
public TeacherEntity getTeacher() {
return teacher;
}
public void setTeacher(TeacherEntity teacher) {
this.teacher = teacher;
}
@JSON(format="yyyy-MM-dd")
public Date getNowd() {
return nowd;
}
public void setNowd(Date nowd) {
this.nowd = nowd;
}
}
紧接着是我的struts配置文件:
<?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>
<package name="test-default" extends="json-default">
<action name="ind" class="StrIndex" method="toIndex">
<result name="success" type="json">
</result>
</action>
</package>
</struts>
3、简单配置说明
在struts中使用 struts2-json-plugin-2.2.3.jar 需要将要返回json的action配置在
<package name="test-default" extends="json-default">
...
...
</package>
中,注意 extends="json-default"继承的是“json-default" 我们正常配置struts是使用‘struts-default’,所以需要将返回json的action和普通action分开使用两个package进行配置
<package name="test-default" extends="json-default">
...
...
</package>
<package name="struts-default" extends="struts-default">
...
...
</package>
在json package中的acton配置
<result name="success" type="json"></result>
便可以成功的转化json,结果如
{"nowd":"2012-12-27T22:23:13","student":{"stuAge":null,"stuName":"李老师","stuSex":null},"teacher":{"teacAge":"100","teacName":"张三","teacSex":"男男"}}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询