前提・実現したいこと
お世話になっております。
データベースからリスト型で結果を取得し、JSP画面に一覧で表示したいと考えており、
DBからSELECTを行い、リスト型での値を取得できているのですが、
JSP,Form,Service,Controllerの連携がうまくいっていないのか、
JSPに値が表示されませんでした。
いくら調べても同事象のものがなく(自分が見落としているだけかもしれませんが)、
自己解決ができないため、お力添えをお願いいたします。
該当のソースコード
JSP
html
1<!DOCTYPE html> 2<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> 3<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 4<%@ taglib prefix="f" uri="http://www.springframework.org/tags/form"%> 5<html> 6<f:form modelAttribute="allSelectForm"> 7 <head> 8 <meta charset="utf-8"> 9 <title>proto-type</title> 10 </head> 11 <body> 12 <h2>proto-type</h2> 13 <h3>${message}</h3> 14 <input type="submit" name="selectAll" value="全件取得" style="margin: 0px 5px 0px 5px; " /> 15 <c:if test="${result} != null"> 16 <table border="1"> 17 <tr> 18 <td>顧客コード</td> 19 <td>顧客名</td> 20 <td>顧客住所</td> 21 <td>サービス名</td> 22 <td>サービス料</td> 23 <td>キャンペーン</td> 24 <td>割引キャンペーン料</td> 25 </tr> 26 <c:forEach var="data" items="${result}"> 27 <tr> 28 <td><c:out value="${data.kokyakuCode}" /></td> 29 <td><c:out value="${data.kokyakuName}" /></td> 30 <td><c:out value="${data.kokyakuAddress}" /></td> 31 <td><c:out value="${data.keiyakuName}" /></td> 32 <td><c:out value="${data.keiyakuPrice}" /></td> 33 <td><c:out value="${data.campaignName}" /></td> 34 <td><c:out value="${data.campaignPrice}" /></td> 35 </tr> 36 </c:forEach> 37 </table> 38 </c:if> 39 </body> 40</f:form> 41</html>
Form
java
1package jp.sample.billing.form; 2import java.util.List; 3import jp.deloitte.maplus.billing.entity.Kokyakutbl; 4import lombok.Data; 5@Data 6public class AllSelectForm { 7 private List<Kokyakutbl> result; 8 public List<Kokyakutbl> getResult() { 9 return result; 10 } 11 public void setResult(List<Kokyakutbl> result) { 12 this.result = result; 13 } 14}
Controller
java
1package jp.deloitte.maplus.billing.controller; 2import java.util.List; 3import org.springframework.beans.factory.annotation.Autowired; 4import org.springframework.context.annotation.Import; 5import org.springframework.stereotype.Controller; 6import org.springframework.ui.Model; 7import org.springframework.web.bind.annotation.GetMapping; 8import org.springframework.web.bind.annotation.ModelAttribute; 9import org.springframework.web.bind.annotation.PostMapping; 10import org.springframework.web.bind.annotation.RequestMapping; 11import jp.deloitte.maplus.billing.entity.Kokyakutbl; 12import jp.deloitte.maplus.billing.form.AllSelectForm; 13import jp.deloitte.maplus.billing.service.AllSelectService; 14@Controller 15@Import(AllSelectService.class) 16@RequestMapping("/prototype") 17public class AllSelectController { 18 @Autowired 19 private AllSelectService ASS; 20 // GETアクセス 21 @GetMapping 22 public String getAccess(@ModelAttribute("allSelectForm") AllSelectForm allSelectForm, Model model){ 23 24 List<Kokyakutbl> resultList = ASS.getAll(); 25 model.addAttribute("result", resultList); 26 model.addAttribute("message", "顧客情報一覧表示"); 27 28 return "allSelect"; 29 } 30 // POSTアクセス 31 @PostMapping 32 public String postAccess(@ModelAttribute("allSelectForm") AllSelectForm allSelectForm, Model model){ 33 List<Kokyakutbl> resultList = ASS.getAll(); 34 if(resultList.size()>0){ 35 model.addAttribute("message", resultList.size()+"件取得できました。"); 36 } else { 37 model.addAttribute("message", "取得できませんでした。"); 38 } 39 model.addAttribute("result", resultList); 40 return "allSelect"; 41 } 42}
Service
java
1package jp.deloitte.maplus.billing.service; 2import java.util.List; 3import org.springframework.beans.factory.annotation.Autowired; 4import org.springframework.stereotype.Service; 5import org.springframework.transaction.annotation.Transactional; 6import jp.deloitte.maplus.billing.entity.Kokyakutbl; 7import jp.deloitte.maplus.billing.mapper.KokyakutblMapper; 8@Service 9public class AllSelectService { 10 @Autowired 11 private KokyakutblMapper KMap; 12 // GETアクセス 13 @Transactional 14 public List<Kokyakutbl> getAll(){ 15 16 List<Kokyakutbl> result = KMap.getKokyakuAll(); 17 18 return result; 19 } 20}
よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
また依頼した内容が修正された場合は、修正依頼を取り消すようにしましょう。