bean类[java.lang.String]的属性'username'无效:Bean属性'username'不可读或具有无效的getter方法:

时间:2016-09-20 14:47:58

标签: jsp spring-mvc tomcat8

运行Spring MVC Web应用程序时出现以下错误: -

org.springframework.beans.NotReadablePropertyException:bean类的无效属性'username'[java.lang.String]:Bean属性'username'不可读或具有无效的getter方法:返回类型是getter匹配setter的参数类型?

为什么错误指向像java.lang.String这样的String类?它应该来自我的UserForm对象。

用户窗体: -

package myspring;

/**
 * @author vinod827
 *
 */
public class UserForm {


    private String username;
    private String password;
    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;
    }

}

控制器类(LoginPageController): -

/**
 * 
 */
package myspring;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * @author vinod827
 *
 */
@Controller
@RequestMapping(value = "/login")
public class LoginPageController {

    private final Logger logger = LoggerFactory.getLogger(LoginPageController.class);

    @RequestMapping(method = RequestMethod.GET)
    public String showLoginPage(ModelMap modelMap){
        logger.info("**************");
        UserForm userForm = new UserForm();
        modelMap.addAttribute("userForm", "userForm");
        return "login";
    }


    @RequestMapping(method = RequestMethod.POST)
    public String verifyLogin(UserForm userForm, BindingResult bindingResult, ModelMap modelMap){
        logger.info("********&&&&&&&&&******");
        String username = "username";
        String password = "password";       
        logger.info(username+":"+password);
        userForm = (UserForm) modelMap.get(userForm);       
        modelMap.addAttribute("userForm", userForm);
        return "success";
    }
}

JSP: -

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Sign-In</title>
</head>
<body>
<h3>Secure Sign-in</h3>
<form:form method="post" action="login" commandName="userForm">
   <form:input path="username" type="text"/>
   <form:errors path="username" /><br>
   <form:input path="password" type="password"/>
   <form:errors path="password" /><br><br>
    <form:button  id="submit" name="submit" value="submit">Submit</form:button> 
</form:form>
</body>
</html>

你能告诉我这个错误的地方吗? 我尝试将UserForm的类名重构为'User.java',并在JSP上相应地将用户名重命名为user,但仍显示相同的错误。

谢谢,

1 个答案:

答案 0 :(得分:0)

在LoginPageController中,切换到

modelMap.addAttribute("userForm", new UserForm());