Lucas_Cavalcanti 9 de fev. de 2011
o mesmo código só que com json() e sem o success gera o json sem a senha?
Lucas_Cavalcanti 9 de fev. de 2011
o extjs foi meio que um copy…paste do json, e como todo copy…paste foi com erros.
vou refatorá-lo pra usaro serializer de json, assim ele vai se comportar da mesma maneira.
[]'s
Lucas_Cavalcanti 9 de fev. de 2011
bittix, faz um teste pra mim por favor.
crie essa classe na sua aplicação e anote-a com @Component :
package br.com.caelum.vraptor.util.extjs ;
import java.io.IOException ;
import javax.servlet.http.HttpServletResponse ;
import br.com.caelum.vraptor.interceptor.TypeNameExtractor ;
import br.com.caelum.vraptor.serialization.ProxyInitializer ;
import br.com.caelum.vraptor.serialization.xstream.XStreamSerializer ;
import com.thoughtworks.xstream.XStream ;
import com.thoughtworks.xstream.io.json.JsonHierarchicalStreamDriver ;
public class FixedExtJson implements ExtJSJson {
private static class ExtJSWrapper {
private Object data ;
private Boolean success ;
private Integer total ;
This file has been truncated. show original
e execute:
result.use(ExtJSJson.class).from(usuariodao.lista()).success(true).exclude("senha").serialize();
bittix 10 de fev. de 2011
Oi Lucas, antes de mais nada muito obrigado pela resposta.
O resultado foi esse :
{"br.com.caelum.vraptor.util.extjs.FixedExtJson$ExtJSWrapper": {
"success": true
}}
Como se ele não consegue acessar internamente as propriedades do ExtJSWrapper.
garcia-jj 10 de fev. de 2011
Lucas, tem que colocar um withoutRoot para o ExtJS.
Lucas_Cavalcanti 10 de fev. de 2011
bittix, agora eu fiz a implementação com mais calma (e com testes!), então deve funcionar tranquilo.
copie a classe:
https://github.com/caelum/vraptor/blob/extjs/vraptor-core/src/main/java/br/com/caelum/vraptor/util/extjs/FixedExtJson.java
e anote-a com @Component .
Abraços
bittix 10 de fev. de 2011
Funcionou 100% Lucas, excelente.
Muito obrigado mesmo. :D
Aproveitei e incluí a propriedade "message" no mesmo esquema do total (que é um campo opcional do ExtJS 3.3), desculpe se não usei a convenção de nomes certa :) :
Interface :
package br.com.caelum.vraptor.util.extjs ;
import br.com.caelum.vraptor.View ;
public interface IFixedExtJson extends View {
public abstract IFixedExtJson from ( Object object );
public abstract IFixedExtJson exclude ( String ... names );
public abstract IFixedExtJson include ( String ... fields );
public abstract IFixedExtJson selected ( Object value );
public abstract IFixedExtJson serialize ();
public abstract IFixedExtJson success ();
public abstract IFixedExtJson success ( boolean success );
public abstract IFixedExtJson total ( Integer total );
public abstract IFixedExtJson message ( String message );
}
Implementação :
package br.com.caelum.vraptor.util.extjs ;
import java.io.IOException ;
import java.io.Writer ;
import java.util.ArrayList ;
import java.util.Collection ;
import java.util.List ;
import javax.servlet.http.HttpServletResponse ;
import br.com.caelum.vraptor.interceptor.TypeNameExtractor ;
import br.com.caelum.vraptor.ioc.Component ;
import br.com.caelum.vraptor.serialization.ProxyInitializer ;
import br.com.caelum.vraptor.serialization.xstream.XStreamSerializer ;
import com.thoughtworks.xstream.XStream ;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter ;
import com.thoughtworks.xstream.io.json.JsonHierarchicalStreamDriver ;
import com.thoughtworks.xstream.io.json.JsonWriter ;
@Component
public class FixedExtJson implements IFixedExtJson {
private static class ExtJSWrapper {
private Object data ;
private List < Object > list ;
private Boolean success ;
private String message ;
private Integer total ;
private Object selected ;
public ExtJSWrapper ( Object object ) {
if ( object instanceof Collection ) {
this . list = new ArrayList < Object > (( Collection ) object );
} else {
this . data = object ;
}
}
}
private XStreamSerializer serializer ;
private XStream xstream ;
private ExtJSWrapper wrapper ;
public FixedExtJson ( HttpServletResponse response , TypeNameExtractor extractor , ProxyInitializer initializer ) throws IOException {
xstream = new XStream ( new JsonHierarchicalStreamDriver () {
@Override
public HierarchicalStreamWriter createWriter ( Writer writer ) {
return new JsonWriter ( writer , new char [ 0 ] , "" , JsonWriter . DROP_ROOT_MODE ) {
@Override
public void addAttribute ( String key , String value ) {
if ( ! key . equals ( "class" )) {
super . addAttribute ( key , value );
}
}
};
}
});
xstream . aliasField ( "data" , ExtJSWrapper . class , "list" );
serializer = new XStreamSerializer ( xstream , response . getWriter (), extractor , initializer );
}
/* (non-Javadoc)
* @see br.com.caelum.vraptor.util.extjs.IFixedExtJson#from(java.lang.Object)
*/
@Override
public IFixedExtJson from ( Object object ) {
wrapper = new ExtJSWrapper ( object );
serializer . from ( object );
return this ;
}
/* (non-Javadoc)
* @see br.com.caelum.vraptor.util.extjs.IFixedExtJson#exclude(java.lang.String)
*/
@Override
public IFixedExtJson exclude ( String ... names ) {
serializer . exclude ( names );
return this ;
}
/* (non-Javadoc)
* @see br.com.caelum.vraptor.util.extjs.IFixedExtJson#include(java.lang.String)
*/
@Override
public IFixedExtJson include ( String ... fields ) {
serializer . include ( fields );
return this ;
}
/* (non-Javadoc)
* @see br.com.caelum.vraptor.util.extjs.IFixedExtJson#selected(java.lang.Object)
*/
@Override
public IFixedExtJson selected ( Object value ) {
wrapper . selected = value ;
return this ;
}
/* (non-Javadoc)
* @see br.com.caelum.vraptor.util.extjs.IFixedExtJson#serialize()
*/
@Override
public IFixedExtJson serialize () {
serializer . from ( wrapper ). recursive (). serialize ();
return this ;
}
/* (non-Javadoc)
* @see br.com.caelum.vraptor.util.extjs.IFixedExtJson#success()
*/
@Override
public IFixedExtJson success () {
wrapper . success = true ;
return this ;
}
/* (non-Javadoc)
* @see br.com.caelum.vraptor.util.extjs.IFixedExtJson#success(boolean)
*/
@Override
public IFixedExtJson success ( boolean success ) {
wrapper . success = success ;
return this ;
}
/* (non-Javadoc)
* @see br.com.caelum.vraptor.util.extjs.IFixedExtJson#total(java.lang.Integer)
*/
@Override
public IFixedExtJson total ( Integer total ) {
wrapper . total = total ;
return this ;
}
/* (non-Javadoc)
* @see br.com.caelum.vraptor.util.extjs.IFixedExtJson#message(java.lang.String)
*/
@Override
public IFixedExtJson message ( String message ) {
wrapper . message = message ;
return this ;
}
}
Teste :
result.use(FixedExtJson.class)
.from(usuariodao.lista())
.success(true)
.exclude("senha")
.message("Funcionou com sucesso")
.total(1)
.serialize();
Resultado :
{ "data" : [{ "id" : 1 , "nome" : "admin" , "login" : "admin" , "dominio" : "default" , "email" : "[email removido]" }], "success" : true , "message" : "Funcionou com sucesso" , "total" : 1 }
Lucas_Cavalcanti 10 de fev. de 2011
=)
a gente vai integrar esse código ao vraptor, daí vc vai poder usar simplesmente:
result.use(ExtJSJson.class)....
bittix 10 de fev. de 2011
Show de bola.
Valeu Lucas.
mascjunior 3 de out. de 2011
Lucas, Aproveitando este tópico gostaria de saber se foi integrado ao vraptor essa correção do exclude na serialização do json.
Pois estou tendo o mesmo problema do nosso amigo.
No aguardo
Lucas_Cavalcanti 3 de out. de 2011
teoricamente foi integrado sim… testa lá, e se der algum erro dá um toque
mascjunior 3 de out. de 2011
Lucas…
foi erro meu.
está funcionando corretamente sim.
Obrigado