Ravi Srinivasan
2018-09-11 b9209f6413cb6985c0089f9677f4db9b52ca1395
commit | author | age
b9209f 1 package com.redhat.training.ui;
RS 2
3 import javax.enterprise.context.RequestScoped;
4 import javax.faces.application.FacesMessage;
5 import javax.faces.context.FacesContext;
6 import javax.inject.Named;
7 import javax.validation.ConstraintViolation;
8 import javax.validation.ConstraintViolationException;
9 import javax.inject.Inject;
10
11 import com.redhat.training.rest.PersonService;
12
13 @RequestScoped
14 @Named("hello")
15 public class Hello {
16     private String name;
17
18     @Inject
19     private PersonService personService;
20
21     public void sayHello() {
22         try {
23             String response = personService.hello(name);
24             FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(response));
25         }catch(Exception e){
26             System.out.println(e.getCause());
27             if(e.getCause() != null && e.getCause() instanceof ConstraintViolationException) {
28                 ConstraintViolationException ex = (ConstraintViolationException) e.getCause();
29                 String violations = "";
30                 for(ConstraintViolation<?> cv: ex.getConstraintViolations()) {
31                     
32                     violations += cv.getMessage() + "\n";
33                     
34                     System.out.println("Violations: "+violations);
35                 }
36                 FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(violations));
37             }
38             
39         }
40         
41     }
42
43     public String getName() {
44         return name;
45     }
46
47     public void setName(String name) {
48         this.name = name;
49     }
50
51 }