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