0

I know it is propably not the best practice to make this work without a managed bean... but I'd like to make it work that way :)

<h:selectOneMenu id="SelectMenu}">        
<f:selectItem itemValue="1" itemLabel="A"/>        
<f:selectItem itemValue="2" itemLabel="B"/>        
<f:selectItem itemValue="3" itemLabel="C"/>  
</h:selectOneMenu>

<h:commandLink value="click" action='#{someController.action(SelectMenu.itemValue)}' />

I guess ajax could be helpfull, but I never used that.

Many thanks for your help

Niy
  • 1
  • 2
    Possible duplicate http://stackoverflow.com/questions/22535174/submit-value-from-form-to-method-without-variable-in-the-backingbean – Omar Mar 21 '14 at 15:55

1 Answers1

1

Bind your <h:selectOneMenu /> value to the view directly:

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets">

<h:form>
    <h:selectOneMenu value="#{selected}">
        <f:selectItem itemValue="1" itemLabel="A" />
        <f:selectItem itemValue="2" itemLabel="B" />
        <f:selectItem itemValue="3" itemLabel="C" />
    </h:selectOneMenu>

    <h:commandLink value="click" action='#{myBean.action(selected)}' />
</h:form>

</html>
@ManagedBean
@RequestScoped
public class MyBean {

    public void action(String selectedValue) {
        System.out.println("Selected " + selectedValue);
    }

}
Aritz
  • 30,971
  • 16
  • 136
  • 217
  • That's the way with the bean :-/ I need one without a bean. Ajax, Richfaces and webflow are allowed, but I have no experience with them. – Niy Mar 24 '14 at 13:30
  • Are you using Spring MVC as your view layer framework? Don't make a mix of everything, it'll turn things more complicated. Richfaces is a JSF component library, using it with other framework is just an overkill. – Aritz Mar 24 '14 at 13:33
  • Not Spring MVC but Spring Webflow (SWF), Spring MVC and JSF are not a good combination, but SWF and JSF go well together... at least that's what I was told. RichFaces is for better Ajax – Niy Mar 24 '14 at 14:13
  • I wouldn't suggest you using SWF as JSF has its own navigation management. You could consider it if it's a legacy project, if otherwise you're building a new one, discard it. JSF manages components, view states and navigation and it's a complete MVC framework. Apart from that, if not using a bean, what you would like to use to handle your POST request here? – Aritz Mar 24 '14 at 14:16
  • Sadly it's a legacy project with way to many pages to switch. I'd like to have it work like: http://stackoverflow.com/questions/22535174/submit-value-from-form-to-method-without-variable-in-the-backingbean but I can't get it to work with selectonemenu :( – Niy Mar 24 '14 at 14:42
  • The given example does exactly what I did in my answer, it uses the bean for the POST method. The only thing the OP wants to avoid is having to declare the `name` and `password` properties in the bean, which is done by binding them to the view, the way I do with `#{selected}`. The good thing of JSF is the possibility to implement your own beans in order to manage the requests. It's what is done in Spring MVC with controllers. Otherwise, you would need to go with a traditional homegrown Servlet. – Aritz Mar 24 '14 at 14:47
  • Hmm.. ok, then the problem is at another point, no other file in the project uses "@ManagedBean" or sets the scope in a Bean. They use the controller-way, but I think the controllers are way to heavy for that small element I need... I was hoping there was a way around it with the use of some local variable or stuff :-) – Niy Mar 24 '14 at 15:04
  • Then are you sure Spring MVC is not used? Controllers are Spring MVC specific. Also, using annotations as `@ManagedBean` or `@RequestScoped` is not mandatory in JSF. You also can define the managed beans in faces-config.xml. – Aritz Mar 24 '14 at 15:38
  • The faces-config.xml is almost empty, there are ressources for messages configured, is there a third way? Our wiki links to SWf, so I guess we are using SWF... but I'm not sure. – Niy Mar 24 '14 at 16:12
  • There isn't a third way. Check there are not additional faces-config.xml files in classpath. – Aritz Mar 24 '14 at 16:19
  • No additional faces-configs, but I found a webflow.xml so I guess we are really using SWF and perhaps since it is build on MVC I guess your guess was right that Spring MVC is used too... Hopefully that pointer helps me to find the right man-pages :-) – Niy Mar 24 '14 at 16:32
  • Then you should either mark the thread as answered or remove your question, as you even don't know how your app is configured. – Aritz Mar 26 '14 at 14:26