Oi pessoal, sou novato em j2ee e gostaria de passar meu sistema swing para jsp, tenho um exemplo em mvc swing a seguir:
no pacote controller
public class ContatoController {
private Date formatarData(String data) throws ParseException {
DateFormat formatter = new SimpleDateFormat(“dd/MM/yyyy”);
return new Date( formatter.parse(data).getTime() );
}
public void salvar(String nome, String apelido, String dtNascimento) throws SQLException, ParseException {
Contato contato = new Contato();
contato.setNome(nome);
contato.setApelido(apelido);
contato.setDtNascimento(formatarData(dtNascimento));
new ContatoDao().salvar(contato);
}
public void alterar(long id, String nome, String apelido, String dtNascimento) throws ParseException, SQLException {
Contato contato = new Contato();
contato.setId(id);
contato.setNome(nome);
contato.setApelido(apelido);
contato.setDtNascimento(formatarData(dtNascimento));
new ContatoDao().alterar(contato);
}
public List<Contato> listaContatos() {
ContatoDao dao = new ContatoDao();
try {
return dao.findContatos();
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, "Problemas ao localizar contato\n" + e.getLocalizedMessage());
}
return null;
}
public void excluir(long id) throws SQLException {
new ContatoDao().excluir(id);
}
public Contato buscaContatoPorNome(String nome) throws SQLException {
ContatoDao dao = new ContatoDao();//acessa a classe ContatoDao metodo findByName
return dao.findByName(nome);
}
}
no pacote model
public class ContatoDao extends GenericDao {
public void salvar(Contato contato) throws SQLException {
String insert = "INSERT INTO CONTATOS(nome, apelido, data_nascimento) VALUES(?,?,?)";
save(insert, contato.getNome(), contato.getApelido(), contato.getDtNascimento());
}
public void alterar(Contato contato) throws SQLException {
String update = "UPDATE CONTATOS " +
"SET nome = ?, apelido = ?, data_nascimento = ? " +
"WHERE id = ?";
update(update, contato.getId(), contato.getNome(), contato.getApelido(), contato.getDtNascimento());
}
public void excluir(long id) throws SQLException {
String delete = "DELETE FROM CONTATOS WHERE id = ?";
delete(delete, id);
}
public List<Contato> findContatos() throws SQLException {
List<Contato> contatos = new ArrayList<Contato>();
String select = "SELECT * FROM CONTATOS";
PreparedStatement stmt = getConnection().prepareStatement(select);
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
Contato contato = new Contato();
contato.setId(rs.getLong("id"));
contato.setNome(rs.getString("nome"));
contato.setApelido(rs.getString("apelido"));
contato.setDtNascimento(rs.getDate("data_nascimento"));
contatos.add(contato);
}
rs.close();
stmt.close();
return contatos;
}
public Contato findByName(String nome) throws SQLException {
//String select = "SELECT * FROM CONTATOS WHERE nome = ?";
String select = "select * from contatos Where Nome like ? '%'";
Contato contato = null;
PreparedStatement stmt = getConnection().prepareStatement(select);
stmt.setString(1, nome);
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
contato = new Contato();
contato.setId(rs.getLong("id"));
contato.setNome(rs.getString("nome"));
contato.setApelido(rs.getString("apelido"));
contato.setDtNascimento(rs.getDate("data_nascimento"));
}
rs.close();
stmt.close();
return contato;
}
}
public class Contato {
private Long id;
private String nome;
private String apelido;
private Date dtNascimento;
getter e setter…
}
pacote view
public class ContatoFrame extends JFrame {
private JLabel lbNome, lbApelido, lbDtNascimento;
private JTextField txtNome, txtApelido, txtDtNascimento, txtLocalizar;
private JButton btnSalvar, btnAlterar, btnExcluir, btnClear, btnLocalizar;
private JButton btnPrimeiro, btnProximo, btnAnterior, btnUltimo;
private List contatoList = new ContatoController().listaContatos();
private int registroAtual = 0;
public ContatoFrame() {
super("Contatos");
Container tela = getContentPane();
setLayout(null);
lbNome = new JLabel("Nome");
lbApelido = new JLabel("Apelido");
lbDtNascimento = new JLabel("Data de Nascimento(dd/mm/aaaa)");
lbNome.setBounds(10, 10, 240, 15);
lbApelido.setBounds(10, 50, 240, 15);
lbDtNascimento.setBounds(10, 90, 240, 15);
lbNome.setForeground(Color.BLACK);
lbApelido.setForeground(Color.BLACK);
lbDtNascimento.setForeground(Color.BLACK);
lbNome.setFont(new Font("Courier New", Font.BOLD, 14));
lbApelido.setFont(new Font("Courier New", Font.BOLD, 14));
lbDtNascimento.setFont(new Font("Courier New", Font.BOLD, 14));
tela.add(lbNome);
tela.add(lbApelido);
tela.add(lbDtNascimento);
txtNome = new JTextField();
txtApelido = new JTextField();
txtDtNascimento = new JTextField();
txtNome.setBounds(10, 25, 265, 20);
txtApelido.setBounds(10, 65, 265, 20);
txtDtNascimento.setBounds(10, 105, 265, 20);
tela.add(txtNome);
tela.add(txtApelido);
tela.add(txtDtNascimento);
btnSalvar = new JButton("Salvar");
btnAlterar = new JButton("Alterar");
btnExcluir = new JButton("Excluir");
btnClear = new JButton("Limpar");
btnPrimeiro = new JButton("|<");
btnAnterior = new JButton("<<");
btnProximo = new JButton(">>");
btnUltimo = new JButton(">|");
btnSalvar.setBounds(280, 25, 80, 20);
btnAlterar.setBounds(280, 65, 80, 20);
btnExcluir.setBounds(280, 105, 80, 20);
tela.add(btnSalvar);
tela.add(btnAlterar);
tela.add(btnExcluir);
btnPrimeiro.setBounds(10, 135, 50, 20);
btnAnterior.setBounds(60, 135, 50, 20);
btnClear.setBounds(110, 135, 75, 20);
btnProximo.setBounds(185, 135, 50, 20);
btnUltimo.setBounds(235, 135, 50, 20);
tela.add(btnPrimeiro);
tela.add(btnAnterior);
tela.add(btnClear);
tela.add(btnProximo);
tela.add(btnUltimo);
JLabel lbLocalizar = new JLabel("Localizar por nome");
lbLocalizar.setBounds(10, 160, 220, 20);
txtLocalizar = new JTextField();
txtLocalizar.setBounds(10, 180, 220, 20);
btnLocalizar = new JButton("Ir");
btnLocalizar.setBounds(230, 180, 55, 20);
tela.add(lbLocalizar);
tela.add(txtLocalizar);
tela.add(btnLocalizar);
setSize(400, 250);
setVisible(true);
setLocationRelativeTo(null);
btnSalvar.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
onClickSalvar();
}
}
);
btnAlterar.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
onClickAlterar();
}
}
);
btnExcluir.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
onClickExcluir();
}
}
);
btnClear.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
clearFields();
registroAtual = 0;
}
}
);
btnLocalizar.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
onClickLocalizar();
}
}
);
btnPrimeiro.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
onClickPrimeiro();
}
}
);
btnAnterior.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
onClickAnterior();
}
}
);
btnProximo.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
onClickProximo();
}
}
);
btnUltimo.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
onClickUltimo();
}
}
);
}
private void onClickUltimo() {
registroAtual = contatoList.size() - 1;
getValores(registroAtual);
}
private void onClickProximo() {
if (registroAtual != contatoList.size() - 1) {
getValores(++registroAtual);
}
}
private void onClickAnterior() {
if (registroAtual != 0) {
getValores(--registroAtual);
}
}
private void onClickPrimeiro() {
registroAtual = 0;
getValores(registroAtual);
}
private void getValores(int index) {
if (index <= contatoList.size() - 1) {
Contato contatoAtual = contatoList.get(index);
txtNome.setText(contatoAtual.getNome());
txtApelido.setText(contatoAtual.getApelido());
txtDtNascimento.setText(new SimpleDateFormat("dd/MM/yyyy").format(contatoAtual.getDtNascimento()));
}
}
private void onClickAlterar() {
ContatoController cc = new ContatoController();
long id = contatoList.get(registroAtual).getId();
try {
cc.alterar(id, txtNome.getText(), txtApelido.getText(), txtDtNascimento.getText());
JOptionPane.showMessageDialog(this, "Contato alterado com sucesso!");
clearFields();
contatoList = new ContatoController().listaContatos();
} catch (SQLException e) {
JOptionPane.showMessageDialog(this, "Nao foi possivel alterar contato!\n" + e.getLocalizedMessage());
} catch (ParseException e) {
JOptionPane.showMessageDialog(this, "Data possui formato inválido!\n" + e.getLocalizedMessage());
}
}
private void onClickSalvar() {
ContatoController cc = new ContatoController();
try {
cc.salvar(txtNome.getText(), txtApelido.getText(), txtDtNascimento.getText());
JOptionPane.showMessageDialog(this, "Contato salvo com sucesso!");
clearFields();
contatoList = new ContatoController().listaContatos();
} catch (SQLException e) {
JOptionPane.showMessageDialog(this, "Nao foi possivel salvar contato!\n" + e.getLocalizedMessage());
} catch (ParseException e) {
JOptionPane.showMessageDialog(this, "Data possui formato inválido!\n" + e.getLocalizedMessage());
}
}
private void onClickExcluir() {
ContatoController cc = new ContatoController();
long id = contatoList.get(registroAtual).getId();
try {
cc.excluir(id);
JOptionPane.showMessageDialog(this, "Contato excluido com sucesso!");
clearFields();
contatoList = new ContatoController().listaContatos();
} catch (SQLException e) {
JOptionPane.showMessageDialog(this, "Nao foi possivel excluir o contato!\n" + e.getLocalizedMessage());
}
}
private void onClickLocalizar() {
ContatoController cc = new ContatoController();//vai para o pacote controler
try {
Contato c = cc.buscaContatoPorNome(txtLocalizar.getText());//metodo buscaContatoNome na classe ContatoControler
txtNome.setText(c.getNome());
txtApelido.setText(c.getApelido());
txtDtNascimento.setText(new SimpleDateFormat("dd/MM/yyyy").format(c.getDtNascimento()));
} catch (SQLException e) {
JOptionPane.showMessageDialog(this, "Ocorreu um erro, tente novamente!\n" + e.getLocalizedMessage());
} catch (NullPointerException e){
JOptionPane.showMessageDialog(this, "Contato não localizado ou não existe!\n" + e.getLocalizedMessage());
}
}
private void clearFields() {
txtNome.setText("");
txtApelido.setText("");
txtDtNascimento.setText("");
txtLocalizar.setText("");
}
public static void main(String[] args) {
ContatoFrame frame = new ContatoFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
como codificar minha view em jsp?