【发布时间】:2012-01-20 06:35:16
【问题描述】:
我是 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>
现在我的注册和登录 bean 包含 getter 和 setter
登录豆
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 字段设置 convertDate 属性??
我使用了@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 然后调用注册和登录控制器
谢谢:)
【问题讨论】:
-
你是否提供了
的方法? -
我在问我是否应该说 action="#{setupController.create}" 或者是否有其他方法。
标签: java jsf jakarta-ee jpa