Peter Penzov
Peter Penzov

Reputation: 1716

JSF - update rows in database without form

I'm interested is it possible to create a h:selectOneMenu which can be used to update rows into database without using a form. Something like this:

                            <h:selectOneMenu value="#{ApplicationController.setting['SessionTTL']}" onclick="#{ApplicationController.Updatesetting(SessionTTL)}">
                                <f:selectItem itemValue="#{ApplicationController.setting['SessionTTL']}" itemLabel="#{ApplicationController.setting['SessionTTL']}" />
                                <f:selectItem itemValue="two" itemLabel="Option two" />
                                <f:selectItem itemValue="three" itemLabel="Option three" />
                                <f:selectItem itemValue="custom" itemLabel="Define custom value" />
                            </h:selectOneMenu>

In other words if the value in h:selectOneMenu is changed the attribute onclick calls java method which makes SQL query with the new selected value. Is there any example?

Best Wishes Peter

Upvotes: 1

Views: 300

Answers (1)

BalusC
BalusC

Reputation: 1109625

No, it's not possible to invoke bean action methods without a form. You definitely need a <h:form>. Instead of that onclick approach which will not work in any way (JS runs at client, not at server), you need the <f:ajax>. It has a listener attribute which allows you to specify a bean action method which is to be invoked on the ajax event.

<h:form>
    <h:selectOneMenu value="#{ApplicationController.setting['SessionTTL']}">
        <f:selectItem itemValue="#{ApplicationController.setting['SessionTTL']}" itemLabel="#{ApplicationController.setting['SessionTTL']}" />
        <f:selectItem itemValue="two" itemLabel="Option two" />
        <f:selectItem itemValue="three" itemLabel="Option three" />
        <f:selectItem itemValue="custom" itemLabel="Define custom value" />
        <f:ajax listener="#{ApplicationController.Updatesetting('SessionTTL')}" />
    </h:selectOneMenu>
</h:form>

(by the way, the uppercased method name is bad naming convention, I'd suggest to lowercase it; the same applies to managed bean name)

If you can, I'd suggest to take a step back and learn the essential basic web development principles such as HTTP, HTML, CSS and JavaScript. Then you need to understand properly that JSF is a component based MVC framework which autogenerates all that HTML/CSS/JS code and takes all the HTTP request/response handling into account.

Upvotes: 1

Related Questions