Jean-Francois Leveque

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

...@@ -8,5 +8,5 @@ public interface CountryService { ...@@ -8,5 +8,5 @@ public interface CountryService {
8 8
9 void addCountry(Country country); 9 void addCountry(Country country);
10 10
11 - public List<Country> getAllCountries(); 11 + List<Country> getAllCountries();
12 } 12 }
......
1 +package org.legrog.application;
2 +
3 +import org.legrog.entities.User;
4 +
5 +import java.util.List;
6 +
7 +public interface UserService {
8 + void addUser(User user);
9 +
10 + List<User> getAllUsers();
11 +}
1 +package org.legrog.application;
2 +
3 +import org.legrog.entities.User;
4 +import org.legrog.entities.UserRepository;
5 +
6 +import javax.ejb.Stateless;
7 +import javax.inject.Inject;
8 +import java.util.List;
9 +
10 +@Stateless
11 +public class UserServiceSpring implements UserService {
12 + @Inject
13 + UserRepository repository;
14 +
15 + public void addUser(User user) {
16 + repository.save(user);
17 + }
18 +
19 + public List<User> getAllUsers() {
20 + return repository.findAll();
21 + }
22 +}
1 +package org.legrog.entities;
2 +
3 +import org.springframework.data.jpa.repository.JpaRepository;
4 +
5 +public interface UserRepository extends JpaRepository<User, Integer> {
6 +}
...@@ -14,6 +14,10 @@ public class CountryBean { ...@@ -14,6 +14,10 @@ public class CountryBean {
14 @Inject 14 @Inject
15 private CountryService service; 15 private CountryService service;
16 16
17 + private List<Country> countries;
18 +
19 + private String countryName;
20 +
17 public List<Country> getCountries() { 21 public List<Country> getCountries() {
18 return countries; 22 return countries;
19 } 23 }
...@@ -22,10 +26,6 @@ public class CountryBean { ...@@ -22,10 +26,6 @@ public class CountryBean {
22 this.countries = countries; 26 this.countries = countries;
23 } 27 }
24 28
25 - private List<Country> countries;
26 -
27 - private String countryName;
28 -
29 public String getCountryName() { 29 public String getCountryName() {
30 return countryName; 30 return countryName;
31 } 31 }
......
1 +package org.legrog.presentation;
2 +
3 +import org.legrog.application.UserService;
4 +import org.legrog.entities.User;
5 +
6 +import javax.enterprise.context.RequestScoped;
7 +import javax.inject.Inject;
8 +import javax.inject.Named;
9 +import java.util.List;
10 +
11 +@Named
12 +@RequestScoped
13 +public class UserBean {
14 + @Inject
15 + private UserService service;
16 +
17 + private String username;
18 +
19 + private List<User> users;
20 +
21 + public List<User> getUsers() {
22 + return users;
23 + }
24 +
25 + public void setUsers(List<User> users) {
26 + this.users = users;
27 + }
28 +
29 + public String getUsername() {
30 + return username;
31 + }
32 +
33 + public void setUsername(String username) {
34 + this.username = username;
35 + }
36 +
37 + public String add()
38 + {
39 + User user = new User();
40 + user.setUsername(username);
41 + service.addUser(user);
42 + return "success";
43 + }
44 +
45 + public void onload() {
46 + users = service.getAllUsers();
47 + }
48 +
49 +}
...@@ -6,6 +6,14 @@ ...@@ -6,6 +6,14 @@
6 version="2.0"> 6 version="2.0">
7 7
8 <navigation-rule> 8 <navigation-rule>
9 + <from-view-id>*</from-view-id>
10 + <navigation-case>
11 + <from-outcome>home</from-outcome>
12 + <to-view-id>/index.xhtml</to-view-id>
13 + </navigation-case>
14 + </navigation-rule>
15 +
16 + <navigation-rule>
9 <from-view-id>/index.xhtml</from-view-id> 17 <from-view-id>/index.xhtml</from-view-id>
10 <navigation-case> 18 <navigation-case>
11 <from-outcome>addBook</from-outcome> 19 <from-outcome>addBook</from-outcome>
...@@ -23,6 +31,14 @@ ...@@ -23,6 +31,14 @@
23 <from-outcome>listCountries</from-outcome> 31 <from-outcome>listCountries</from-outcome>
24 <to-view-id>/listCountries.xhtml</to-view-id> 32 <to-view-id>/listCountries.xhtml</to-view-id>
25 </navigation-case> 33 </navigation-case>
34 + <navigation-case>
35 + <from-outcome>addUser</from-outcome>
36 + <to-view-id>/addUser.xhtml</to-view-id>
37 + </navigation-case>
38 + <navigation-case>
39 + <from-outcome>listUsers</from-outcome>
40 + <to-view-id>/listUsers.xhtml</to-view-id>
41 + </navigation-case>
26 </navigation-rule> 42 </navigation-rule>
27 43
28 <navigation-rule> 44 <navigation-rule>
...@@ -34,6 +50,14 @@ ...@@ -34,6 +50,14 @@
34 </navigation-rule> 50 </navigation-rule>
35 51
36 <navigation-rule> 52 <navigation-rule>
53 + <from-view-id>/addUser.xhtml</from-view-id>
54 + <navigation-case>
55 + <from-outcome>success</from-outcome>
56 + <to-view-id>/listUsers.xhtml</to-view-id>
57 + </navigation-case>
58 + </navigation-rule>
59 +
60 + <navigation-rule>
37 <from-view-id>/book.xhtml</from-view-id> 61 <from-view-id>/book.xhtml</from-view-id>
38 <navigation-case> 62 <navigation-case>
39 <from-outcome>success</from-outcome> 63 <from-outcome>success</from-outcome>
......
...@@ -7,6 +7,10 @@ ...@@ -7,6 +7,10 @@
7 xmlns:f="http://xmlns.jcp.org/jsf/core"> 7 xmlns:f="http://xmlns.jcp.org/jsf/core">
8 <body> 8 <body>
9 <h:form> 9 <h:form>
10 + <h:commandLink action="home">
11 + <h:outputText value="Menu principal"/>
12 + </h:commandLink>
13 +
10 <h:panelGrid columns="2"> 14 <h:panelGrid columns="2">
11 <h:outputText value='Nom du pays'/> 15 <h:outputText value='Nom du pays'/>
12 <h:inputText value='#{countryBean.countryName}'/> 16 <h:inputText value='#{countryBean.countryName}'/>
......
1 +<?xml version="1.0" encoding="UTF-8"?>
2 +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
3 + "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4 +<html xmlns="http://www.w3.org/1999/xhtml"
5 + xmlns:h="http://xmlns.jcp.org/jsf/html"
6 + xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
7 + xmlns:f="http://xmlns.jcp.org/jsf/core">
8 +<body>
9 +<h:form>
10 + <h:commandLink action="home">
11 + <h:outputText value="Menu principal"/>
12 + </h:commandLink>
13 +
14 + <h:panelGrid columns="2">
15 + <h:outputText value="Username de l'utilisateur"/>
16 + <h:inputText value='#{userBean.username}'/>
17 + <h:outputText value='Add'/>
18 + <h:commandButton action="#{userBean.add}" value="Add"/>
19 + </h:panelGrid>
20 +</h:form>
21 +</body>
22 +</html>
...@@ -28,6 +28,16 @@ ...@@ -28,6 +28,16 @@
28 <h:outputText value="List countries"/> 28 <h:outputText value="List countries"/>
29 </h:commandLink> 29 </h:commandLink>
30 </li> 30 </li>
31 + <li>
32 + <h:commandLink action="addUser">
33 + <h:outputText value="Add user"/>
34 + </h:commandLink>
35 + </li>
36 + <li>
37 + <h:commandLink action="listUsers">
38 + <h:outputText value="List users"/>
39 + </h:commandLink>
40 + </li>
31 </ul> 41 </ul>
32 </h:form> 42 </h:form>
33 </body> 43 </body>
......
...@@ -6,6 +6,11 @@ ...@@ -6,6 +6,11 @@
6 xmlns:ui="http://xmlns.jcp.org/jsf/facelets" 6 xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
7 xmlns:f="http://xmlns.jcp.org/jsf/core"> 7 xmlns:f="http://xmlns.jcp.org/jsf/core">
8 <body> 8 <body>
9 +<h:form>
10 +<h:commandLink action="home">
11 + <h:outputText value="Menu principal"/>
12 +</h:commandLink>
13 +</h:form>
9 <f:metadata> 14 <f:metadata>
10 <f:event type="preRenderView" listener="#{countryBean.onload}" /> 15 <f:event type="preRenderView" listener="#{countryBean.onload}" />
11 </f:metadata> 16 </f:metadata>
...@@ -14,7 +19,5 @@ ...@@ -14,7 +19,5 @@
14 <li>#{country.countryName}</li> 19 <li>#{country.countryName}</li>
15 </ui:repeat> 20 </ui:repeat>
16 </ul> 21 </ul>
17 -
18 -<h:outputLabel value="Hello, world"/>
19 </body> 22 </body>
20 </html> 23 </html>
......
1 +<?xml version="1.0" encoding="UTF-8"?>
2 +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
3 + "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4 +<html xmlns="http://www.w3.org/1999/xhtml"
5 + xmlns:h="http://xmlns.jcp.org/jsf/html"
6 + xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
7 + xmlns:f="http://xmlns.jcp.org/jsf/core">
8 +<f:view>
9 + <h:form>
10 + <h:commandLink action="home">
11 + <h:outputText value="Menu principal"/>
12 + </h:commandLink>
13 + </h:form>
14 + <f:metadata>
15 + <f:event type="preRenderView" listener="#{userBean.onload}" />
16 + </f:metadata>
17 + <ul>
18 + <ui:repeat value="#{userBean.users}" var="user">
19 + <li>#{user.username}</li>
20 + </ui:repeat>
21 + </ul>
22 +</f:view>
23 +</html>