简体   繁体   中英

Receivng error “Invalid property 'xxx' of bean class” when trying to submit form via JSTL form tags

I'm trying to build a simple Spring project where a user can search the database via form. It works perfectly if I use a regular form input, but if I try to use JSTL form tags it breaks. Any help would be appreciated

JSP:

<form:form action="/search" method="post" modelAttribute="music">
        <form:label path="artist">Artist</form:label>
        <form:errors path="artist"/>
        <form:input path="artist"/>
    <input type="submit" value="Submit"/>

Controller:

    @RequestMapping(value="/search", method=RequestMethod.POST)
        public String search(@RequestParam(value="artist") String search, Model model) {
        List<Music> results = musicService.findArtist(search);
        model.addAttribute("search", search);
        model.addAttribute("results", results);
        return "music/search.jsp";
    }

Service:

    public List<Music> findArtist(String search){
        return musicRepository.findByArtistContaining(search);
    }

repo:

List<Music> findByArtistContaining(String search);

Really not sure what I'm doing wrong, but I'm sure I'm messing up the JSP code somehow. I just want to pull the results from the DB via the repo method findByArtistContaining

error I'm receiving when trying to access is:

Invalid property 'artist' of bean class [java.util.ArrayList]: Bean property 'artist' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?

sorry late to the party, but I think this wouldn't work as the request param doesn't match the name of the String your trying to map it to, as stated here;

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/bind/annotation/RequestParam.html

So changing to below should work;

@RequestParam(name="artist") String artist,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM