2016-09-20 85 views
0

我在運行Spring MVC Web應用程序時遇到以下錯誤: -Bean類的無效屬性'username'[java.lang.String]:Bean屬性'username'不可讀或具有無效的getter方法:

org.springframework.beans.NotReadablePropertyException:豆類的無效的屬性「用戶名」 [java.lang.String中]:Bean屬性「用戶名」不可讀或有一個無效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上將用戶名重命名爲用戶,但仍顯示相同的錯誤。

謝謝

回答

0

在LoginPageController,改爲

modelMap.addAttribute("userForm", new UserForm()); 
相關問題