我是jsf jpa rest的新手。我想知道如何从控制器bean中设置bean的值。
假设一旦用户注册我想要将值插入数据库以及我在controllerbean中生成的日期。现在我如何在POJO中设置它
register.xhtml
<h:form id="registerForm">
<table>
<tr>
<td colspan="3" id="register">
<h2 style="border-bottom: 1px solid #CCCCCC;">Customer
Information</h2>
<tr>
<td class="label">
<h:outputLabel for="name" value="Name:"></h:outputLabel>
</td>
<td class="field">
<h:inputText id="name" size="20" required="true" requiredMessage="Please enter your name" value="#{registerController.selected.contactName}">
<f:validateLength maximum="50"></f:validateLength>
</h:inputText>
</td>
</tr>
<tr>
<td class="label">
<h:outputLabel for="company" value="Company:"></h:outputLabel>
</td>
<td class="field">
<h:inputText id="company" size="20" required="true" requiredMessage="Please enter your company" value="#{registerController.selected.company}">
<f:validateLength maximum="50"></f:validateLength>
</h:inputText>
</td>
</tr>
<tr>
<td class="label">
<h:outputLabel for="designation" value="Designation:"></h:outputLabel>
</td>
<td class="field">
<h:inputText id="designation" size="20" required="true" requiredMessage="Please enter your designation" value="#{registerController.selected.designation}">
<f:validateLength maximum="50"></f:validateLength>
</h:inputText>
</td>
</tr>
<tr>
<td class="label">
<h:outputLabel for="email" value="Email Id:"></h:outputLabel>
</td>
<td class="field">
<h:inputText id="email" size="20" required="true" requiredMessage="Please enter your email,eg:yourid@domain.com" value="#{registerController.selected.primaryEmailId}" title="eg:www.domain.com OR http://127.0.0.1/domain">
<f:validateLength maximum="70"></f:validateLength>
</h:inputText>
</td>
</tr>
<tr>
<td class="label">
<h:outputLabel for="phone" value="Contact No:"></h:outputLabel>
</td>
<td class="field">
<h:inputText id="phone" size="20" required="true" requiredMessage="Please enter your contact number" value="#{registerController.selected.contactNo}" title="eg:+919999999999">
<f:validateLength minimum="13" maximum="25"></f:validateLength>
</h:inputText>
</td>
</tr>
</td>
</tr>
<tr>
<td colspan="4" id="loginInfo">
<h2 style="border-bottom: 1px solid #CCCCCC;">Login Information</h2>
<tr>
<td class="label">
<h:outputLabel for="email" value="Login Email:"></h:outputLabel>
</td>
<td class="field">
<h:inputText id="email" size="20" maxlength="70" required="true" requiredMessage="Please enter your login email id,eg:yourid@domain.com" value="#{loginController.selected.clientId}">
<f:validateLength maximum="70"></f:validateLength>
</h:inputText>
</td>
</tr>
<tr>
<td class="label">
<h:outputLabel for="password" value="Password:"></h:outputLabel>
</td>
<td class="field">
<h:inputSecret id="password" size="20" maxlength="70" required="true" requiredMessage="Please enter your desired password(minimun 6 characters)" value="#{loginController.selected.password}">
<f:validateLength minimum="6" maximum="50"></f:validateLength>
</h:inputSecret>
</td>
</tr>
<tr>
<td class="label">
<h:outputLabel for="confirm_password" value="Confirm Password:"></h:outputLabel>
</td>
<td class="field">
<h:inputSecret id="confirm_password" size="20" required="true" requiredMessage="Please confirm your password">
<f:validateLength minimum="6" maximum="50"></f:validateLength>
</h:inputSecret>
</td>
</tr>
</td>
<tr>
<td>
<h:commandButton value="Sign Up" action="">
</h:commandButton>
</td>
</tr>
</tr>
</table>
现在我的Register和login bean包含getter和setter
登录bean
public String getLoginEmail() {
return loginEMail;
}
public void setLoginEmail(String loginEMail) {
this.loginEMail = loginEMail;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
注册bean
public String getContactName() {
return contactName;
}
public void setContactName(String ContactName) {
this.contactName = contactName;
}
public String getCompany() {
System.out.println("get....." + company);
return company;
}
public void setCompany(String company) {
this.company = company;
System.out.println("set....." + company);
}
public String getDesignation() {
System.out.println("get....." + designation);
return designation;
}
public void setDesignation(String designation) {
this.designation = designation;
System.out.println("set....." + primaryDesignation);
}
public String getContactNo() {
return contactNo;
}
public void setContactNo(String contactNo) {
this.contactNo = contactNo;;
}
public String getEmailId() {
return emailId;
}
public void setEmailId(String emailId) {
this.emailId = emailId;
}
public String getActiveFlag() {
return activeFlag;
}
public void setActiveFlag(String activeFlag) {
this.activeFlag = activeFlag;
}
public String getCreatedBy() {
System.out.println("get create name....." + createdBy);
createdBy=getPrimaryContactName();
return createdBy;
}
public void setCreatedBy(String primaryContactName) {
this.createdBy = primaryContactName;
}
public Date getCreatedOn() {
return createdOn;
}
public void setCreatedOn(Date createdOn) {
this.createdOn = createdOn;
System.out.println("set....." + createdOn);
}
public String getLastUpdatedBy() {
return lastUpdatedBy;
}
public void setLastUpdatedBy(String primaryContactName) {
this.lastUpdatedBy = primaryContactName;
}
public Date getLastUpdatedOn() {
return lastUpdatedOn;
}
public void setLastUpdatedOn(Date lastUpdatedOn) {
this.lastUpdatedOn = lastUpdatedOn;
}
我有两个控制器registerController和loginController,我有业务逻辑。
@ManagedBean(name = "registerController")
@SessionScoped
public String prepareCreate() {
System.out.println("Inside create");
current = new Register();
System.out.println("Inside create current");
selectedItemIndex = -1;
return "Signup Success";
}
public String create() {
try {
System.out.println("I am inside create");
getFacade().create(current);
JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("RegisterCreated"));
return prepareCreate();
} catch (Exception e) {
System.out.println("I am here inside create exception");
e.printStackTrace();
JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
return null;
}
}
public Date generateCurrentTime()
{
// get current system time yyyy/MMM/dd HH:mm:ss
Calendar currentDate = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String dateNow = formatter.format(currentDate.getTime());
Date convertedDate=null;
try
{
convertedDate=formatter.parse(dateNow);
}
catch(Exception e)
{
e.printStackTrace();
}
System.out.println("date now is:"+dateNow);
return convertedDate;
}
类似地,loginController也包含相同的代码。 如何在pojo中为createdOn和lastUpdatedOn feild设置convertedDate属性?
我使用了@ManagedBeanProperty但它设置了值...
@ManagedBeanProperty(value=#{registerController.getgenerateCurrentTime})
@Column(name = "created_on")
@Temporal(TemporalType.DATE)
private Date createdOn;
@ManagedBeanProperty(value=#{registerController.getgenerateCurrentTime})
@Column(name = "created_on")
@Temporal(TemporalType.DATE)
private Date lastUpdatedOn;
请指出我的错误。我使用了@sessionscoped
另外你可以看到我在一个表单中有两个表并且它们相互依赖。当用户点击注册按钮时如何将它们插入数据库...我应该在按钮上最初传递控件点击控制器说setupController然后调用寄存器和登录控制器
感谢:)
答案 0 :(得分:0)
<h:commandButton value="Sign Up" action="">
提交表单,导致在后面的bean中存储字段的值(如果没有使用ajax)
并在action="#{setupController.create}"
之类的命令按钮中提供操作将导致在将字段分配给bean之后对表单的提交调用该方法create
。
您也可以在表单
中提供它答案 1 :(得分:0)
正如其他朋友所说,你需要调用一个动作来设置支持bean上的值...你可以创建一个由你的按钮动作调用的方法,并且在那个方法中你可以调用你的服务来存储你的数据库中的数据。欢呼声