JSP,显示每个表中的两个表的结果

时间:2015-08-14 10:01:24

标签: spring hibernate jsp

在JSP中我有简单的foreach,它应该显示来自两个表的信息。第一桌"组织" as" country"参数将country的id作为外键保存到另一个表" Countries"。 如何在这个foreach中显示国家名称,该名称保留在“组织"作为国家的身份?

<c:forEach items=“${organizations}" var=“organization">
<c:url var="edit" value="/edit/${organization.id}" />
<c:url var="remove" value="/remove/${organization.id}" />
<tr>
<td><c:out value="${organization.name}" /></td>
<td><c:out value="${organization.country}" /></td> // In this line
<td><c:out value="${organization.address}" /></td>
<td><c:out value="${organization.phone}" /></td>
<td><c:out value="${organization.market_cap}" /></td>
<td valign = "top"><a href="${edit}">Edit</a></td>
<td valign = "top"><a href="${remove}">Remove</a></td>
</tr>
</c:forEach>

表:

CREATE TABLE IF NOT EXISTS Organization (id int auto_increment , name varchar(255), country int, address varchar(255), phone varchar(255)primary key(id), foreign key (country) references public.country(id_country));
CREATE TABLE IF NOT EXISTS Country (id_country int auto_increment , name varchar(255), primary key(id_country));

模特,组织:

import javax.persistence.*;


@Entity
public class Organization {

    @Id
    @GeneratedValue
    private Integer id;
    private String name;
    private Integer country;
    private String address;
    private String phone;
    private Long market_cap;

    public Organization(Integer id, String name, Integer country, String address, String phone, Long market_cap) {
        this.id = id;
        this.name = name;
        this.country = country;
        this.address = address;
        this.phone = phone;
        this.market_cap = market_cap;
    }

    public Organization() {
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public Integer getCountry() {
        return country;
    }

    public void setCountry(Integer country) {
        this.country = country;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public Long getMarket_cap() {
        return market_cap;
    }

    public void setMarketCap(Long market_cap) {
        this.market_cap = market_cap;
    }

}

模特,国家:

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class Country {
    @Id
    @GeneratedValue
    private Integer id_country;
    private String name;
    private String isocode;

    public Country() {
    }

    public Country(Integer id_country, String name, String isocode) {
        this.id_country = id_country;
        this.name = name;
        this.isocode = isocode;
    }

    public Integer getId_country() {
        return id_country;
    }

    public void setId_country(Integer id_country) {
        this.id_country = id_country;
    }

    public String getName() {
        return name;
    }

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

    public String getIsocode() {
        return isocode;
    }

    public void setIsocode(String isocode) {
        this.isocode = isocode;
    }
}

控制器:

@Controller
@RequestMapping("/")
public class HomeController {

    private final OrganizationService organizationService;
    private final CountryService countryService;

    @Autowired
    public HomeController(final OrganizationService organizationService, final CountryService countryService) {
        this.organizationService = organizationService;
        this.countryService = countryService;
    }

        @RequestMapping(value="add", method=RequestMethod.GET)
        public ModelAndView addOrganization() {
        ModelAndView modelAndView = new ModelAndView("add");
        Organization organization = new Organization();
        modelAndView.addObject("organization", organization);
        List<Country> countries = countryService.listOfCountries();
        modelAndView.addObject("countries", countries);
        return modelAndView;
    }

    @RequestMapping(value="add", method=RequestMethod.POST)
    public ModelAndView addingConfirm(Organization organization)
{
        ModelAndView modelAndView = new ModelAndView("confirm");
        organizationService.addOrganization(organization);
        String message = "Organization was successfully added.";
        modelAndView.addObject("message", message);
        return modelAndView;
    }

    @RequestMapping(method = RequestMethod.GET)
    public ModelAndView list() {
        ModelAndView modelAndView = new ModelAndView("index");
        List<Organization> organizations = organizationService.listOfOrganizations();
        modelAndView.addObject("organizations", organizations);
        return modelAndView;
    }

    @RequestMapping(value="/edit/{id}", method=RequestMethod.GET)
    public ModelAndView editOrganization(@PathVariable Integer id) {
        ModelAndView modelAndView = new ModelAndView("edit");
        Organization organization = organizationService.getOrganization(id);
        modelAndView.addObject("organization", organization);
        List<Country> countries = countryService.listOfCountries();
        modelAndView.addObject("countries", countries);
        return modelAndView;
    }

    @RequestMapping(value="/edit/{id}", method=RequestMethod.POST)
    public ModelAndView editConfirm(@ModelAttribute Organization organization, @PathVariable Integer id) {
        ModelAndView modelAndView = new ModelAndView("confirm");
        organizationService.editOrganization(organization);
        String message = "Organization was successfully edited.";
        modelAndView.addObject("message", message);
        return modelAndView;
    }

    @RequestMapping(value="/remove/{id}", method=RequestMethod.GET)
    public ModelAndView removeOrganization(@PathVariable Integer id) {
        ModelAndView modelAndView = new ModelAndView("confirm");
        organizationService.removeOrganization(id);
        String message = "Organization was successfully deleted.";
        modelAndView.addObject("message", message);
        return modelAndView;
    }
}

1 个答案:

答案 0 :(得分:0)

您的模型不适合此类操作。你需要做的是:

您的模型必须更改:

import javax.persistence.*;


@Entity
public class Organization {

    @Id
    @GeneratedValue
    private Integer id;
    private String name;
    //This will load automatically when you load your Organization entity.
    @OneToOne(cascade=CascadeType.ALL,fetch=FetchType.EAGER)
    @JoinColumn(name="country")
    private Country country;
    private String address;
    private String phone;
    private Long market_cap;

    public Organization(Integer id, String name, Country country, String address, String phone, Long market_cap) {
        this.id = id;
        this.name = name;
        this.country = country;
        this.address = address;
        this.phone = phone;
        this.market_cap = market_cap;
    }

    public Organization() {
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public Country getCountry() {
        return country;
    }

    public void setCountry(Country country) {
        this.country = country;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public Long getMarket_cap() {
        return market_cap;
    }

    public void setMarketCap(Long market_cap) {
        this.market_cap = market_cap;
    }

}

并在那种情况下查看jsp:

<c:forEach items=“${organizations}" var=“organization">
<c:url var="edit" value="/edit/${organization.id}" />
<c:url var="remove" value="/remove/${organization.id}" />
<tr>
<td><c:out value="${organization.name}" /></td>
<td><c:out value="${organization.country.name}" /></td> //Should do the trick
<td><c:out value="${organization.address}" /></td>
<td><c:out value="${organization.phone}" /></td>
<td><c:out value="${organization.market_cap}" /></td>
<td valign = "top"><a href="${edit}">Edit</a></td>
<td valign = "top"><a href="${remove}">Remove</a></td>
</tr>
</c:forEach>