hibernate 如何在Spring中处理值类型转换

plupiseo  于 2022-11-14  发布在  Spring
关注(0)|答案(1)|浏览(145)

我是Spring的新手,我面临着一个值类型转换问题。在需要整型的Free Pass字段中输入字符串时,我希望从属性文件中得到自定义错误消息“INVALID NUMBER”,但我得到的是:

Failed to convert property value of type java.lang.String to required type java.lang.Integer for property freePasses; nested exception is java.lang.NumberFormatException: For input string: "sdf"

我是不是做错了什么,或者错过了一些Java库?
控制器:

import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.validation.Valid;

@Controller
@RequestMapping("/customer")
public class CustomerController {

    @InitBinder
    public void initBinder(WebDataBinder dataBinder) {
        StringTrimmerEditor stringTrimmerEditor = new StringTrimmerEditor(true);
        dataBinder.registerCustomEditor(String.class, stringTrimmerEditor);
    }

    @RequestMapping("/showForm")
    public String showForm(Model theModel) {
        theModel.addAttribute("customer", new Customer());
        return "customer-form";
    }

    @RequestMapping("/processForm")
    public String processForm(
            @Valid @ModelAttribute("customer") Customer theCustomer,
            BindingResult theBindingResult
    ) {
        if (theBindingResult.hasErrors()) {
            return "customer-form";
        } else {
            return "customer-confirmation";
        }
    }
}

班级:

import javax.validation.constraints.*;

public class Customer {

    @NotNull(message="is required")
    @Min(value = 0, message = "cannot be less than 0")
    @Max(value = 10, message = "cannot be more than 10")
    private Integer freePasses;
    
    public Integer getFreePasses() {
        return freePasses;
    }

    public void setFreePasses(Integer freePasses) {
        this.freePasses = freePasses;
    }
}

配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:util="http://www.springframework.org/schema/util"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc.xsd
            http://www.springframework.org/schema/util
            http://www.springframework.org/schema/util/spring-util.xsd">

    <!-- Step 3: Add support for component scanning -->
    <context:component-scan base-package="com.autosdet.autosdetmvc" />

    <util:properties  id="countryOptions" location="classpath:countries.properties" />

    <!-- Step 4: Add support for conversion, formatting and validation support -->
    <mvc:annotation-driven/>

    <!-- Step 5: Define Spring MVC view resolver -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/view/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basenames" value="messages" />
    </bean>
</beans>

表格:

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Custome Form</title>
    <style>
        .error {color: red}
    </style>
</head>
<body>
    Fill out the form.Asterisk (*) required.
    <form:form action="processForm" modelAttribute="customer">
        Free Passes (*): <form:input path="freePasses" />
        <form:errors path="freePasses" cssClass="error" />
        <br><br>
        <input type="submit" value="Submit" />
    </form:form>
    <br><br>
    <a href="/autosdet_mvc_war_exploded">Main Menu</a>
</body>
</html>

属性:

type.Mismatch.customer.freePasses=Invalid number
4jb9z9bj

4jb9z9bj1#

Java.lang.NumberFormatException:输入字符串:“sdf”
在Spring中,字符串到整数的转换是使用Integer.parseInt()Integer.valueOf()执行的。显然,您的输入"sdf"不能转换为数值,这与"12345"不同。
我会检查您实际传递到表单中的值,即freePasses参数。看起来你是在给他们发短信,而不是数字:

<form:form action="processForm" modelAttribute="customer">
        Free Passes (*): <form:input path="freePasses" />
        <form:errors path="freePasses" cssClass="error" />
        <br><br>
        <input type="submit" value="Submit" />
    </form:form>

相关问题