struts2-json-plugin-2.3.8.jar怎么用
答案:1 悬赏:0 手机版
解决时间 2021-01-16 04:37
- 提问者网友:精神病院里
- 2021-01-16 01:27
struts2-json-plugin-2.3.8.jar怎么用
最佳答案
- 五星知识达人网友:荒野風
- 2021-01-16 03:02
由于这是一个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配置文件:
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
3、简单配置说明
在struts中使用 struts2-json-plugin-2.2.3.jar 需要将要返回json的action配置在
...
...
中,注意 extends="json-default"继承的是“json-default" 我们正常配置struts是使用‘struts-default’,所以需要将返回json的action和普通action分开使用两个package进行配置
...
...
...
...
在json package中的acton配置
便可以成功的转化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配置文件:
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
3、简单配置说明
在struts中使用 struts2-json-plugin-2.2.3.jar 需要将要返回json的action配置在
...
...
中,注意 extends="json-default"继承的是“json-default" 我们正常配置struts是使用‘struts-default’,所以需要将返回json的action和普通action分开使用两个package进行配置
...
...
...
...
在json package中的acton配置
便可以成功的转化json,结果如
{"nowd":"2012-12-27T22:23:13","student":{"stuAge":null,"stuName":"李老师","stuSex":null},"teacher":{"teacAge":"100","teacName":"张三","teacSex":"男男"}}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