Spring MVC中StoreController servlet的问题

时间:2015-07-29 21:41:48

标签: java spring servlets

我正在编写关于Spring MVC的教程,我在StoreController中遇到了一个我似乎无法排序的错误。我添加的三个代码片段是StoreController.java(servlet),Album.java和Genre.java,它们是Entity类。我收到StoreController的第76行错误,如下所示:对于Integer类型,方法getName()未定义。我将在下面添加代码,我希望有人可以帮助我理解我的问题: 感谢致敬 格斯

代码:storeController.java

package com.MVCMusicStore.Controllers;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.MVCMusicStore.Entities.Album;
import com.MVCMusicStore.Entities.Genre;
import com.MVCMusicStore.Models.AlbumModel;
import com.MVCMusicStore.Models.GenreModel;
import com.MVCMusicStore.Models.ArtistModel;
import com.MVCMusicStore.Entities.Artist;

@Controller
@RequestMapping("/Store")
public class StoreController {

    @Resource(name = "genreService")
    private GenreModel genreModel;

    @Resource(name = "albumService")
    private AlbumModel albumModel;


    /**
     * Map Root of Store Page
     * @param model
     * @return
     */

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String getStoreIndexPage(ModelMap model) {

        // This is for the side menu
        List<Genre> myGenreList = genreModel.findAllGenres();
        model.put("genreList" , myGenreList);
        model.put("genreCount", myGenreList.size());

        // This is for the albums
        //List<Genre> myGenreList = genreModel.findAllGenres();
        //model.put("allAlbums", allAlbums);

        return "storeindex";
    }


     /**
     * Map the Browse page
     *
     * @param genreName
     * @param model
     * @return
     */
    @RequestMapping(value = "/Browse", method = RequestMethod.GET)
    public String getStoreBrowsePage(
            @RequestParam(value = "genre", required = false) String genreName,
            ModelMap model) {



//            It's for albums
        List<Album> foundAlbum = new ArrayList<Album>();

        if (genreName == null) {
            model.put("genre", "Empty");
        } else {
            List<Album> myAlbum = albumModel.findAllAlbums();
            for (Album tempAlbum : myAlbum) {
                if (tempAlbum.getGenreid().getName().equals(genreName)) 
                {
                    foundAlbum.add(tempAlbum);
                }
            }

            model.put("genre", genreName);
            model.put("foundAlbum", foundAlbum);
        }



        // "genre" - should have the same name as value="genre"
        //model.addAttribute("genre", genre);
        return "Browse";
    }

    /**
     * Map the Browse Page
     * @param genre
     * @param model
     * @return
     */

    //@RequestMapping(value = "/Browse", method = RequestMethod.GET)
    //public String getStoreBrowsePage(@RequestParam(value="genre", required=false) 
    //String genre, ModelMap model) {

        //model.addAttribute("genre",genre);
        //return "Browse";
    //}


}

Album.java

package com.MVCMusicStore.Entities;

/**
 * @author Gus
 *
 */
import java.io.Serializable;
import java.math.BigDecimal;
import java.math.BigInteger;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlRootElement;

/**
 *
 * @author Gus
 */
@Entity
@Table(name = "ALBUM")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Album.findAll", query = "SELECT a FROM Album a"),
    @NamedQuery(name = "Album.findByAlbumid", query = "SELECT a FROM Album a WHERE a.albumid = :albumid"),
    @NamedQuery(name = "Album.findByGenreid", query = "SELECT a FROM Album a WHERE a.genreid = :genreid"),
    @NamedQuery(name = "Album.findByArtistid", query = "SELECT a FROM Album a WHERE a.artistid = :artistid"),
    @NamedQuery(name = "Album.findByTitle", query = "SELECT a FROM Album a WHERE a.title = :title"),
    @NamedQuery(name = "Album.findByPrice", query = "SELECT a FROM Album a WHERE a.price = :price"),
    @NamedQuery(name = "Album.findByAlbumarturl", query = "SELECT a FROM Album a WHERE a.albumarturl = :albumarturl")})
