【问题标题】:updating bean values from controller,multiple inserts on button click从控制器更新 bean 值,单击按钮时多次插入
【发布时间】: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


【解决方案1】:
<h:commandButton value="Sign Up" action="">

是否提交导致将字段值存储在后台 bean 中的表单(如果没有使用 ajax)

并在命令按钮中提供操作,如action="#{setupController.create}",将在将字段分配给 bean 后,在提交表单时调用该方法 create

您也可以以表单的形式提供它

参考How to define form action in JSF?

【讨论】:

  • 我使用 value="#{registerController.selected.contactName}" 将值存储在后 bean 中。既然我有两个控制器,我应该再创建一个控制器,然后将控件传递给它那个控制器我称之为 registerController 和 loginController 的 create 方法...
  • 对于我添加的每个用户,createdOn 字段存储了其逻辑在控制器中的日期。如何在我的后台 bean 中更新 this 的值??
  • 这些值仅在表单提交时存储在 backbean 中。不仅将其提供为 value="#{registerController.selected.contactName}" 或者您需要提供
  • 这意味着当用户首先注册时你想要两个操作你想调用 createdon 然后创建?
  • 我明白了......我很困惑的是如何通过单击一次按钮来调用两个控制器??
【解决方案2】:

正如其他朋友所说,您需要调用一个操作来设置支持 bean 上的值...您可以创建一个由按钮的操作调用的方法,并且在该方法中您可以调用您的服务来存储您的数据库中的数据。干杯

【讨论】:

    猜你喜欢
    • 2020-10-22
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多