无法将类型“java.lang.String”的值转换为所需类型“java.lang.Long”;对于输入字符串:“employeedetail.html”

问题描述 投票:0回答:1

我正在尝试通过在我的 Springboot 应用程序中通过 ID 搜索员工详细信息来显示员工详细信息。 我的index.html 中有一个HTML 表单,它获取员工ID 并返回employeedetail.html 页面,其中包含相应的详细信息。 我尝试在 javascript 中执行此操作,因为我的老师要求我不要使用 Thymeleaf。

我收到此错误:

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

Tue Apr 23 23:20:19 IST 2024
There was an unexpected error (type=Bad Request, status=400).
Failed to convert value of type 'java.lang.String' to required type 'java.lang.Long'; For input string: "employeedetail.html"
org.springframework.web.method.annotation.MethodArgumentTypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'java.lang.Long'; For input string: "employeedetail.html"
    at org.springframework.web.method.annotation.AbstractNamedValueMethodArgumentResolver.convertIfNecessary(AbstractNamedValueMethodArgumentResolver.java:302)
    at org.springframework.web.method.annotation.AbstractNamedValueMethodArgumentResolver.resolveArgument(AbstractNamedValueMethodArgumentResolver.java:137)
    at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:122)
    at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:224)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:178)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:118)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:926)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:831)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1089)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:979)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1014)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:903)
    at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:564)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:885)
    at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:658)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:150)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:175)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:150)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:110)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:175)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:150)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:110)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:175)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:150)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:110)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:175)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:150)
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:653)
    at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:419)
    at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:340)
    at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:277)
    at org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:171)
    at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:314)
    at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1431)
    at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1167)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1106)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:979)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1014)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:903)
    at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:564)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:885)
    at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:658)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:150)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:175)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:150)
    at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:175)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:150)
    at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:175)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:150)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:175)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:150)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:167)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:90)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:482)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:115)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:93)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:344)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:391)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:63)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:896)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1736)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:52)
    at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191)
    at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:63)
    at java.base/java.lang.Thread.run(Thread.java:833)
Caused by: java.lang.NumberFormatException: For input string: "employeedetail.html"
    at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67)
    at java.base/java.lang.Long.parseLong(Long.java:711)
    at java.base/java.lang.Long.valueOf(Long.java:1163)
    at org.springframework.util.NumberUtils.parseNumber(NumberUtils.java:206)
    at org.springframework.beans.propertyeditors.CustomNumberEditor.setAsText(CustomNumberEditor.java:115)
    at org.springframework.beans.TypeConverterDelegate.doConvertTextValue(TypeConverterDelegate.java:439)
    at org.springframework.beans.TypeConverterDelegate.doConvertValue(TypeConverterDelegate.java:412)
    at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:161)
    at org.springframework.beans.TypeConverterSupport.convertIfNecessary(TypeConverterSupport.java:80)
    at org.springframework.beans.TypeConverterSupport.convertIfNecessary(TypeConverterSupport.java:53)
    at org.springframework.validation.DataBinder.convertIfNecessary(DataBinder.java:860)
    at org.springframework.web.method.annotation.AbstractNamedValueMethodArgumentResolver.convertIfNecessary(AbstractNamedValueMethodArgumentResolver.java:294)
    ... 77 more

这是我的index.html 表单,我正在其中通过 ID 搜索员工


<div class="card">
            <div class="card-body">
                <h5 class="card-title">Search Employee By ID</h5>
                <p class="card-text">Enter employee ID to search</p>
                <form class="d-flex" role="search" action="/employees/{id}" method="get">
                    <input
                            class="form-control me-2"
                            type="search"
                            placeholder="Search"
                            aria-label="Search"
                            name="id"
                            value="0"
                    />

                    <button class="btn btn-outline-success" type="submit">
                        Search
                    </button>
                </form>


            </div>
        </div>

以及相应的 javascript

<script>
    // Get the form and input element by their IDs
const form = document.querySelector('form'); // Selecting the form element
const input = document.querySelector('input[type="search"]'); // Selecting the input element

