Spring Boot 模板解析Sping Boot Thymeleaf期间发生错误?

ddrv8njm  于 2023-01-30  发布在  Spring
关注(0)|答案(1)|浏览(168)

控制器类别:

@Autowired
 private PatientServiceImp PatienceService;
    private PatientDTO patientDTO;
    
    @GetMapping("/register")
    public String registerGET(Model model) {
        
        Patient p = new Patient();
        PatientDTO patientDTO = PatienceService.convertPatientDTO(p);
        
        model.addAttribute("patient", patientDTO);
        return "register";
    }

    @PostMapping("/register")
    public String registerGET(Patient patient) {
        
        PatienceService.savePatient(patient);
        
        return "redirect:/login";
    }

服务等级

@Service
@NoArgsConstructor
public class PatientServiceImp implements PatientService {

    @Autowired
    private PatientRepository PatientRepository;
    private PatientDTO PatientDTO;

    @Override
    public Patient findPatientId(String x) {
        Optional<Patient> p = PatientRepository.findById(x);
        Patient patient = p.get();
        return patient;
    }

    @Override
    public PatientDTO savePatient(Patient x) {
        Patient patient = new Patient(x.getName(),
                x.getEmail(),
                x.getPassword(),
                x.getId());
        x.setId(UUID.randomUUID().toString());

        return convertPatientDTO(PatientRepository.save(patient));
    }

    @Override
    public void deletePatient(Patient x) {
        PatientRepository.delete(x);
    }

    public PatientDTO convertPatientDTO(Patient p) {
        return new PatientDTO(p.getName(), p.getEmail(), p.getPassword());
    }

}

错误消息:

org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/register.html]")
    at org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parse(AbstractMarkupTemplateParser.java:241) ~[thymeleaf-3.1.1.RELEASE.jar:3.1.1.RELEASE]
    at org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parseStandalone(AbstractMarkupTemplateParser.java:100) ~[thymeleaf-3.1.1.RELEASE.jar:3.1.1.RELEASE]
    at org.thymeleaf.engine.TemplateManager.parseAndProcess(TemplateManager.java:649) ~[thymeleaf-3.1.1.RELEASE.jar:3.1.1.RELEASE]
    at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1103) ~[thymeleaf-3.1.1.RELEASE.jar:3.1.1.RELEASE]
    at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1077) ~[thymeleaf-3.1.1.RELEASE.jar:3.1.1.RELEASE]
    at org.thymeleaf.spring6.view.ThymeleafView.renderFragment(ThymeleafView.java:372) ~[thymeleaf-spring6-3.1.1.RELEASE.jar:3.1.1.RELEASE]
    at org.thymeleaf.spring6.view.ThymeleafView.render(ThymeleafView.java:192) ~[thymeleaf-spring6-3.1.1.RELEASE.jar:3.1.1.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1414) ~[spring-webmvc-6.0.4.jar:6.0.4]
    at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1158) ~[spring-webmvc-6.0.4.jar:6.0.4]
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1097) ~[spring-webmvc-6.0.4.jar:6.0.4]
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:973) ~[spring-webmvc-6.0.4.jar:6.0.4]
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1011) ~[spring-webmvc-6.0.4.jar:6.0.4]
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:903) ~[spring-webmvc-6.0.4.jar:6.0.4]
    at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:705) ~[tomcat-embed-core-10.1.5.jar:6.0]
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:885) ~[spring-webmvc-6.0.4.jar:6.0.4]
    at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:814) ~[tomcat-embed-core-10.1.5.jar:6.0]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:223) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:158) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-embed-websocket-10.1.5.jar:10.1.5]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:185) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:158) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) ~[spring-web-6.0.4.jar:6.0.4]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116) ~[spring-web-6.0.4.jar:6.0.4]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:185) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:158) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:177) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:542) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:119) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:357) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:400) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:859) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1734) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:52) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
    at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
    at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
    at java.base/java.lang.Thread.run(Thread.java:833) ~[na:na]
Caused by: org.attoparser.ParseException: Attribute name cannot be null or empty
    at org.attoparser.MarkupParser.parseDocument(MarkupParser.java:393) ~[attoparser-2.0.6.RELEASE.jar:2.0.6.RELEASE]
    at org.attoparser.MarkupParser.parse(MarkupParser.java:257) ~[attoparser-2.0.6.RELEASE.jar:2.0.6.RELEASE]
    at org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parse(AbstractMarkupTemplateParser.java:230) ~[thymeleaf-3.1.1.RELEASE.jar:3.1.1.RELEASE]
    ... 40 common frames omitted
