jsf-spring-integration - minor refactors

This commit is contained in:
Slavisa Baeldung 2016-05-26 14:29:19 +02:00
parent c76666a9b3
commit 193adf82bc
3 changed files with 27 additions and 38 deletions

View File

@ -10,28 +10,21 @@ import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext; import javax.faces.context.FacesContext;
import java.io.Serializable; import java.io.Serializable;
@ManagedBean(name = "registration") @ManagedBean(name = "registration")
@ViewScoped @ViewScoped
public class RegistrationBean implements Serializable { public class RegistrationBean implements Serializable {
private static final Logger LOGGER = LoggerFactory.getLogger(RegistrationBean.class); private static final Logger LOGGER = LoggerFactory.getLogger(RegistrationBean.class);
@ManagedProperty(value = "#{userManagementDAO}") @ManagedProperty(value = "#{userManagementDAO}")
transient private IUserManagementDAO theUserDao; transient private IUserManagementDAO userDao;
private String userName; private String userName;
private String operationMessage; private String operationMessage;
/** public void createNewUser() {
* Creates a new instance of RegistrationBean
*/
public RegistrationBean() {
}
public String createNewUser() {
try { try {
LOGGER.info("Creating new user"); LOGGER.info("Creating new user");
FacesContext context = FacesContext.getCurrentInstance(); FacesContext context = FacesContext.getCurrentInstance();
boolean operationStatus = theUserDao.createUser(userName); boolean operationStatus = userDao.createUser(userName);
context.isValidationFailed(); context.isValidationFailed();
if (operationStatus) { if (operationStatus) {
operationMessage = "User " + userName + " created"; operationMessage = "User " + userName + " created";
@ -39,8 +32,8 @@ public class RegistrationBean implements Serializable {
} catch (Exception ex) { } catch (Exception ex) {
LOGGER.error("Error registering new user "); LOGGER.error("Error registering new user ");
ex.printStackTrace(); ex.printStackTrace();
operationMessage = "Error " + userName + " not created";
} }
return null;
} }
public String getUserName() { public String getUserName() {
@ -51,15 +44,12 @@ public class RegistrationBean implements Serializable {
this.userName = userName; this.userName = userName;
} }
/** public void setUserDao(IUserManagementDAO userDao) {
* @param theUserDao the theUserDao to set this.userDao = userDao;
*/
public void setTheUserDao(IUserManagementDAO theUserDao) {
this.theUserDao = theUserDao;
} }
public IUserManagementDAO getTheUserDao() { public IUserManagementDAO getUserDao() {
return this.theUserDao; return this.userDao;
} }
public String getOperationMessage() { public String getOperationMessage() {

View File

@ -9,12 +9,8 @@
<param-value>SERVER</param-value> <param-value>SERVER</param-value>
</context-param> </context-param>
<context-param> <context-param>
<param-name> <param-name>javax.faces.FACELETS_SKIP_COMMENTS</param-name>
primefaces.PUBLIC_CAPTCHA_KEY <param-value>true</param-value>
</param-name>
<param-value>
6LdG-84SAAAAALQfp6DuJqd1XLnz3ZlHfhunPPjY
</param-value>
</context-param> </context-param>
<servlet> <servlet>
<servlet-name>Faces Servlet</servlet-name> <servlet-name>Faces Servlet</servlet-name>

View File

@ -2,23 +2,26 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" <html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html" xmlns:h="http://java.sun.com/jsf/html">
xmlns:f="http://java.sun.com/jsf/core">
<h:head> <h:head>
<title><h:outputText value="#{msg['message.welcome']}"/></title> <title><h:outputText value="#{msg['message.welcome']}"/></title>
</h:head> </h:head>
<h:body> <h:body>
<h:form> <h:form>
<f:ajax render="theGrid">
<h:panelGrid id="theGrid" columns="3"> <h:panelGrid id="theGrid" columns="3">
<h:outputText value="Username"/> <h:outputText value="Username"/>
<h:inputText id="firstName" required="true" requiredMessage="#{msg['message.valueRequired']}" <h:inputText id="firstName" binding="#{userName}" required="true" requiredMessage="#{msg['message.valueRequired']}"
value="#{registration.userName}"/> value="#{registration.userName}"/>
<h:message for="firstName"/> <h:message for="firstName" style="color:red;"/>
<h:commandButton value="#{msg['label.saveButton']}" action="#{registration.createNewUser}"/> <h:commandButton value="#{msg['label.saveButton']}" action="#{registration.createNewUser}"
<h:outputText value="#{registration.operationMessage}"/> process="@this"/>
<!--
Accessing the Spring bean directly from the page
<h:commandButton value="Save"
action="#{registration.userDao.createUser(userName.value)}"/>
-->
<h:outputText value="#{registration.operationMessage}" style="color:green;"/>
</h:panelGrid> </h:panelGrid>
</f:ajax>
</h:form> </h:form>
</h:body> </h:body>
</html> </html>