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



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

org.springframework.beans。noreadablepropertyexception: bean类的"username"属性无效。字符串]:Bean属性'username'是不可读的或有一个无效的getter方法:getter的返回类型是否匹配setter的参数类型?

为什么错误指向String类如java.lang.String?它应该取自我的UserForm对象。

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用户,但仍然显示相同的错误。

谢谢你,

在LoginPageController中,更改为

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

相关内容

  • 没有找到相关文章

最新更新