Caused by: java.lang.IllegalArgumentException: Attribute name cannot be null or empty
    at org.thymeleaf.engine.AttributeName.<init>(AttributeName.java:51) ~[thymeleaf-3.1.1.RELEASE.jar:3.1.1.RELEASE]
    at org.thymeleaf.engine.HTMLAttributeName.<init>(HTMLAttributeName.java:76) ~[thymeleaf-3.1.1.RELEASE.jar:3.1.1.RELEASE]
    at org.thymeleaf.engine.HTMLAttributeName.forName(HTMLAttributeName.java:63) ~[thymeleaf-3.1.1.RELEASE.jar:3.1.1.RELEASE]
    at org.thymeleaf.engine.AttributeNames.buildHTMLAttributeName(AttributeNames.java:292) ~[thymeleaf-3.1.1.RELEASE.jar:3.1.1.RELEASE]
    at org.thymeleaf.engine.AttributeNames.access$300(AttributeNames.java:37) ~[thymeleaf-3.1.1.RELEASE.jar:3.1.1.RELEASE]
    at org.thymeleaf.engine.AttributeNames$AttributeNamesRepository.storeAttribute(AttributeNames.java:693) ~[thymeleaf-3.1.1.RELEASE.jar:3.1.1.RELEASE]
    at org.thymeleaf.engine.AttributeNames$AttributeNamesRepository.getAttribute(AttributeNames.java:607) ~[thymeleaf-3.1.1.RELEASE.jar:3.1.1.RELEASE]
    at org.thymeleaf.engine.AttributeNames.forHTMLName(AttributeNames.java:480) ~[thymeleaf-3.1.1.RELEASE.jar:3.1.1.RELEASE]
    at org.thymeleaf.engine.AttributeDefinitions$AttributeDefinitionRepository.storeAttribute(AttributeDefinitions.java:788) ~[thymeleaf-3.1.1.RELEASE.jar:3.1.1.RELEASE]
    at org.thymeleaf.engine.AttributeDefinitions$AttributeDefinitionRepository.getAttribute(AttributeDefinitions.java:677) ~[thymeleaf-3.1.1.RELEASE.jar:3.1.1.RELEASE]
    at org.thymeleaf.engine.AttributeDefinitions.forHTMLName(AttributeDefinitions.java:405) ~[thymeleaf-3.1.1.RELEASE.jar:3.1.1.RELEASE]
    at org.thymeleaf.engine.AttributeDefinitions.forName(AttributeDefinitions.java:336) ~[thymeleaf-3.1.1.RELEASE.jar:3.1.1.RELEASE]
    at org.thymeleaf.engine.TemplateHandlerAdapterMarkupHandler.handleAttribute(TemplateHandlerAdapterMarkupHandler.java:492) ~[thymeleaf-3.1.1.RELEASE.jar:3.1.1.RELEASE]
    at org.thymeleaf.templateparser.markup.InlinedOutputExpressionMarkupHandler$InlineMarkupAdapterPreProcessorHandler.handleAttribute(InlinedOutputExpressionMarkupHandler.java:360) ~[thymeleaf-3.1.1.RELEASE.jar:3.1.1.RELEASE]
    at org.thymeleaf.standard.inline.OutputExpressionInlinePreProcessorHandler.handleAttribute(OutputExpressionInlinePreProcessorHandler.java:260) ~[thymeleaf-3.1.1.RELEASE.jar:3.1.1.RELEASE]
    at org.thymeleaf.templateparser.markup.InlinedOutputExpressionMarkupHandler.handleAttribute(InlinedOutputExpressionMarkupHandler.java:205) ~[thymeleaf-3.1.1.RELEASE.jar:3.1.1.RELEASE]
    at org.attoparser.HtmlElement.handleAttribute(HtmlElement.java:250) ~[attoparser-2.0.6.RELEASE.jar:2.0.6.RELEASE]
    at org.attoparser.HtmlMarkupHandler.handleAttribute(HtmlMarkupHandler.java:529) ~[attoparser-2.0.6.RELEASE.jar:2.0.6.RELEASE]
    at org.attoparser.MarkupEventProcessorHandler.handleAttribute(MarkupEventProcessorHandler.java:593) ~[attoparser-2.0.6.RELEASE.jar:2.0.6.RELEASE]
    at org.attoparser.ParsingAttributeSequenceUtil.parseAttributeSequence(ParsingAttributeSequenceUtil.java:300) ~[attoparser-2.0.6.RELEASE.jar:2.0.6.RELEASE]
    at org.attoparser.ParsingElementMarkupUtil.parseStandaloneElement(ParsingElementMarkupUtil.java:90) ~[attoparser-2.0.6.RELEASE.jar:2.0.6.RELEASE]
    at org.attoparser.MarkupParser.parseBuffer(MarkupParser.java:706) ~[attoparser-2.0.6.RELEASE.jar:2.0.6.RELEASE]
    at org.attoparser.MarkupParser.parseDocument(MarkupParser.java:301) ~[attoparser-2.0.6.RELEASE.jar:2.0.6.RELEASE]
    ... 42 common frames omitted

