Select che ritorna unico record

public Reference getReference(String idRole,String idUser){

String sql="SELECT * FROM refers WHERE id_role=? AND id_user=?";
Reference ref=(Reference)jdbcTemplate.queryForObject(sql, new Object[]{idRole,idUser},new BeanPropertyRowMapper(Reference.class){
public Reference extractData(ResultSet rs) throws SQLException,DataAccessException{
Reference ref=new Reference();

ref.setId(rs.getInt("id"));
ref.setIdRole(rs.getString("id_role"));
ref.setIdUser(rs.getString("id_user"));
ref.setDescription(rs.getString("description"));

return ref;
}
});

 


Redirect URL

Per passare da un form ad un altro si può ricorrere a “redirect:/index” che però non mantiene il contesto

ModelAndView mav=new ModelAndView("redirect:/index"));

 

oppure a RedirectView() che consente di conservare il contesto

String path=retContextPath();
ModelAndView mav=new ModelAndView(new RedirectView(path+"/index"));

 


Questa modalità richiede che venga passato il ContextPath che può essere ricavato nel modo seguente

/**
* Ritorna il contextPath dall'URL.
* Prerequisito: la classe controller deve dichiarare
* @Autowired
* private ApplicationContext appContext;
*
* @author N4N
* @return
*/
private String retContextPath(){
String ctx = appContext.getId();
String path = ctx.substring(ctx.indexOf(":")+1);
path = path.substring(0,path.indexOf("/",1));

return path;
}