Ahoj, snazim se podle navodu Stripes a Spring ve webových aplikacích udelat jednoduchy adresar (ucim se, vim ze je adresar k nicemu).
Mam zatim nasledujici db v MySQL:
Pro tabulku stat mam upraveny kod podle vyse uvedeneho tutorialu (funguje):
package com.example.test.model;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import javax.sql.DataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.simple.ParameterizedSingleColumnRowMapper;
import org.springframework.stereotype.Repository;
@Repository("statManager")
public class StatManagerImpl implements StatManager {
final static Logger log = LoggerFactory.getLogger(StatManagerImpl.class);
public StatManagerImpl() {
log.debug("vytvoren");
}
private JdbcTemplate jdbc;
@Autowired
public void setDataSource(DataSource dataSource) {
log.debug("dataSource set to {}", dataSource);
jdbc = new JdbcTemplate(dataSource);
}
private static final RowMapper<Stat> STAT_MAPPER = new RowMapper<Stat>() {
@Override
public Stat mapRow(ResultSet rs, int i) throws SQLException {
Stat stat = new Stat();
stat.setId(rs.getInt("id"));
stat.setNazev(rs.getString("nazev"));
return stat;
}
};
private static final ParameterizedSingleColumnRowMapper<String> STRING_MAPPER = ParameterizedSingleColumnRowMapper.newInstance(String.class);
@Override
public void vytvoritStat(Stat stat) {
jdbc.update("INSERT INTO stat (nazev) VALUES (?)", stat.getNazev() );
}
@Override
public Stat getStatById(int id) {
return jdbc.queryForObject("SELECT * FROM stat WHERE id = ?", STAT_MAPPER, id);
}
@Override
public Stat getStatByNazev(String nazev) {
return jdbc.queryForObject("SELECT * FROM stat WHERE nazev = ?", STAT_MAPPER, nazev);
}
@Override
public List<Stat> getVsechnyStaty() {
return jdbc.query("SELECT id, nazev FROM stat ORDER BY nazev", STAT_MAPPER);
}
@Override
public void aktualizaceStatu(Stat stat) {
jdbc.update("UPDATE stat SET nazev=? WHERE id=?", stat.getNazev(), stat.getId() );
}
@Override
public void smazaniStatu(Stat stat) {
jdbc.update("DELETE FROM stat WHERE id = ?", stat.getId() );
}
}
/* -------------------------------------------------------------- */
package com.example.test.web;
import com.example.test.model.Stat;
import com.example.test.model.StatManager;
import java.util.List;
import net.sourceforge.stripes.action.ActionBean;
import net.sourceforge.stripes.action.ActionBeanContext;
import net.sourceforge.stripes.action.Before;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.UrlBinding;
import net.sourceforge.stripes.controller.LifecycleStage;
import net.sourceforge.stripes.integration.spring.SpringBean;
import net.sourceforge.stripes.validation.Validate;
import net.sourceforge.stripes.validation.ValidateNestedProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@UrlBinding("/staty/{$event}/{stat.id}")
public class StatActionBean implements ActionBean {
final static Logger log = LoggerFactory.getLogger(StatActionBean.class);
private ActionBeanContext context;
@SpringBean
protected StatManager statManager;
@DefaultHandler
public Resolution all() {
log.debug("all()");
return new ForwardResolution("/statyShow.jsp");
}
public List<Stat> getStaty() {
return statManager.getVsechnyStaty();
}
@Override
public void setContext(ActionBeanContext context) {
this.context = context;
}
@Override
public ActionBeanContext getContext() {
return context;
}
@ValidateNestedProperties(value = {
@Validate(on = {"add", "save"}, field = "nazev", required = true, minlength = 3)
})
private Stat stat;
public Stat getStat() {
return stat;
}
public void setStat(Stat stat) {
this.stat = stat;
}
public Resolution add() {
log.debug("add() stat={}", stat);
statManager.vytvoritStat(stat);
return new RedirectResolution(this.getClass(), "all");
}
public Resolution delete() {
log.debug("delete({})", stat.getId());
statManager.smazaniStatu(stat);
return new RedirectResolution(this.getClass(), "all");
}
@Before(stages = LifecycleStage.BindingAndValidation, on = {"edit", "save"})
public void nacitStatZDatabaze() {
String ids = context.getRequest().getParameter("stat.id");
if (ids == null) return;
stat = statManager.getStatById(Integer.parseInt(ids));
}
public Resolution edit() {
log.debug("edit() stat={}", stat);
return new ForwardResolution("/statyEdit.jsp");
}
public Resolution save() {
log.debug("save() stat={}", stat);
statManager.aktualizaceStatu(stat);
return new RedirectResolution(this.getClass(), "all");
}
public Resolution storno() {
log.debug("edit() stat={}", stat);
return new RedirectResolution(this.getClass(), "all");
}
}
Problem mam u tabulky adresa. SQL dotazy spustene v phpMyAdminu funguji dobre. Jak mam pracovat s cizim klicem v jave?:
package com.example.test.model;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import javax.sql.DataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.simple.ParameterizedSingleColumnRowMapper;
import org.springframework.stereotype.Repository;
@Repository("adresaManager")
public class AdresaManagerImpl implements AdresaManager {
final static Logger log = LoggerFactory.getLogger(AdresaManagerImpl.class);
public AdresaManagerImpl() {
log.debug("vytvoren");
}
private JdbcTemplate jdbc;
@Autowired
public void setDataSource(DataSource dataSource) {
log.debug("dataSource set to {}", dataSource);
jdbc = new JdbcTemplate(dataSource);
}
private static final RowMapper<Adresa> ADRESA_MAPPER = new RowMapper<Adresa>() {
@Override
public Adresa mapRow(ResultSet rs, int i) throws SQLException {
Adresa adresa = new Adresa();
adresa.setId(rs.getInt("id"));
adresa.setUlice(rs.getString("ulice"));
adresa.setCp(rs.getString("cp"));
adresa.setMesto(rs.getString("mesto"));
adresa.setPsc(rs.getString("psc"));
adresa.setStat("X");
// adresa.setStat(Integer.toString(rs.getInt("stat")));
return adresa;
}
};
//mapuje data z ResultSet na tridu Stat
/*
private static final RowMapper<Stat> STAT_MAPPER = new RowMapper<Stat>() {
@Override
public Stat mapRow(ResultSet rs, int i) throws SQLException {
Stat stat = new Stat();
stat.setId(rs.getInt("id"));
stat.setNazev(rs.getString("nazev"));
return stat;
}
};
*/
/*
@SpringBean
protected StatManager statManager;
*/
//mapuje jediny sloupec z ResultSet na String
private static final ParameterizedSingleColumnRowMapper<String> STRING_MAPPER = ParameterizedSingleColumnRowMapper.newInstance(String.class);
@Override
public void vytvoritAdresu(Adresa adresa) {
//int statId = jdbc.queryForObject("SELECT id, nazev FROM stat WHERE nazev= ? LIMIT 1", STAT_MAPPER, 1).getId();
// int statId = statManager.getStatByNazev(adresa.getStat()).getId();
int statId = 1;
jdbc.update("INSERT INTO adresa (ulice, cp, mesto, psc, stat_id) VALUES (?,?,?,?,?)",
adresa.getUlice(), adresa.getCp(), adresa.getMesto(),
adresa.getPsc(), statId );
}
@Override
public Adresa getAdresaById(int id) {
return jdbc.queryForObject("SELECT * FROM adresa WHERE id = ?", ADRESA_MAPPER, id);
}
@Override
public List<Adresa> getVsechnyAdresy() {
return jdbc.query("SELECT adresa.id, adresa.ulice, adresa.cp, adresa.mesto, adresa.psc, stat.nazev AS stat FROM adresa INNER JOIN stat ON adresa.stat_id = stat.id ORDER BY adresa.mesto ASC",
ADRESA_MAPPER);
}
@Override
public void aktualizaceAdresy(Adresa adresa) {
//int statId = jdbc.queryForObject("SELECT id, nazev FROM stat WHERE nazev= ? LIMIT 1", STAT_MAPPER, 1).getId();
// int statId = statManager.getStatByNazev(adresa.getStat()).getId();
int statId = 1;
jdbc.update("UPDATE adresa SET ulice=?, cp=?, mesto=?, psc=?, stat_id=? WHERE id=?",
adresa.getUlice(), adresa.getCp(), adresa.getMesto(), adresa.getPsc(), statId );
}
@Override
public void smazaniAdresy(Adresa adresa) {
jdbc.update("DELETE FROM adresa WHERE id = ?", adresa.getId() );
}
}
/* --------------------------------------------------------- */
package com.example.test.web;
import com.example.test.model.Adresa;
import com.example.test.model.AdresaManager;
import java.util.List;
import net.sourceforge.stripes.action.ActionBean;
import net.sourceforge.stripes.action.ActionBeanContext;
import net.sourceforge.stripes.action.Before;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.UrlBinding;
import net.sourceforge.stripes.controller.LifecycleStage;
import net.sourceforge.stripes.integration.spring.SpringBean;
import net.sourceforge.stripes.validation.Validate;
import net.sourceforge.stripes.validation.ValidateNestedProperties;
import org.omg.PortableInterceptor.SYSTEM_EXCEPTION;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@UrlBinding("/adresy/{$event}/{adresa.id}")
public class AdresaActionBean implements ActionBean {
final static Logger log = LoggerFactory.getLogger(AdresaActionBean.class);
private ActionBeanContext context;
@SpringBean
protected AdresaManager adresaManager;
@DefaultHandler
public Resolution all() {
log.debug("all()");
log.info("ALL ADRESY");
return new ForwardResolution("/adresyShow.jsp");
}
public List<Adresa> getAdresy() {
return adresaManager.getVsechnyAdresy();
}
@Override
public void setContext(ActionBeanContext context) {
this.context = context;
}
@Override
public ActionBeanContext getContext() {
return context;
}
@ValidateNestedProperties(value = {
@Validate(on = {"add", "save"}, field = "ulice", required = true)
})
private Adresa adresa;
public Adresa getAdresa() {
return adresa;
}
public void setAdresa(Adresa adresa) {
this.adresa = adresa;
}
public Resolution add() {
log.debug("add() adresa={}", adresa);
adresaManager.vytvoritAdresu(adresa);
return new RedirectResolution(this.getClass(), "all");
}
public Resolution delete() {
log.debug("delete({})", adresa.getId());
adresaManager.smazaniAdresy(adresa);
return new RedirectResolution(this.getClass(), "all");
}
@Before(stages = LifecycleStage.BindingAndValidation, on = {"edit", "save"})
public void nacitAdresuZDatabaze() {
String ids = context.getRequest().getParameter("adresa.id");
if (ids == null) return;
adresa = adresaManager.getAdresaById(Integer.parseInt(ids));
}
public Resolution edit() {
log.debug("edit() adresa={}", adresa);
return new ForwardResolution("/adresyEdit.jsp");
}
public Resolution save() {
log.debug("save() adresa={}", adresa);
adresaManager.aktualizaceAdresy(adresa);
return new RedirectResolution(this.getClass(), "all");
}
public Resolution storno() {
log.debug("edit() adresa={}", adresa);
return new RedirectResolution(this.getClass(), "all");
}
}
Zde je cely kod: http://db.tt/L5y8OYY5
Muzete mi prosim nekdo poradit? Jinak obecne to delam dobre, nebo je to blbost a mam na to jit uplne jinak?
Dekuji za vase rady!