register.html

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
<html lang="en" xmlns:th="http://www.thymeleaf.org">
    <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.2/dist/umd/popper.min.js" integrity="sha384-IQsoLXl5PILFhosVNubq5LC7Qb9DXgDA9i+tQ8Zj3iwWAwPtgFTxbJ8NT4GN1R8p" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.min.js" integrity="sha384-cVKIPhGWiC2Al4u+LWgxfKTRIcfu0JTxR+EQDz/bgldoEyl4H0zUF0QKbrJ0EcQF" crossorigin="anonymous"></script>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">

    </head>
    <body>
        <style>
            @import url('https://fonts.googleapis.com/css2?family=Rubik:wght@550&display=swap');
            *{
                font-family: 'Rubik', sans-serif;
                margin:15px inherit;

            }
            .btn{
                padding: 14px 6px;
                font-size:28px;
                border-radius: 30px;
                background: #1a8ecb;
                color:black;
                border:none;
            }
        </style>
    <section class="vh-100"
             style="background-image: url('https://www.bleepstatic.com/content/hl-images/2022/12/08/medical-data-header.jpg');
             height: 70%;">
        <div class="mask d-flex align-items-center vh-100 gradient-custom-4">
            <div class="container mt-0">
                <div class="row d-flex justify-content-center align-items-center">
                    <div class="col-12 col-md-9 col-md-5 col-lg-6">
                        <div class="card container-md" style="border-radius: 25px; background-color:#c2f3ff;opacity: 95%;">
                            <div class="card-body p-5 container mt-2">
                                <h2 class="text-uppercase text-center mt-3">Create an Account</h2>
                                <form th:action="@{/register}"th:object="${patient}" method="post">

                                    <div class="form-inline mb-3">
                                        <input type="text" id="form3Example1cg" 
                                               class="form-control form-control-md"
                                               th:field="*{name}"
                                               placeholder="Please Enter Your Name..." />
                                        <label class="form-label" for="form3Example1cg">Your Name</label>
                                    </div>

                                    <div class="form-inline mb-3">
                                        <input type="email" id="form3Example3cg"
                                               class="form-control form-control-md"
                                               th:field="*{email}"
                                               placeholder="Please Enter Your Email"/>
                                        <label class="form-label" for="form3Example3cg">Your Email</label>
                                    </div>

                                    <div class="form-inline mb-3">
                                        <input type="password" id="form3Example4cg"
                                               class="form-control form-control-md"
                                               th:field="*{password}"
                                               placeholder="Please Enter Your Password"/>
                                        <label class="form-label" for="form3Example4cg">Password</label>
                                    </div>

                                    <div class="form-inline mb-3">
                                        <input type="password" id="form3Example4cdg"
                                               class="form-control form-control-md"
                                               placeholder="Please Enter Your Password Again"
                                               th:field="*{repeatPassword}"
                                               th:=if"${#fields.hasError('isPasswordEquals')}"/>
                                        <label class="form-label" for="form3Example4cdg">Repeat your password</label>
                                    </div>

                            </div>
                            <div class="form-check d-flex justify-content-center mx-2">
                                <input class="form-check-input mt-3 mx-0" type="checkbox" value="" id="form2Example3cg" />
                                <label class="form-check-label mt-3 mx-0" for="form2Example3g">
                                    I agree all statements in <a href="#!" class="text-body"><u>Terms of service</u></a>
                                </label>
                            </div>

                            <div class="d-flex justify-content-center mt-0">
                                <button type="button"
                                        class="btn btn-success btn-block btn-sm gradient-custom-4 text-body">Register</button>
                            </div>

                            <p class="text-center  mt-1">Have already an account? <a href="login.html"
                                                                                     class="fw-bold text-body"><u>Login here</u></a></p>

                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>
</body>

输出说总是null或空或thymeleaf无法解析。有一件事失踪,但我不能理解,什么是失踪。基本上我的代码一样。我检查了几次我的请求,但我无法获得任何可接受的结果。
Spring Boot框架

s1ag04yj

s1ag04yj1#

它与thymeleaf无关,它表示在调用PatientService的方法时,您的控制器中存在NPE
你得到NPE是因为你没有自动将这个依赖关系连接到你的控制器上,你只是声明了一个字段,仅此而已。
因此,您应该将@Autowired(例如,或使用构造函数注入)放在控制器类的private PatienceService patienceService;字段之上。

相关问题