public class Album implements Serializable {
    private static final long serialVersionUID = 1L;
    // @Max(value=?)  @Min(value=?)//if you know range of your decimal fields consider using these annotations to enforce field validation
    @Id
    @Basic(optional = false)
    @Column(name = "ALBUMID")
    private Integer albumid;
    @Column(name = "GENREID")
    private Integer genreid;
    @Column(name = "ARTISTID")
    private Integer artistid;
    @Column(name = "TITLE")
    private String title;
    @Column(name = "PRICE")
    private Integer price;
    @Column(name = "ALBUMARTURL")
    private String albumarturl;

    public Album() {
    }

    public Album(Integer albumid) {
        this.albumid = albumid;
    }

    public Integer getAlbumid() {
        return albumid;
    }

    public void setAlbumid(Integer albumid) {
        this.albumid = albumid;
    }

    public Integer getGenreid() {
        return genreid;
    }

    public void setGenreid(Integer genreid) {
        this.genreid = genreid;
    }

    public Integer getArtistid() {
        return artistid;
    }

    public void setArtistid(Integer artistid) {
        this.artistid = artistid;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public Integer getPrice() {
        return price;
    }

    public void setPrice(Integer price) {
        this.price = price;
    }

    public String getAlbumarturl() {
        return albumarturl;
    }

    public void setAlbumarturl(String albumarturl) {
        this.albumarturl = albumarturl;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (albumid != null ? albumid.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Album)) {
            return false;
        }
        Album other = (Album) object;
        if ((this.albumid == null && other.albumid != null) || (this.albumid != null && !this.albumid.equals(other.albumid))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.MVCMusicStore.Entities.Album[ albumid=" + albumid + " ]";
    }

}

Genre.java

package com.MVCMusicStore.Entities;

import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlRootElement;
import com.MVCMusicStore.Entities.Artist;
import com.MVCMusicStore.Entities.Album;

/**
 *
 * @author Gus
 */
@Entity
@Table(name = "GENRE")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Genre.findAll", query = "SELECT g FROM Genre g"),
    @NamedQuery(name = "Genre.findByGenreid", query = "SELECT g FROM Genre g WHERE g.genreid = :genreid"),
    @NamedQuery(name = "Genre.findByName", query = "SELECT g FROM Genre g WHERE g.name = :name"),
    @NamedQuery(name = "Genre.findByDescription", query = "SELECT g FROM Genre g WHERE g.description = :description")})
public class Genre implements Serializable {
    private static final long serialVersionUID = 1L;
    // @Max(value=?)  @Min(value=?)//if you know range of your decimal fields consider using these annotations to enforce field validation
    @Id
    @Basic(optional = false)
    @Column(name = "GENREID")
    private Integer genreid;
    @Column(name = "NAME")
    private String name;
    @Column(name = "DESCRIPTION")
    private String description;

    public Genre() {
    }

    public Genre(Integer genreid) {
        this.genreid = genreid;
    }

    public Integer getGenreid() {
        return genreid;
    }

    public void setGenreid(Integer genreid) {
        this.genreid = genreid;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (genreid != null ? genreid.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Genre)) {
            return false;
        }
        Genre other = (Genre) object;
        if ((this.genreid == null && other.genreid != null) || (this.genreid != null && !this.genreid.equals(other.genreid))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.MVCMusicStore.Entities.Genre[ genreid=" + genreid + " ]";
    }

}

2 个答案:

答案 0 :(得分:0)

错误告诉你这里到底出了什么问题。当你运行这个: tempAlbum.getGenreid().getName().equals(genreName)

方法getGenreid()返回一个Integer,然后当你真正想要做的是在一个类型对象上运行getName()方法时,你试图获取整数的名称。

答案 1 :(得分:0)

相册实体

