Needless to say, the controller looks like this. "Registration", "Search", "Done", and "Resurrection" are all redirected. And every time, all the data is taken out from the table, made into a list and passed to the screen.
@RequestMapping(value = "/newItem", params="newItem",method = RequestMethod.POST)
@Transactional("transactionManagerName")
public String newItem(@Validated TodoForm form, BindingResult result, Model model)
{
DefaultTransactionDefinition dtDef = new DefaultTransactionDefinition();
TransactionStatus tSts = txMgr.getTransaction(dtDef);
try
{
jdbcTemplate.update("INSERT INTO todo (content,done) VALUES (?, ?)", form.getContent(),false);
txMgr.commit(tSts);
}
catch(Exception ex)
{
txMgr.rollback(tSts);
logger.debug("update failed",ex.toString());
}
return "redirect:/";
}
@RequestMapping(value = "/newItem", params="searchItem",method = RequestMethod.POST)
@Transactional("transactionManagerName")
public String searchItem(@Validated TodoForm form, BindingResult result, Model model)
{
String likeSQL = "select * from todo where content like '%" + form.getContent() + "%'";
//Get all data from table todo
List<Map<String, Object>> ret = jdbcTemplate.queryForList(likeSQL);
//Generate a list of data to pass to the screen
List<TodoItem> mList = new ArrayList<TodoItem>();
for(int i=0;i<ret.size();i++)
{
TodoItem tmp = new TodoItem();
tmp.setId(ret.get(i).get("id").toString());
tmp.setContent(ret.get(i).get("content").toString());
if(ret.get(i).get("done").toString().equals("false"))
{
tmp.setDone(false);
}
else
{
tmp.setDone(true);
}
mList.add(tmp);
}
//Set the list to be passed to the screen to Model
model.addAttribute("mList", mList );
return "todo/todo";
}
@RequestMapping(value = "/", method = RequestMethod.GET)
public String index(Locale locale, Model model)
{
//Get all data from table todo
List<Map<String, Object>> ret = jdbcTemplate.queryForList("select * from todo");
//Generate a list of data to pass to the screen
List<TodoItem> mList = new ArrayList<TodoItem>();
for(int i=0;i<ret.size();i++)
{
TodoItem tmp = new TodoItem();
tmp.setId(ret.get(i).get("id").toString());
tmp.setContent(ret.get(i).get("content").toString());
if(ret.get(i).get("done").toString().equals("false"))
{
tmp.setDone(false);
}
else
{
tmp.setDone(true);
}
mList.add(tmp);
}
//Set the list to be passed to the screen to Model
model.addAttribute("mList", mList );
return "todo/todo";
}
@RequestMapping(value = "/restore", method = RequestMethod.POST)
@Transactional("transactionManagerName")
public String restore(@Validated TodoForm form, BindingResult result, Model model)
{
DefaultTransactionDefinition dtDef = new DefaultTransactionDefinition();
TransactionStatus tSts = txMgr.getTransaction(dtDef);
List<Map<String, Object>> ret = jdbcTemplate.queryForList("select * from todo WHERE id=?",new Object[]{form.getId()});
if(ret.size()>0)
{
TodoItem upItem = new TodoItem();
upItem.setId(form.getId());
upItem.setContent(form.getContent());
upItem.setDone(form.getDone());
SqlParameterSource param = new BeanPropertySqlParameterSource(upItem);
try
{
jdbcTemplate.update("UPDATE todo SET done = ? WHERE id = ?",false,form.getId());
txMgr.commit(tSts);
}
catch(Exception ex)
{
txMgr.rollback(tSts);
logger.debug("update failed",ex.toString());
}
}
else
{
logger.debug("Not subject to update");
}
return "redirect:/";
}
@RequestMapping(value = "/done", method = RequestMethod.POST)
@Transactional("transactionManagerName")
public String done(@Validated TodoForm form, BindingResult result, Model model)
{
DefaultTransactionDefinition dtDef = new DefaultTransactionDefinition();
TransactionStatus tSts = txMgr.getTransaction(dtDef);
List<Map<String, Object>> ret = jdbcTemplate.queryForList("select * from todo WHERE id=?",new Object[]{form.getId()});
if(ret.size()>0)
{
TodoItem upItem = new TodoItem();
upItem.setId(form.getId());
upItem.setContent(form.getContent());
upItem.setDone(form.getDone());
SqlParameterSource param = new BeanPropertySqlParameterSource(upItem);
try
{
jdbcTemplate.update("UPDATE todo SET done = ? WHERE id = ?",true,form.getId());
txMgr.commit(tSts);
}
catch(Exception ex)
{
txMgr.rollback(tSts);
logger.debug("update failed",ex.toString());
}
}
else
{
logger.debug("Not subject to update");
}
return "redirect:/";
}
I'm using MySQL. The table looks like this.
CREATE TABLE `todo` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`content` varchar(50) DEFAULT NULL,
`done` tinyint(1) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8
Click here for a thin sauce: hugging: https://github.com/pugachev/todomvc.git
The weather was bad this Saturday and Sunday: umbrella2: It was cold: snowflake: So I made it by hand. I also referred to other people. Thank you very much.
Recommended Posts