JSPの入力画面でsample_idを入力し、そのsample_idとマッチするsample_nameを取得したいのですが、nullが戻ってきてしまいます。
色々と調べましたが、自力での解決は難しいと思いましたので、こちらに質問させて頂きます。
コンソール
14:22:44.467 [http-nio-8080-exec-3] DEBUG org.springframework.web.servlet.DispatcherServlet - GET "/web_sample/search", parameters={} 14:22:44.467 [http-nio-8080-exec-3] DEBUG org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped to group.study.dev.sample.search.controller.UserController#goSearch() 14:22:44.470 [http-nio-8080-exec-3] DEBUG org.springframework.web.servlet.view.JstlView - View name 'search/search', model {} 14:22:44.470 [http-nio-8080-exec-3] DEBUG org.springframework.web.servlet.view.JstlView - Forwarding to [/WEB-INF/views/search/search.jsp] 14:22:46.402 [http-nio-8080-exec-3] DEBUG org.springframework.web.servlet.DispatcherServlet - Completed 200 OK 14:22:48.423 [http-nio-8080-exec-4] DEBUG org.springframework.web.servlet.DispatcherServlet - POST "/web_sample/search/result", parameters={masked} 14:22:48.425 [http-nio-8080-exec-4] DEBUG org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped to group.study.dev.sample.search.controller.UserController#postSearch(String, Model) 14:22:48.502 [http-nio-8080-exec-4] DEBUG org.mybatis.spring.SqlSessionUtils - Creating a new SqlSession 14:22:48.515 [http-nio-8080-exec-4] DEBUG org.mybatis.spring.SqlSessionUtils - SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@34a499fc] was not registered for synchronization because synchronization is not active 14:22:48.531 [http-nio-8080-exec-4] DEBUG org.springframework.jdbc.datasource.DataSourceUtils - Fetching JDBC Connection from DataSource 14:22:48.531 [http-nio-8080-exec-4] DEBUG org.springframework.jdbc.datasource.DriverManagerDataSource - Creating new JDBC DriverManager Connection to [jdbc:postgresql://localhost:5432/mydb] 14:22:48.690 [http-nio-8080-exec-4] DEBUG org.mybatis.spring.transaction.SpringManagedTransaction - JDBC Connection [org.postgresql.jdbc42.Jdbc42Connection@2e3fedd6] will not be managed by Spring 14:22:48.709 [http-nio-8080-exec-4] DEBUG group.study.dev.dao.sample.SampleDao.selectOne - ==> Preparing: select * from public.sampletable where sample_id = ? 14:22:48.801 [http-nio-8080-exec-4] DEBUG group.study.dev.dao.sample.SampleDao.selectOne - ==> Parameters: 1(String) 14:22:48.939 [http-nio-8080-exec-4] DEBUG group.study.dev.dao.sample.SampleDao.selectOne - <== Total: 1 14:22:48.955 [http-nio-8080-exec-4] DEBUG org.mybatis.spring.SqlSessionUtils - Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@34a499fc] 14:22:48.956 [http-nio-8080-exec-4] DEBUG org.springframework.jdbc.datasource.DataSourceUtils - Returning JDBC Connection to DataSource 14:22:48.965 [http-nio-8080-exec-4] DEBUG org.springframework.web.servlet.view.JstlView - View name 'search/search', model {list=null} 14:22:48.966 [http-nio-8080-exec-4] DEBUG org.springframework.web.servlet.view.JstlView - Forwarding to [/WEB-INF/views/search/search.jsp] 14:22:48.971 [http-nio-8080-exec-4] DEBUG org.springframework.web.servlet.DispatcherServlet - Completed 200 OK
入力画面で1を受け取り、SQL文で一件取得できていると思うのですが、listの中身を確認するとnullが入ってしまいます。
UserController
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.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 @RequestMapping(value = "/search") public class UserController extends AbstractController { @Autowired private UserService userService; @GetMapping public String goSearch() { return "search/search"; } @PostMapping("/result") public String postSearch(@RequestParam("sampleId")String sampleId, Model model) { SampleDto list = userService.selectOne(sampleId); model.addAttribute("list", list); return "search/search"; } }
UserService
package group.study.dev.sample.search.service; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import group.study.dev.dao.sample.SampleDao; import group.study.dev.dto.sample.SampleDto; import group.study.dev.sample.search.model.UserModel; import group.study.dev.web.common.controller.AbstractController; @Service public class UserService extends AbstractController{ @Autowired private SampleDao sampleMapper; public SampleDto selectOne(String sampleId){ UserModel userModel = new UserModel(); userModel.setSampleDto(sampleMapper.selectOne(sampleId)); return userModel.getSampleDto(); } }
UserModel
package group.study.dev.sample.search.model; import java.util.List; import group.study.dev.dto.sample.SampleDto; import group.study.dev.web.common.controller.AbstractController; import lombok.Data; @Data public class UserModel extends AbstractController{ private SampleDto sampleDto; private List<SampleDto> sampleList; }
SampleDto
package group.study.dev.dto.sample; import lombok.Data; @Data public class SampleDto { private String sampleId; private String sampleName; }
SampleDao
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); }
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"> <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 * from public.sampletable where sample_id = #{sampleId} </select> </mapper>
search.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <% String method = request.getMethod(); %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>hoge</title> </head> <body> <% if ("GET".equals(method)) { %> <h1>検索画面</h1> <form method="post" action="/web_sample/search/result"> <input type="text" name="sampleId" /> <button type="submit" value="送信"></button> </form> <% } else { %> <h1>検索結果</h1> <table border="1" bordercolor="#333333"> <tr> <th>ID</th> <th>NAME</th> </tr> <c:forEach items="${list}" var="item"> <tr> <td><c:out value="${item.sampleId}" /></td> <td><c:out value="${item.sampleName}" /></td> </c:forEach> </table> <% } %> </body> </html>
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/09/02 06:55
2020/09/02 08:27
2020/09/03 04:32
2020/09/03 07:54