| Remover |
<td><s:property value="id"/></td>
<s:url id="alterarPessoa" method="carregarAlterar" action="pessoa">
<s:param name="pessoa.id" value="top.id"/>
</s:url>
<td><s:a href="%{alterarPessoa}"><s:property value="nome"/></s:a></td>
</tr>
</s:iterator>
</s:form>
PessoaAction.java:
/**
*
*/
package br.com.stefanini.treinamento.talento.struts.action;
import java.util.List;
import javax.annotation.Resource;
import org.apache.struts2.config.Namespace;
import org.apache.struts2.config.ParentPackage;
import org.apache.struts2.config.Result;
import org.apache.struts2.config.Results;
import br.com.stefanini.treinamento.talento.entity.Pessoa;
import br.com.stefanini.treinamento.talento.service.IPessoaService;
import com.opensymphony.xwork2.ActionSupport;
/**
*/
@Results ({
@Result (name=PessoaAction.RESULT_INSERIR, value="/pessoa/inserirPessoa.jsp"),
@Result (name=PessoaAction.RESULT_ALTERAR, value="/pessoa/alterarPessoa.jsp")
})
@ParentPackage (“struts-talento”)
@Namespace ("/")
public class PessoaAction extends ActionSupport {
protected static final String RESULT_INSERIR = "RESULT_INSERIR";
protected static final String RESULT_ALTERAR = "RESULT_ALTERAR";
private IPessoaService pessoaService;
private Pessoa pessoa;
public String carregarInserir() {
return RESULT_INSERIR;
}
public String inserir() {
try {
getPessoaService().inserir(getPessoa());
} catch (Exception e) {
e.printStackTrace();
addActionError(e.getMessage());
}
return RESULT_INSERIR;
}
public String alterar() {
try {
Pessoa pessoaExistente = getPessoaService().findById(getPessoa().getId());
pessoaExistente.setNome(getPessoa().getNome());
getPessoaService().alterar(pessoaExistente);
} catch (Exception e) {
e.printStackTrace();
addActionError(e.getMessage());
}
return RESULT_INSERIR;
}
public String carregarAlterar() {
setPessoa(getPessoaService().findById(getPessoa().getId()));
return RESULT_ALTERAR;
}
public String remover() {
try {
getPessoaService().remove(getPessoa());
} catch (Exception e) {
e.printStackTrace();
addActionError(e.getMessage());
}
return RESULT_INSERIR;
}
public Pessoa getPessoa() {
if (this.pessoa == null) {
this.pessoa = new Pessoa();
}
return this.pessoa;
}
public void setPessoa(Pessoa pessoa) {
this.pessoa = pessoa;
}
public List<Pessoa> getPessoas() {
return getPessoaService().getTodos();
}
public IPessoaService getPessoaService() {
return pessoaService;
}
@Resource(name="PessoaService")
public void setPessoaService(IPessoaService pessoaService) {
this.pessoaService = pessoaService;
}
}
Entyti:
/**
*
*/
package br.com.stefanini.treinamento.talento.entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
/**
*/
@Entity
public class Pessoa extends AbstractEntity {
private Long id;
private String nome;
private String sexo;
@Id
@GeneratedValue
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Column
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
@Column
public String getSexo() {
return sexo;
}
public void setSexo(String sexo) {
this.sexo = sexo;
}
public void inserir(Pessoa pessoaService) {
// TODO Auto-generated method stub
}
}
DAO:
/**
*
*/
package br.com.stefanini.treinamento.talento.dao.impl;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.springframework.stereotype.Repository;
import br.com.stefanini.treinamento.talento.dao.IPessoaDAO;
import br.com.stefanini.treinamento.talento.entity.Pessoa;
/**
*/
@Repository (“PessoaDAO”)
public class PessoaDAOImpl extends AbstractEntityDAOImpl implements IPessoaDAO {
@Override
protected Class getEntityClass() {
return Pessoa.class;
}
}
Service:
/**
*
*/
package br.com.stefanini.treinamento.talento.service.impl;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import br.com.stefanini.treinamento.talento.dao.IEntityDAO;
import br.com.stefanini.treinamento.talento.entity.Pessoa;
import br.com.stefanini.treinamento.talento.service.IPessoaService;
/**
*/
@Service(“PessoaService”)
public class PessoaServiceImpl extends AbstractEntityServiceImpl implements IPessoaService {
@Override
@Transactional (propagation=Propagation.REQUIRED)
public void inserir(Pessoa entity) {
if (entity.getNome() == null) {
throw new IllegalArgumentException("O nome nao foi preenchido.");
}
super.inserir(entity);
}
@Override
@Resource (name="PessoaDAO")
protected void setDao(IEntityDAO<Pessoa> dao) {
super.setDao(dao);
}
}
Isso é apenmas uma agenda, mas não consigo fazer as ligações.