在我的 Spring Boot 應用程式中,我有一個 HTML Thymeleaf 表單,我在其中從用戶那里獲取一個字串值。我想將此值從我的控制器類傳遞到我的服務類,最后在回購類中。后者有一個@Query,整個實作的目的是讓用戶能夠根據他們的輸入從我的資料庫中選擇內容。例如,我有不同日期的記錄。我希望用戶選擇他們想要的日期以及從我的資料庫中顯示正確記錄的日期。
我的控制器類有問題,我需要以某種方式連接我的模型并將其作為引數傳遞給我的服務類。然后后者應該與 Repo 類通信以執行查詢@Query并回傳我的結果。到目前為止,我得到的只是空值。
我究竟做錯了什么???
控制器類
package com.andrekreou.covid.gov.controller;
import com.andrekreou.covid.gov.model.Covid;
import com.andrekreou.covid.gov.service.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import javax.servlet.http.HttpServletRequest;
@Controller
public class WelcomePageController {
private final Service service;
@Autowired
public WelcomePageController(Service service) {
this.service = service;
}
@Value("${welcome.message}")
private String message;
@GetMapping("/")
public String main(Model model){
model.addAttribute("message", message);
return "welcome";
}
@GetMapping("/login")
public String getLoginView() {
return "login";
}
@GetMapping("/date/insert")
public String getDateInsertView() {
return "date-insert";
}
@GetMapping("/show-contents")
public String showAllRates(HttpServletRequest request){
request.setAttribute("covidcases", service.showAllCases());
return "databasecontents";
}
@RequestMapping(value = "/dateInsertion", method = RequestMethod.POST)
public String submit(@ModelAttribute("covid") Covid covid,
ModelMap model,
HttpServletRequest request) {
model.addAttribute("referencedate", covid.getReferencedate());
request.setAttribute("covidcases", service.showCases("referencedate"));
return "databasecontents";
}
}
服務等級
package com.andrekreou.covid.gov.service;
import com.andrekreou.covid.gov.model.Covid;
import com.andrekreou.covid.gov.repository.CovidRepo;
import org.springframework.beans.factory.annotation.Autowired;
import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;
@org.springframework.stereotype.Service
@Transactional
public class Service {
private final CovidRepo covidRepo;
@Autowired
public Service(CovidRepo covidRepo) {
this.covidRepo = covidRepo;
}
public List<Covid> showAllCases(){
return new ArrayList<>(covidRepo.findAll());
}
public List<Covid> showCases(String referencedate){
return new ArrayList<>(covidRepo.findByReferencedate(referencedate));
}
}
回購類
package com.andrekreou.covid.gov.repository;
import com.andrekreou.covid.gov.model.Covid;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface CovidRepo
extends JpaRepository<Covid,Integer> {
@Query("select e from #{#entityName} e where e.referencedate = ?1")
List<Covid> findByReferencedate(@Param("referencedate") String referencedate);
}
百里香視圖
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="">
<title>login</title>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-/Y6pD6FV/Vv2HJnA6t vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous">
<link href="https://getbootstrap.com/docs/4.0/examples/signin/signin.css" rel="stylesheet" crossorigin="anonymous">
<style>
body {
background-color: #3e3e3e;
color: white;
}
</style>
</head>
<body>
<div class="container">
<form class="form-signin" method="post" action="/dateInsertion" th:object="${covid}">
<h2 class="form-signin-heading">Please Choose The Date</h2>
<p>
<label for="referencedate" class="sr-only">Username</label>
<input type="text" id="referencedate" name="referencedate" class="form-control" placeholder="referencedate" required=""
autofocus="">
</p>
<button class="btn btn-lg btn-primary btn-block" type="submit">Set Date</button>
</form>
</div>
</body>
</html>
uj5u.com熱心網友回復:
Thymeleaf 是一個渲染引擎,因此當您將某些內容發布到控制器時,它不會執行任何功能。這意味著基本表單發布適用,在您的情況下將是 1 個名為referencedate
假設您的 covid 物體將其作為具有有效 setter 方法的變數,則錯誤將出現在request.setAttribute("covidcases", service.showCases("referencedate"));應位于的這一行中request.setAttribute("covidcases", service.showCases(covid.getReferencedate()));
如果您的 covid 物體不包含此引數,您必須將其添加或單獨添加到您的控制器功能中作為@RequestParam String referencedate
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/521222.html
