• 欢迎浏览“String me = Creater\忠实的资深Linux玩家;”,请文明浏览,理性发言,有侵犯你的权益请邮件我(creater@vip.qq.com).
  • 把任何的失败都当作一次尝试,不要自卑;把所有的成功都想成是一种幸运,不要自傲。
  •    3年前 (2015-07-13)  Android |   抢沙发  3 
    文章评分 0 次,平均分 0.0

    首先定义一个实体类User用于测试,还有一个Info类。

    package com.demo.bean;
    
    public class Info {
    	public int id;
    	public String addr;
    	public int getId() {
    		return id;
    	}
    	public void setId(int id) {
    		this.id = id;
    	}
    	public String getAddr() {
    		return addr;
    	}
    	public void setAddr(String addr) {
    		this.addr = addr;
    	}
    	public String getSchool() {
    		return school;
    	}
    	public void setSchool(String school) {
    		this.school = school;
    	}
    	public String school;
    }
    
    package com.demo.bean;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class User {
    
    	private static final long serialVersionUID = 1L; 
    	  public int id;
    	  public String name;
    	  public String title;
    	  public List<Info> info = new ArrayList<Info>();
    
    	public List<Info> getInfo() {
    		return info;
    	}
    	public void setInfo(List<Info> info) {
    		this.info = info;
    	}
    	public int getId() {
    		return id;
    	}
    	public void setId(int id) {
    		this.id = id;
    	}
    	public String getName() {
    		return name;
    	}
    	public void setName(String name) {
    		this.name = name;
    	}
    	public String getTitle() {
    		return title;
    	}
    	public void setTitle(String title) {
    		this.title = title;
    	}
    }
    
    

    在IndexController中展开测试:
    1.

    		user.id = 1;
    		user.name = "Creater";//or set
    		user.title = "Love Linux";
    		user.info.setAddr("四川绵阳");
    		user.info.setId(2);
    		user.info.setSchool("SWUST");
    		setAttr("user",user);
    		renderJson();

    测试结果:

    {"user":{"id":1,"title":"Love Linux","name":"Creater","info":{"id":2,"school":"SWUST","addr":"四川绵阳"}}}

    2.

    		User user = new User();
    		user.id = 1;
    		user.name = "Creater";//or set
    		user.title = "Love Linux";
    		user.info.setAddr("四川绵阳");
    		user.info.setId(2);
    		user.info.setSchool("SWUST");
    		setAttr("user",user);
    		
    		User user1 = new User();
    		user1.id = 1;
    		user1.name = "Creater1";//or set
    		user1.title = "Love Linux1";
    		user1.info.setAddr("四川绵阳1");
    		user1.info.setId(2);
    		user1.info.setSchool("SWUST1");
    		setAttr("user1",user1);
    		renderJson();

    测试结果:

    {"user1":{"id":1,"title":"Love Linux1","name":"Creater1","info":{"id":2,"school":"SWUST1","addr":"四川绵阳1"}},"user":{"id":1,"title":"Love Linux","name":"Creater","info":{"id":2,"school":"SWUST","addr":"四川绵阳"}}}

    3.

    		setAttr("user","creater");
    		setAttr("name","renzhenwen");
    		renderJson(new String[]{"user","name"});

    测试结果:

    {"name":"renzhenwen","user":"creater"}

    4.

    		user.id = 1;
    		user.name = "Creater";//or set
    		user.title = "Love Linux";
    		user.info.setAddr("四川绵阳");
    		user.info.setId(2);
    		user.info.setSchool("SWUST");
    		renderJson(user);

    测试结果:

    {"id":1,"title":"Love Linux","name":"Creater","info":{"id":2,"school":"SWUST","addr":"四川绵阳"}}

    4.

    renderJson("{\"age\":18} ");

    测试结果:

    {"age":18} 

    4.

    User user = new User();
    		
    		user.id = 1;
    		user.name = "Creater";//or set
    		user.title = "Love Linux";
    		for(int i = 0; i < 3; ++i){
    			Info info = new Info();
    			info.setAddr("四川绵阳"+i);
    			info.setId(i);
    			info.setSchool("SWUST"+i);
    			user.info.add(info);
    		}
    		setAttr("user",user);
    		renderJson();

    测试结果:

    {"user":{"id":1,"title":"Love Linux","name":"Creater","info":[{"id":0,"school":"SWUST0","addr":"四川绵阳0"},{"id":1,"school":"SWUST1","addr":"四川绵阳1"},{"id":2,"school":"SWUST2","addr":"四川绵阳2"}]}}

    5.

    		User user = new User();
    		
    		user.id = 1;
    		user.name = "Creater";//or set
    		user.title = "Love Linux";
    		for(int i = 0; i < 3; ++i){
    			Info info = new Info();
    			info.setAddr("四川绵阳"+i);
    			info.setId(i);
    			info.setSchool("SWUST"+i);
    			user.info.add(info);
    		}
    		renderJson("test",user);

    测试结果:

    {"test":{"id":1,"title":"Love Linux","name":"Creater","info":[{"id":0,"school":"SWUST0","addr":"四川绵阳0"},{"id":1,"school":"SWUST1","addr":"四川绵阳1"},{"id":2,"school":"SWUST2","addr":"四川绵阳2"}]}}

    jfinal_demo

     

    除特别注明外,本站所有文章均为String me = "Creater\忠实的资深Linux玩家";原创,转载请注明出处来自http://unix8.net/home.php/4115.html

    关于

    发表评论

    暂无评论

    切换注册

    登录

    忘记密码 ?

    切换登录

    注册

    扫一扫二维码分享