Bean属性'favLang'不可读或具有无效的getter方法:getter的返回类型是否与setter的参数类型匹配?

时间:2019-09-26 09:35:11

标签: spring forms spring-mvc jsp spring-form

我创建了一个具有简单形式和模型的spring mvc项目。在运行项目时,它给出以下错误: Bean类[com.springdemo.mvc.Student]的无效属性'favLang':Bean属性'favLang'无法读取或具有无效的getter方法:getter的返回类型是否与setter的参数类型匹配?

Student.java

Props

学生控制器

package com.springdemo.mvc;

import java.util.LinkedHashMap;

public class Student {

    private String firstName;
    private String lastName;
    private String countryName;
    private LinkedHashMap<String, String> countryOptions;
    private String favLang;

    public Student() {

        countryOptions = new LinkedHashMap<>(); 
        countryOptions.put("BR", "Brazil");
        countryOptions.put("IN", "India");
        countryOptions.put("FR", "France");
        countryOptions.put("GE", "Germany");

    }

    public LinkedHashMap<String, String> getCountryOptions() {
        return countryOptions;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getCountryName() {
        return countryName;
    }

    public void setCountryName(String countryName) {
        this.countryName = countryName;
    }

    public String getFavLang() {
        return favLang;
    }

    public void setFavLang(String favLang) {
        this.favLang = favLang;
    }


}

student-form.jsp:

package com.springdemo.mvc;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/student")
public class StudentController {

    @RequestMapping("/showForm")
    public String showForm(Model theModel) {

        //create a new student object
        Student theStudent=new Student();

        //add student object as model attribute
        theModel.addAttribute("student", new Student());

        return "student-form";
    }

    @RequestMapping("/processForm")
    public String processForm(@ModelAttribute("student") Student theStudent) {

        System.out.println("theStudent: "+theStudent.getFirstName());
        return "student-confirmation";
    }

}

student-confirmation.jsp

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>

<html>
<head>
<title>Student Registration</title>
</head>
<body>

<form:form action="processForm" modelAttribute="student">
    First Name : <form:input path="firstName"/>
    <br><br>
    Last Name: <form:input path="lastName"/>
    <br><br>
<!--    Country :  -->
<%--    <form:select path="countryName"> --%>
<%--     <form:options items="${student.countryOptions}"/> --%>
<%--    </form:select> --%>
    <br><br>

    Fav Language: 
    Java <form:radiobutton path="favLang" value="Java"/>
    C++ <form:radiobutton path="favLang" value="C++"/>
    C# <form:radiobutton path="favLang" value="C#"/>
    PHP <form:radiobutton path="favLang" value="PHP"/>

    <input type="submit" value="Submit"/>


</form:form>
</body>

</html>

错误的完整堆栈跟踪:

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<!DOCTYPE html>
<html>
<head>
<title>Student Confirmation Page</title>
</head>
<body>
<p>The student is confirmed: ${student.firstName} ${student.lastName} </p>
<%-- <p>Country: ${student.countryName}</p> --%>
<p>Fav Language: ${student.favLang}</p>

</body>
</html>

有人可以让我知道这里的问题是什么吗?

1 个答案:

答案 0 :(得分:2)

您需要在modelAndView中添加对象,然后从StudentController返回该对象。 检查以下代码更改并运行项目。

Student.java

  public class Student {

  private String firstName;
  private String lastName;
  private String countryName;
  private LinkedHashMap<String, String> countryOptions;
  private String favLang;

  public Student() {

  countryoptions = new LinkedHashMap<String, String>();
  countryoptions.put("BR", "Brazil");
  countryoptions.put("IN", "India");
  countryoptions.put("FR", "France");
  countryoptions.put("GE", "Germany");

  }

  public LinkedHashMap<String, String> getCountryOptions() {
      return countryoptions;
  }
  public String getFirstname() {
      return firstname;
  }
  public void setFirstname(String firstname) {
      this.firstname = firstname;
  }
  public String getLastname() {
      return lastname;
  }
  public void setLastname(String lastname) {
      this.lastname = lastname;
  }
  public String getCountryname() {
      return countryname;
  }
  public void setCountryname(String countryname) {
      this.countryname = countryname;
  }
  public LinkedHashMap<String, String> getCountryoptions() {
      return countryoptions;
  }
  public void setCountryoptions(LinkedHashMap<String, String> countryoptions) {
      this.countryoptions = countryoptions;
  }
  public String getFavlang() {
      return favlang;
  }
  public void setFavlang(String favlang) {
      this.favlang = favlang;
  }
}

Student-Controller.java

@Controller
public class StudentController {
    @RequestMapping(value = "/showForm", method = RequestMethod.GET)
    public ModelAndView showRegister(HttpServletRequest request, HttpServletResponse 
    response) {
        ModelAndView mav = new ModelAndView("student-form");
        mav.addObject("student", new Student());
        return mav;
    }

    @RequestMapping(value = "/processForm", method = RequestMethod.POST)
    public ModelAndView addUser(HttpServletRequest request, HttpServletResponse 
    response,@ModelAttribute("student") Student student) {

        ModelAndView mav = new ModelAndView("student-confirmation");
        mav.addObject("firstname", student.getFirstname());
        mav.addObject("favName", student.getFavlang());
        return mav;
   }
}

student-form.jsp:

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Registration</title>
</head>
<body>
<form:form id="regForm" modelAttribute="user" action="processForm"
    method="post">

<table align="center">
        <tr>
            <td><form:label path="firstname">Username</form:label></td>
            <td><form:input path="firstname" name="username" id="username" /></td>
        </tr>
        <tr>
            <td><form:label path="lastname">Password</form:label></td>
            <td><form:password path="lastname" name="password"
                    id="password" /></td>
        </tr>
        <tr>
            <td>
                 Fav Language:
    Java <form:radiobutton path="favlang" value="Java"/>
   C++ <form:radiobutton path="favlang" value="C++"/>
   C# <form:radiobutton path="favlang" value="C#"/>
   PHP <form:radiobutton path="favlang" value="PHP"/>
            </td>
        </tr>
    </table>

<input type="submit" value="Submit"/>

</form:form>

student-confirmation.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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>Welcome</title>
</head>
<body>
    <table>
        <tr>
            <td>Welcome ${firstname}</td>
        </tr>
        <tr>
            <td><p>Fav Language: ${favName}</p></td>
        </tr>
    </table>
</body>
</html>

如果此解决方案适合您,请标记为答案。