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

View File

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

View File

@ -2,24 +2,27 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
xmlns:h="http://java.sun.com/jsf/html">
<h:head>
<title><h:outputText value="#{msg['message.welcome']}"/></title>
</h:head>
<h:body>
<h:form>
<f:ajax render="theGrid">
<h:panelGrid id="theGrid" columns="3">
<h:outputText value="Username"/>
<h:inputText id="firstName" required="true" requiredMessage="#{msg['message.valueRequired']}"
value="#{registration.userName}"/>
<h:message for="firstName"/>
<h:commandButton value="#{msg['label.saveButton']}" action="#{registration.createNewUser}"/>
<h:outputText value="#{registration.operationMessage}"/>
</h:panelGrid>
</f:ajax>
</h:form>
<h:form>
<h:panelGrid id="theGrid" columns="3">
<h:outputText value="Username"/>
<h:inputText id="firstName" binding="#{userName}" required="true" requiredMessage="#{msg['message.valueRequired']}"
value="#{registration.userName}"/>
<h:message for="firstName" style="color:red;"/>
<h:commandButton value="#{msg['label.saveButton']}" action="#{registration.createNewUser}"
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:form>
</h:body>
</html>