1 /* 2 * JBoss, Home of Professional Open Source 3 * Copyright 2014, Red Hat, Inc. and/or its affiliates, and individual 4 * contributors by the @authors tag. See the copyright.txt in the 5 * distribution for a full listing of individual contributors. 6 * 7 * Licensed under the Apache License, Version 2.0 (the "License"); 8 * you may not use this file except in compliance with the License. 9 * You may obtain a copy of the License at 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 package org.jboss.as.quickstarts.ear.controller; 18 19 import javax.ejb.EJB; 20 import javax.enterprise.context.RequestScoped; 21 import javax.inject.Named; 22 23 import org.jboss.as.quickstarts.ear.client.GreeterEJBLocal; 24 import org.jboss.as.quickstarts.ear.client.GreeterException; 25 26 27 /** 28 * @author bmaxwell 29 * 30 * @Named defaults to the name of the class with the first letter lower case so this bean can be referred to as greeterBean in a JSF page 31 * It is RequestScoped so nothing is maintained beyond the request/response. 32 */ 33 @Named 34 @RequestScoped 35 public class GreeterBean { 36 37 /* Inject the Local interface of the GreeterEJB so we can invoke sayHello */ 38 private @EJB GreeterEJBLocal greeterEJB; 39 40 /* An instance variable to hold the data bound in the inputText of the JSF page */ 41 private String name; 42 43 /* And instance variable to hold the EJB response or exception message that is bound to the outputText on the JSF page */ 44 private String response; 45 46 /** 47 * 48 */ 49 public GreeterBean() { 50 } 51 52 /** 53 * The name getter to retrieve the name value which will be displayed in the inputText 54 * @return the name to be displayed in the inputText on the JSF page 55 */ 56 public String getName() { 57 return name; 58 } 59 60 /** 61 * The name setter will be called when the user submits the JSF page and pass the value which can be usined in the sayHello action 62 * @param name the string which is bound to the JSF page inputText 63 */ 64 public void setName(String name) { 65 this.name = name; 66 } 67 68 /** 69 * The response displayed on the JSF page is an outputText, so we only need a getter 70 * @return the value of the response variable which is set after the EJB is invoked in the sayHello action 71 */ 72 public String getResponse() { 73 return response; 74 } 75 76 /** 77 * This is a action which the JSF page will invoke when the Say Hello commandButton is clicked 78 * @return a string which JSF uses for page navigation, an empty string means reload the original page 79 */ 80 public String sayHello() { 81 82 try { 83 response = greeterEJB.sayHello(name); 84 } 85 catch(GreeterException e) { 86 response = "Error: " + e.getMessage(); 87 } 88 return ""; 89 } 90 91 92 }