mendigosujo 11 de jun. de 2008
faz a pesquisa pelo ID do objeto…
public Object findByID ( Integer id ) {
Object object = null ;
transaction = session .beginTransaction () ;
object = session .get ( classe , id ) ;
transaction .commit () ;
return object ;
}
Depois vc faz um cast pro seu objeto e seta ele pra ser visto!
[]'s
diogoprosoft 11 de jun. de 2008
o meu dao para fazer isso esta assim
public Object buscar ( Class clazz , Long pk ) throws Exception
{
sessao = HibernateFactory . getSession ();
transacao = sessao . beginTransaction ();
Conta objt = ( Conta ) sessao . load ( clazz , pk );
transacao . commit ();
sessao . flush ();
sessao . close ();
return objt ;
}
[ /code
depois tenho minha action onde eu nao estou conseguindo implementar
[code ]
public ActionForward buscar ( ActionMapping mapping , ActionForm form ,
HttpServletRequest request , HttpServletResponse response )
throws Exception {
PersistenciaDAO persistencia = new PersistenciaDAO ();
return mapping . findForward ( LISTAR );
}
tenho o jsp
<html:form action= "conta" focus= "idconta" >
Id:<html:text property= "idconta" />
Data_Cadastro<html:text property= "datacadastro" />
Nome:<html:text property= "nome" />
Obs:<html:text property= "obs" />
<html:submit property= "method" ><bean:message key= "button.salvar" /></html:submit>
<html:submit property= "method" ><bean:message key= "button.deletar" /></html:submit>
<html:submit property= "method" ><bean:message key= "button.buscar" /></html:submit>
</html:form>
vou colocar tbm o struts-config
<action input= "/conta.jsp" name= "ContaForm" parameter= "method" path= "/conta" scope= "request" type= "conta.action.ContaAction" >
<forward name= "success" path= "/conta.jsp" />
</action>
Está tudo ai minha dificuldade esta em criar a action e nao sei se o meu dao e o jsp estao corretos, aguardo respota
mendigosujo 11 de jun. de 2008
ta pronto ai
public ActionForward buscar ( ActionMapping mapping , ActionForm form ,
HttpServletRequest request , HttpServletResponse response )
throws Exception {
SeuForm formulario = ( ActionForm ) form ;
// setar seu formulario no bean se vc fizer assim
PersistenciaDAO persistencia = new PersistenciaDAO () ;
SeuBean seuBean = ( SeuBean ) persistencia .buscar ( SuaClasseMapeada .class , formulario .getId ()) ;
// adicionar na requicao
return mapping .findForward ( LISTAR ) ;
}
diogoprosoft 11 de jun. de 2008
Cara agradeço a sua ajuda desde já
ficou assim a action
public ActionForward buscar ( ActionMapping mapping , ActionForm form ,
HttpServletRequest request , HttpServletResponse response )
throws Exception {
HttpSession session = request .getSession () ;
ContaForm contaForm = ( ContaForm ) form ;
Conta conta = new Conta () ;
BeanUtils .copyProperties ( conta , contaForm ) ;
PersistenciaDAO persistencia = new PersistenciaDAO () ;
conta = ( Conta ) persistencia .buscar ( Conta .class , contaForm .getIdconta ()) ;
return mapping .findForward ( SUCCESS ) ;
}
não funcionou ainda, o meu jsp que coloquei logo assim está certo?
está dando um erro quando coloco somente o codigo o erro pede para converter o sql da data vou colocar o meu form aqui tbm pra dar uma olhada
public class ContaForm extends org . apache . struts . action . ActionForm {
private Long idconta ;
private Date datacadastro ;
private String nome = "" ;
private String obs = "" ;
Conta conta = new Conta ();
public Long getIdconta () {
return idconta ;
}
public void setIdconta ( Long idconta ) {
this . idconta = idconta ;
}
public Date getDatacadastro () {
return datacadastro ;
}
public void setDatacadastro ( Date datacadastro ) {
this . datacadastro = datacadastro ;
}
public String getNome () {
return nome ;
}
public void setNome ( String nome ) {
this . nome = nome ;
}
public String getObs () {
return obs ;
}
public void setObs ( String obs ) {
this . obs = obs ;
}
public ActionErrors validate ( ActionMapping mapping , HttpServletRequest request ) {
ActionErrors errors = new ActionErrors ();
if ( getNome () == null || getNome (). length () < 1 ) {
errors . add ( "nome" , new ActionMessage ( "error.nome.required" ));
}
return errors ;
}
}
mendigosujo 11 de jun. de 2008
ql erro que ta ocorrendo?
ps.: to indo embora…qdo eu chegar em casa eu posto o código de solução para vc…
[]'s
diogoprosoft 11 de jun. de 2008
Bom o erro que está dando é na hora em que eu vou digitar o codigo e mandar buscar, ai aparece o erro dizendo que a data nao pode ser string deve ser um java.sql.Date, ai preencho a data clico em buscar nem processa entao preencho o nome e a obs clico em buscar faz o processamento e aparece outro erro, vou colocar o da data primeiro logo abaixo esta o segundo erro
Erro da data
javax.servlet.ServletException: BeanUtils.populate
org.apache.struts.util.RequestUtils.populate(RequestUtils.java:495)
org.apache.struts.action.RequestProcessor.processPopulate(RequestProcessor.java:816)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:203)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:390)
root cause
org.apache.commons.beanutils.ConversionException
org.apache.commons.beanutils.converters.SqlDateConverter.convert(SqlDateConverter.java:162)
org.apache.commons.beanutils.ConvertUtils.convert(ConvertUtils.java:379)
org.apache.commons.beanutils.BeanUtils.setProperty(BeanUtils.java:1001)
org.apache.commons.beanutils.BeanUtils.populate(BeanUtils.java:808)
org.apache.struts.util.RequestUtils.populate(RequestUtils.java:493)
org.apache.struts.action.RequestProcessor.processPopulate(RequestProcessor.java:816)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:203)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:390)
Segundo erro
javax . servlet . ServletException : java . lang . reflect . InvocationTargetException
org . apache . struts . action . RequestProcessor . processException ( RequestProcessor . java : 535 )
org . apache . struts . action . RequestProcessor . processActionPerform ( RequestProcessor . java : 433 )
org . apache . struts . action . RequestProcessor . process ( RequestProcessor . java : 236 )
org . apache . struts . action . ActionServlet . process ( ActionServlet . java : 1196 )
org . apache . struts . action . ActionServlet . doPost ( ActionServlet . java : 432 )
javax . servlet . http . HttpServlet . service ( HttpServlet . java : 710 )
javax . servlet . http . HttpServlet . service ( HttpServlet . java : 803 )
org . netbeans . modules . web . monitor . server . MonitorFilter . doFilter ( MonitorFilter . java : 390 )
root cause
java . lang . reflect . InvocationTargetException
sun . reflect . NativeMethodAccessorImpl . invoke0 ( Native Method )
sun . reflect . NativeMethodAccessorImpl . invoke ( NativeMethodAccessorImpl . java : 39 )
sun . reflect . DelegatingMethodAccessorImpl . invoke ( DelegatingMethodAccessorImpl . java : 25 )
java . lang . reflect . Method . invoke ( Method . java : 597 )
org . apache . commons . beanutils . PropertyUtils . getSimpleProperty ( PropertyUtils . java : 1185 )
org . apache . commons . beanutils . BeanUtils . copyProperties ( BeanUtils . java : 262 )
conta . action . ContaAction . buscar ( ContaAction . java : 86 )
sun . reflect . NativeMethodAccessorImpl . invoke0 ( Native Method )
sun . reflect . NativeMethodAccessorImpl . invoke ( NativeMethodAccessorImpl . java : 39 )
sun . reflect . DelegatingMethodAccessorImpl . invoke ( DelegatingMethodAccessorImpl . java : 25 )
java . lang . reflect . Method . invoke ( Method . java : 597 )
org . apache . struts . actions . DispatchAction . dispatchMethod ( DispatchAction . java : 270 )
org . apache . struts . actions . DispatchAction . execute ( DispatchAction . java : 187 )
org . apache . struts . actions . LookupDispatchAction . execute ( LookupDispatchAction . java : 150 )
org . apache . struts . action . RequestProcessor . processActionPerform ( RequestProcessor . java : 431 )
org . apache . struts . action . RequestProcessor . process ( RequestProcessor . java : 236 )
org . apache . struts . action . ActionServlet . process ( ActionServlet . java : 1196 )
org . apache . struts . action . ActionServlet . doPost ( ActionServlet . java : 432 )
javax . servlet . http . HttpServlet . service ( HttpServlet . java : 710 )
javax . servlet . http . HttpServlet . service ( HttpServlet . java : 803 )
org . netbeans . modules . web . monitor . server . MonitorFilter . doFilter ( MonitorFilter . java : 390 )
Cara novamente vou te agradecer pela ajuda