Operation Spring Awakening Spring MVC Part 1

Play with Spring MVC (5)

I can't be a Struts 1 system forever: sweat_smile:

Do you want to make something first?

I made a transcendentally simple Todo app: joy:

  1. Initial screen 初期画面.png
  2. Before registration 登録前.png
  3. After registration 登録後.png
  4. After completion 完了.png
  5. Before searching 検索前.png
  6. After searching 検索後.png
  7. List scenery 全景.png

When you press the "Finish" button, a strikethrough will appear. If you press the "Resurrection" button, the cancellation destination will disappear.

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

Operation Spring Awakening Spring MVC Part 1
Operation Spring Awakening Spring MVC Part 4
Operation Spring Awakening Spring MVC Part 3
Operation Spring Awakening Spring MVC Part 2
Introduction to Spring Boot Part 1
Implement file download with Spring MVC
Spring Boot starting from zero Part 2
Spring Boot starting from zero Part 1