java primefaces 提交表单以保存

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13920899/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 14:27:17  来源:igfitidea点击:

primefaces submit a form to save

javaxhtmlprimefaces

提问by Ali Yucel Akgul

I have code snippet as follows:

我有如下代码片段:

<h:form>
                <p:panel id="projectLabel" header="Yeni Proje" style="margin-bottom:10px;">
                    <p:messages id="messages" />

                    <h:panelGrid columns="2">

                        <h:outputLabel for="projectname" value="Proje Ad? :" />
                        <p:inputText id="projectname" value="" label="ProjectName">
                            <f:validateLength minimum="2" />
                        </p:inputText>
                        <p:message for="projectname" />
                        <h:outputText value="" />

                        <h:outputLabel for="sector" value="Sekt?r :" />
                        <p:inputText id="sector" value=""  label="Sector"/>
                        <p:message for="sector" />
                        <h:outputText value="" />


                        <center>
                            <p:commandButton ajax="false" value="Kaydet" icon="ui-icon-disk" action="#{createProject.create()}" />
                        </center>
                    </h:panelGrid>
                </p:panel>
            </h:form>

What I would like to do is, when user clicks on save button, all entries of form will be triggered to a java class to save the database. How can I do that?

我想做的是,当用户点击保存按钮时,表单的所有条目都会被触发到一个java类来保存数据库。我怎样才能做到这一点?

回答by Kerem Can Kurtay

This is backed-bean

这是支持豆

@ManagedBean(name="saveData")
@ViewScoped
public class SaveData{
    public SaveData(){}
    private String projectName;
    private String sector;
    //getters setters
    public void save(){
        // you have your data here
    }
}

And this is the html

这是 html

<p:panel id="projectLabel" header="Yeni Proje" style="margin-bottom:10px;">
                <p:messages id="messages" />

                <h:panelGrid columns="2">

                    <h:outputLabel for="projectname" value="Proje Ad? :" />
                    <p:inputText id="projectname" value="#{saveData.projectName}" label="ProjectName">
                        <f:validateLength minimum="2" />
                    </p:inputText>
                    <p:message for="projectname" />
                    <h:outputText value="" />

                    <h:outputLabel for="sector" value="Sekt?r :" />
                    <p:inputText id="sector" value="#{saveData.projectName}"  label="Sector"/>
                    <p:message for="sector" />
                    <h:outputText value="" />


                    <center>
                        <p:commandButton ajax="false" value="Kaydet" icon="ui-icon-disk" action="#{saveData.save}" />
                    </center>
                </h:panelGrid>
            </p:panel>
        </h:form>