// Add event listener for form submission
form.addEventListener('submit', function(event) {
  // Prevent default form submission
  event.preventDefault();

  // Get the ID value from the input field
  const id = input.value.trim();

  // Regular expression to match only digits
  const numberRegex = /^\d+$/;

  // Validate if the ID consists only of digits
  if (!numberRegex.test(id)) {
    alert("Please enter a valid numeric ID.");
    return; // Prevent form submission if validation fails
  }

  // Rest of the code for constructing URL and submitting the form (unchanged)
  const url = '/employees/' + encodeURIComponent(id);
  form.action = url;
  form.submit();
});

// Add event listener for input changes
input.addEventListener('keyup', function() {
  const id = input.value.trim();
  const numberRegex = /^\d+$/;
  if (!numberRegex.test(id)) {
    // Add visual cue or display an error message if validation fails
    input.classList.add('invalid'); // Example class for styling
  } else {
    input.classList.remove('invalid'); // Remove error class on valid input
  }
});
</script>

这是“/employees/{id}”的控制器

@GetMapping("/employees/{id}")
    public String getEmployeeDetail(@PathVariable("id") Long id, Model model) {
        Optional<Employee> employee;
            employee = employeeService.getEmployeeById(id);
        if (employee.isPresent()) {
            model.addAttribute("employee", employee.get());
            return "employeedetail.html";
        } else {
            return "error.html";
        }
    }

这是来自employeedetail.html页面的javascript代码,我试图在其中显示详细信息

<script>
    // Function to fetch employee details by ID
    function fetchEmployeeDetailsById(id) {
      fetch(`/employees/${id}`)
        .then(response => {
          if (!response.ok) {
            throw new Error('Failed to fetch employee details');
          }
          return response.json();
        })
        .then(employee => {
          // Display employee details
          const employeeDetails = document.getElementById('employeeDetails');
          employeeDetails.innerHTML = `
            <p><strong>First Name:</strong> ${employee.firstName}</p>
            <p><strong>Last Name:</strong> ${employee.lastName}</p>
            <p><strong>Department:</strong> ${employee.department}</p>
            <p><strong>Gender:</strong> ${employee.gender}</p>
            <p><strong>Street Address:</strong> ${employee.streetAddress}</p>
            <p><strong>City:</strong> ${employee.city}</p>
            <p><strong>State:</strong> ${employee.state}</p>
            <p><strong>Country:</strong> ${employee.country}</p>
            <p><strong>Phone:</strong> ${employee.phone}</p>
            <p><strong>Email:</strong> ${employee.email}</p>
          `;
        })
        .catch(error => console.error('Error fetching employee details:', error));
    }

    // Get employee ID from the URL parameter
    const urlParams = new URLSearchParams(window.location.search);
    const employeeId = urlParams.get('id');

    // Call the function to fetch and display employee details
    if (employeeId) {
      console.log(employeeId);
      fetchEmployeeDetailsById(employeeId);
    } else {
      // Handle case when employee ID is not provided
      console.error('Employee ID is missing');
    }
</script>

我的员工实体

package com.example.proj.entity;

import jakarta.persistence.*;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;

@Entity
@Data
@Table(name = "employees")
public class Employee {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String firstName;
    private String lastName;
    private String department;

    @Enumerated(EnumType.STRING)
    private Gender gender;

    private enum Gender {
        MALE,
        FEMALE,
        OTHER
    }

    private String streetAddress;
    private String city;
    private String state;
    private String country;
    private String phone;
    private String countryCode;
    private String email;
    private String password;

}

我可能做错了什么?

javascript java spring-boot
1个回答
0
投票

看起来您正在尝试使用不正确的参数类型调用方法

getEmployeeDetail
。您提供的以下定义有两个参数,即
id
model

public String getEmployeeDetail(@PathVariable("id") Long id, Model model) {
    ...
}

您收到的错误似乎暗示类型为

String
的参数正在为
id
而不是
Long
传递。如果您尝试从路径中获取
id
作为
Long
,您可能必须将参数类型声明为
String
,然后在函数中解析它,如下所示:

public String getEmployeeDetail(@PathVariable("id") String idString, Model model) {
    Long id = Long.parseLong(idString);
    ...
}
© www.soinside.com 2019 - 2024. All rights reserved.