Jean-Francois Leveque

Ajout d'une entité et archi liée ainsi que de navigation.

......@@ -8,5 +8,5 @@ public interface CountryService {
void addCountry(Country country);
public List<Country> getAllCountries();
List<Country> getAllCountries();
}
......
package org.legrog.application;
import org.legrog.entities.User;
import java.util.List;
public interface UserService {
void addUser(User user);
List<User> getAllUsers();
}
package org.legrog.application;
import org.legrog.entities.User;
import org.legrog.entities.UserRepository;
import javax.ejb.Stateless;
import javax.inject.Inject;
import java.util.List;
@Stateless
public class UserServiceSpring implements UserService {
@Inject
UserRepository repository;
public void addUser(User user) {
repository.save(user);
}
public List<User> getAllUsers() {
return repository.findAll();
}
}
package org.legrog.entities;
import org.springframework.data.jpa.repository.JpaRepository;
public interface UserRepository extends JpaRepository<User, Integer> {
}
......@@ -14,6 +14,10 @@ public class CountryBean {
@Inject
private CountryService service;
private List<Country> countries;
private String countryName;
public List<Country> getCountries() {
return countries;
}
......@@ -22,10 +26,6 @@ public class CountryBean {
this.countries = countries;
}
private List<Country> countries;
private String countryName;
public String getCountryName() {
return countryName;
}
......
package org.legrog.presentation;
import org.legrog.application.UserService;
import org.legrog.entities.User;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.inject.Named;
import java.util.List;
@Named
@RequestScoped
public class UserBean {
@Inject
private UserService service;
private String username;
private List<User> users;
public List<User> getUsers() {
return users;
}
public void setUsers(List<User> users) {
this.users = users;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String add()
{
User user = new User();
user.setUsername(username);
service.addUser(user);
return "success";
}
public void onload() {
users = service.getAllUsers();
}
}
......@@ -6,6 +6,14 @@
version="2.0">
<navigation-rule>
<from-view-id>*</from-view-id>
<navigation-case>
<from-outcome>home</from-outcome>
<to-view-id>/index.xhtml</to-view-id>
</navigation-case>
</navigation-rule>
<navigation-rule>
<from-view-id>/index.xhtml</from-view-id>
<navigation-case>
<from-outcome>addBook</from-outcome>
......@@ -23,6 +31,14 @@
<from-outcome>listCountries</from-outcome>
<to-view-id>/listCountries.xhtml</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome>addUser</from-outcome>
<to-view-id>/addUser.xhtml</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome>listUsers</from-outcome>
<to-view-id>/listUsers.xhtml</to-view-id>
</navigation-case>
</navigation-rule>
<navigation-rule>
......@@ -34,6 +50,14 @@
</navigation-rule>
<navigation-rule>
<from-view-id>/addUser.xhtml</from-view-id>
<navigation-case>
<from-outcome>success</from-outcome>
<to-view-id>/listUsers.xhtml</to-view-id>
</navigation-case>
</navigation-rule>
<navigation-rule>
<from-view-id>/book.xhtml</from-view-id>
<navigation-case>
<from-outcome>success</from-outcome>
......
......@@ -7,6 +7,10 @@
xmlns:f="http://xmlns.jcp.org/jsf/core">
<body>
<h:form>
<h:commandLink action="home">
<h:outputText value="Menu principal"/>
</h:commandLink>
<h:panelGrid columns="2">
<h:outputText value='Nom du pays'/>
<h:inputText value='#{countryBean.countryName}'/>
......
<?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:commandLink action="home">
<h:outputText value="Menu principal"/>
</h:commandLink>
<h:panelGrid columns="2">
<h:outputText value="Username de l'utilisateur"/>
<h:inputText value='#{userBean.username}'/>
<h:outputText value='Add'/>
<h:commandButton action="#{userBean.add}" value="Add"/>
</h:panelGrid>
</h:form>
</body>
</html>
......@@ -28,6 +28,16 @@
<h:outputText value="List countries"/>
</h:commandLink>
</li>
<li>
<h:commandLink action="addUser">
<h:outputText value="Add user"/>
</h:commandLink>
</li>
<li>
<h:commandLink action="listUsers">
<h:outputText value="List users"/>
</h:commandLink>
</li>
</ul>
</h:form>
</body>
......
......@@ -6,6 +6,11 @@
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<body>
<h:form>
<h:commandLink action="home">
<h:outputText value="Menu principal"/>
</h:commandLink>
</h:form>
<f:metadata>
<f:event type="preRenderView" listener="#{countryBean.onload}" />
</f:metadata>
......@@ -14,7 +19,5 @@
<li>#{country.countryName}</li>
</ui:repeat>
</ul>
<h:outputLabel value="Hello, world"/>
</body>
</html>
......
<?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">
<f:view>
<h:form>
<h:commandLink action="home">
<h:outputText value="Menu principal"/>
</h:commandLink>
</h:form>
<f:metadata>
<f:event type="preRenderView" listener="#{userBean.onload}" />
</f:metadata>
<ul>
<ui:repeat value="#{userBean.users}" var="user">
<li>#{user.username}</li>
</ui:repeat>
</ul>
</f:view>
</html>