org.springframework.beans.NotReadablePropertyExcep

2019-08-20 02:40发布

问题:

I have a dropdown, the value of which I am generating in my controller and passing that value to my jsp page. I am getting the following exception in the "itemValue" attribute of my "form:options" tag. Why do I need a getter setter for the itemValue because however I am having value for my "typeCode". But to avoid any confusion, I added getter setter for idValue also, but still getting the same error. Kindly help.

JSP:

<%
Map LCCLSTypeCode = (Map) request.getAttribute("LCCLSTypeCode"); 
System.out.println("LCCLSTypeCode:"+LCCLSTypeCode);
%>

<form:form action="/tradelc/ClsSave" method="POST" onsubmit="return CheckPassword()">
.
.
.
.
.
<form:select path="typeCode">
<form:options items="<%=LCCLSTypeCode%>" itemValue="idValue" itemLabel="displayValue"/>       
</form:select>

.
.
.
<a href="javascript:submitPageX();">Create</a>

<script>
function submitPageX() 
{
document.forms[0].submit();
            return false;
}

Controller:

@Controller
@SessionAttributes("clsData")
public class InformLoginAction{
@ModelAttribute("clsData")
    public ClauseData createBean() {
        return new ClauseData();
    }

@RequestMapping(value = "/informlogin", method = RequestMethod.GET)
    public ModelAndView execute( HttpServletRequest  request,
                                 HttpServletResponse response,
                                 @ModelAttribute("clsData") ClauseData clauseData,
                                 BindingResult bindingResult)
        throws Exception {
.
.
.
.
Map newMap = new HashMap();
while(enu!=null && enu.hasMoreElements())
        {
newMap.put(omxComboBean.assignId(item.trim()),omxComboBean.assignValue(text));
ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("menu1");
        modelAndView.addObject("command", new ClauseData());//Specifying the bean
        modelAndView.addObject("LCCLSTypeCode", newMap);//Specifying the value for the dropdown
        return modelAndView;
}
}

OmxComboBean.java:

public String assignId(String id)
    {
        idValue = id;
        return id;
    }

    public String assignValue(String value)
    {
        displayValue = value;
        return value;
    }

Bean class ClauseData.java:

public class ClauseData {
private String typeDesc;
private String idValue;
public String getTypeCode(){return typeCode;}
    public void setTypeCode(String typeCode){this.typeCode = typeCode;}
public String getIdValue(){return idValue;}
    public void setIdValue(String idValue){this.idValue = idValue;}
}

Exception:

org.springframework.beans.NotReadablePropertyException: Invalid property 'idValue' of bean class [java.lang.String]: Bean property 'idValue' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
    org.springframework.beans.BeanWrapperImpl.getPropertyValue(BeanWrapperImpl.java:723)
    org.springframework.beans.BeanWrapperImpl.getPropertyValue(BeanWrapperImpl.java:714)
    org.springframework.web.servlet.tags.form.OptionWriter.renderFromMap(OptionWriter.java:166)
    org.springframework.web.servlet.tags.form.OptionWriter.writeOptions(OptionWriter.java:136)
    org.springframework.web.servlet.tags.form.OptionsTag.writeTagContent(OptionsTag.java:155)
    org.springframework.web.servlet.tags.form.AbstractFormTag.doStartTagInternal(AbstractFormTag.java:84)

回答1:

Your controller should look like this

@RequestMapping(value = "/informlogin", method = RequestMethod.GET)
public ModelAndView execute(HttpServletRequest  request,HttpServletResponse response,@ModelAttribute("clsData") ClauseData clauseData,BindingResult bindingResult, ModelMap model) throws Exception {
    model.addAttribute("command", clauseData); // not modelAndView.addObject
    model.addAttribute("LCCLSTypeCode", map);
    ...
    return new ModelAndView("view name");
}

and here is jsp page

<form:form action="/tradelc/ClsSave" method="POST" onsubmit="return CheckPassword()" path="command">
...
<form:select path="idValue">
    <form:options items="${LCCLSTypeCode}"/>   <!-- you should not use scriptlet and once map has been attached to ModelMap you don't need to retrive it mannually by scriptlet -->    
</form:select>
...
<a href="javascript:submitPageX();">Create</a>