@Column(name = "GENREID")
private Integer genreid;

<强> StoreController

for (Album tempAlbum : myAlbum) {
      if (tempAlbum.getGenreid().getName().equals(genreName)) <- does it even compile ? Don't think so

如果您想使其有效,则需要更改Album实体:

package com.MVCMusicStore.Entities;

/**
 * @author Gus
 *
 */
import java.io.Serializable;
import java.math.BigDecimal;
import java.math.BigInteger;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlRootElement;

/**
 *
 * @author Gus
 */
@Entity
@Table(name = "ALBUM")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Album.findAll", query = "SELECT a FROM Album a"),
    @NamedQuery(name = "Album.findByAlbumid", query = "SELECT a FROM Album a WHERE a.albumid = :albumid"),
    //@NamedQuery(name = "Album.findByGenreid", query = "SELECT a FROM Album a WHERE a.genreid = :genreid"),
    @NamedQuery(name = "Album.findByArtistid", query = "SELECT a FROM Album a WHERE a.artistid = :artistid"),
    @NamedQuery(name = "Album.findByTitle", query = "SELECT a FROM Album a WHERE a.title = :title"),
    @NamedQuery(name = "Album.findByPrice", query = "SELECT a FROM Album a WHERE a.price = :price"),
    @NamedQuery(name = "Album.findByAlbumarturl", query = "SELECT a FROM Album a WHERE a.albumarturl = :albumarturl")})
public class Album implements Serializable {
    private static final long serialVersionUID = 1L;
    // @Max(value=?)  @Min(value=?)//if you know range of your decimal fields consider using these annotations to enforce field validation
    @Id
    @Basic(optional = false)
    @Column(name = "ALBUMID")
    private Integer albumid;
    //@Column(name = "GENREID")
    //private Integer genreid;

    @OneToOne(cascade=CascadeType.ALL,fetch = FetchType.EAGER)
    @JoinColumn(name="genreId")
    private Genre genre;//Added new field with mapping to Genre

    @Column(name = "ARTISTID")
    private Integer artistid;
    @Column(name = "TITLE")
    private String title;
    @Column(name = "PRICE")
    private Integer price;
    @Column(name = "ALBUMARTURL")
    private String albumarturl;

    public Album() {
    }

    public Album(Integer albumid) {
        this.albumid = albumid;
    }

    public Integer getAlbumid() {
        return albumid;
    }

    public void setAlbumid(Integer albumid) {
        this.albumid = albumid;
    }

    //public Integer getGenreid() {
    //    return genreid;
    //}

    //public void setGenreid(Integer genreid) {
    //    this.genreid = genreid;
    //}

    public Integer getArtistid() {
        return artistid;
    }

    public void setArtistid(Integer artistid) {
        this.artistid = artistid;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public Integer getPrice() {
        return price;
    }

    public void setPrice(Integer price) {
        this.price = price;
    }

    public String getAlbumarturl() {
        return albumarturl;
    }

    public void setAlbumarturl(String albumarturl) {
        this.albumarturl = albumarturl;
    }

    public Genre getGenre() {
        return genre;
    }

    public void setGenre(Genre genre) {
        this.genre = genre;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (albumid != null ? albumid.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Album)) {
            return false;
        }
        Album other = (Album) object;
        if ((this.albumid == null && other.albumid != null) || (this.albumid != null && !this.albumid.equals(other.albumid))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.MVCMusicStore.Entities.Album[ albumid=" + albumid + " ]";
    }

}

然后在StoreController更改这些行:

    for (Album tempAlbum : myAlbum) {
        if (tempAlbum.getGenre().getName().equals(genreName)) <- See the changes ?
        {
            foundAlbum.add(tempAlbum);
        }
    }

您的Album实体需要&#34;抓住&#34;到Genre实体。如果你想这样做,你需要在这两个实体之间建立@OneToOne

还有一件事。如果此更改有效,则您的genreId实体中不需要Album