Jean-Francois Leveque

Ajout de pays

package org.legrog.application;
import org.legrog.entities.Country;
public interface CountryService {
void addCountry(Country country);
}
package org.legrog.application;
import org.legrog.entities.Country;
import org.legrog.entities.CountryRepository;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
import javax.ejb.Stateless;
import javax.enterprise.inject.Alternative;
import javax.inject.Inject;
//import java.util.List;
@Stateless
public class CountryServiceSpring implements CountryService {
@Inject
CountryRepository repository;
public void addCountry(Country country) {
repository.save(country);
}
}
package org.legrog.entities;
import org.springframework.data.jpa.repository.JpaRepository;
public interface CountryRepository extends JpaRepository<Country, Integer> {
}
package org.legrog.presentation;
import org.legrog.application.CountryService;
import org.legrog.entities.Country;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.inject.Named;
import java.util.List;
@Named
@RequestScoped
public class CountryBean {
@Inject
private CountryService service;
private String countryName;
public String getCountryName() {
return countryName;
}
public void setCountryName(String countryName) {
this.countryName = countryName;
}
public String add()
{
Country country = new Country();
country.setCountryName(countryName);
service.addCountry(country);
return "success";
}
}
......@@ -15,6 +15,22 @@
<from-outcome>listBooks</from-outcome>
<to-view-id>/result.xhtml</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome>addCountry</from-outcome>
<to-view-id>/addCountry.xhtml</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome>listCountries</from-outcome>
<to-view-id>/listCountries.xhtml</to-view-id>
</navigation-case>
</navigation-rule>
<navigation-rule>
<from-view-id>/addCountry.xhtml</from-view-id>
<navigation-case>
<from-outcome>success</from-outcome>
<to-view-id>/listCountries.xhtml</to-view-id>
</navigation-case>
</navigation-rule>
<navigation-rule>
......
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<body>
<h:form>
<h:panelGrid columns="2">
<h:outputText value='Nom du pays'/>
<h:inputText value='#{countryBean.countryName}'/>
<h:outputText value='Add'/>
<h:commandButton action="#{bookBean.add}" value="Add"/>
</h:panelGrid>
</h:form>
</body>
</html>
......@@ -18,6 +18,18 @@
<h:outputText value="List books"/>
</h:commandLink>
</li>
<li>
<h:commandLink action="addCountry">
<h:outputText value="Add country"/>
</h:commandLink>
</li>
<!--
<li>
<h:commandLink action="listCountries">
<h:outputText value="List countries"/>
</h:commandLink>
</li>
-->
</ul>
</h:form>
</body>
......
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<body>
<h:outputLabel value="Hello, world"/>
</body>
</html>