やりたい事
- Spring+MyBatis+PostgreSQLにおけるCRUDのupdateの実装(ID検索で取得した名前の更新)
分からない事
- 更新ボタンを押すと、以下のエラーが起きる。
20:38:25.720 [http-nio-8080-exec-7] DEBUG org.springframework.web.servlet.DispatcherServlet - POST "/web_sample/search/userDetail/search/userDetail/2", parameters={masked}
20:38:25.752 [http-nio-8080-exec-7] DEBUG org.springframework.web.servlet.handler.SimpleUrlHandlerMapping - Mapped to ResourceHttpRequestHandler ["/WEB-INF/public/"]
20:38:25.773 [http-nio-8080-exec-7] DEBUG org.springframework.web.servlet.resource.ResourceHttpRequestHandler - Resource not found
20:38:25.774 [http-nio-8080-exec-7] DEBUG org.springframework.web.servlet.DispatcherServlet - Completed 404 NOT_FOUND
懸念点
jspファイルのaction属性の指定がおかしいかもしれません。
UserControllerの@PostMapping("/search/userDetail/{id}")も間違っているかもしれません。
UserController.java
package group.study.dev.sample.search.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import group.study.dev.dto.sample.SampleDto; import group.study.dev.sample.search.service.UserService; import group.study.dev.web.common.controller.AbstractController; @Controller public class UserController extends AbstractController { @Autowired private UserService userService; @RequestMapping(value = "/search") public String goSearch() { return "search/search"; } @GetMapping("/search/insert") public String goInsert() { return "/search/insert"; } @PostMapping("/search/insert") public void postInsert(SampleDto sampleDto) { userService.insert(sampleDto); } @PostMapping("/search/result") public String postSearch(@RequestParam("sampleId")String sampleId, Model model) { SampleDto list = userService.selectOne(sampleId); model.addAttribute("list", list); return "search/search"; } @GetMapping("/search/userDetail/{id}") public String goUserDetail(@PathVariable("id")String sampleId, Model model) { SampleDto list2 = userService.selectOne(sampleId); model.addAttribute("list2", list2); return "search/userDetail"; } @PostMapping("/search/userDetail/{id}") public void updateOne(SampleDto sampleDto, @RequestParam("${list2.sampleId}")String sampleId, @RequestParam("sampleName")String sampleName, Model model) { userService.updateOne(sampleDto); } }
userDetail.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>詳細画面</title> </head> <body> <table border="1"> <tr> <th>ID</th> <th>NAME</th> </tr> <tr> <td><c:out value="${list2.sampleId}" /></td> <td> <form method="post" action="search/userDetail/${list2.sampleId}"> <input type="text" name="sampleName" value="${list2.sampleName}"> <input type="submit" name="update" value="更新" /> </form> </td> <%-- <td><c:out value="${list2.sampleName}" /></td> --%> </tr> </table> <form method="post" action="/search/{id}/"> <button type="submit">削除ページ</button> </form> </body> </html>
SampleDao.java
package group.study.dev.dao.sample; import java.util.List; import org.apache.ibatis.annotations.Mapper; import group.study.dev.dto.sample.SampleDto; @Mapper public interface SampleDao { public List<SampleDto> selectAll(); public SampleDto selectOne(String sampleId); public void insert(SampleDto sampleDto); public void updateOne(SampleDto sampleDto); public boolean deleteOne(String userId); }
SampleMapper.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="group.study.dev.dao.sample.SampleDao"> <resultMap type="group.study.dev.dto.sample.SampleDto" id="user"> <id column="sample_id" property="sampleId" /> <id column="sample_name" property="sampleName" /> </resultMap> <select id="selectAll" resultType="group.study.dev.dto.sample.SampleDto"> select sample_name, sample_id from public.sample_table where sample_id = 'S003'; </select> <select id="selectOne" parameterType="string" resultType="group.study.dev.dto.sample.SampleDto"> select sample_id as sampleId, sample_name as sampleName from sampletable where sample_id = #{sampleId} </select> <insert id="insert" parameterType="group.study.dev.dto.sample.SampleDto"> insert into public.sampletable(sample_id,sample_name) values (#{sampleId}, #{sampleName}) </insert> <update id="updateOne" parameterType="group.study.dev.dto.sample.SampleDto"> UPDATE sampletable SET sample_name = #{sampleName} WHERE sample_id = #{sampleId} </update> <delete id="deleteOne" parameterType="string"> DELETE FROM sampletable WHERE sample_id = #{sampleId} </delete> </mapper>
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/09/08 01:17
2020/09/08 04:02