更新表单错误 - Spring4 mvc - org.springframework.beans.TypeMismatchException:

时间:2017-09-14 11:19:35

标签: java spring spring-mvc

我正在尝试使用Spring mvc创建更新表单。用户单击更新按钮后,将显示输入标记中的当前值。当我编辑当前值并点击提交按钮时,它会显示以下错误。

  

org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver handleTypeMismatch       警告:无法绑定请求元素:org.springframework.beans.TypeMismatchException:无法将类型'java.lang.String'的值转换为必需的类型'int';嵌套异常是java.lang.NumberFormatException:对于输入字符串:“updateStudent”

针对home.jsp

<form:form method="GET" action="updateStudent" commandName="selectedUser">
        <table>

            <tr>
                <form:input path="userID" placeholder="userID"
                    value="${selectedUsers.userID}" />
                <form:input path="userName" placeholder="userName"
                    value="${selectedUsers.userName}" />
                <form:input path="password" placeholder="password"
                    value="${selectedUsers.password}" />
            </tr>
            <tr>
                <td colspan="2"><input type="submit" value="Submit" /></td>
            </tr>
        </table>
    </form:form>

controller.java

@RequestMapping(value = "updateStudent", method = RequestMethod.GET)

public String updateStudent(@ModelAttribute("selectedUser") User user, 
        ModelMap model) throws Exception {

    Student student = new Student();
    System.out.println("come here helloController()");
    model.addAttribute("userID", user.getUserID());
    model.addAttribute("userName", user.getUserName());
    model.addAttribute("password", user.getPassword());
    model.addAttribute("student",student);
    model.addAttribute("user",user);
    System.out.println("student id is " + user.getUserID());
    // model.addAttribute("userI", user);
    if (user.getUserID() == 0) {
        model.addAttribute("error", "please enter id");
        return "home";
    }
    if (user.getUserName() == null) {
        model.addAttribute("error", "please enter getUserName");
        return "home";
    }
    if (user.getPassword() == null) {
        model.addAttribute("error", "please enter getPassword");
        return "home";
    }
    return "home";
}

User.java

public class User {
    int userID;
    String userName;
    String password;

    public int getUserID() {
        return userID;
    }

    public void setUserID(int userID) {
        this.userID = userID;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

我需要将更新的值传递给控制器​​。希望你有问题。请帮助解决问题。

1 个答案:

答案 0 :(得分:0)

您的模型类User具有属性int id,它是原始类型数据。将其更改为Interger对象。

Integer userID;

与getter和setter一起。我认为一切都会好的。