You absolutely can, and it's a much better way of taking input for this kind of thing than using free text.
You could list the values in the page itself, but it's a better idea to use the Apex controller to provide the options, as then you'll be defining them in the same piece of code that's checking them later on, as opposed to you having to maintain a list of values in both the page and the controller.
In your controller you just need a method that provides a list of SelectOption
instances:
public List<SelectOption> getOperations()
{
List<SelectOption> options = new List<SelectOption>();
options.add(new SelectOption('1' 'Edit Users'));
options.add(new SelectOption('2','Delete Users'));
options.add(new SelectOption('3','Buy Beers for Users'));
return options;
}
And then in the page you can write to a property on the controller as before:
<apex:selectList value="{!userinput}" title="Choose a Task">
<apex:selectOptions value="{!Operations}"/>
</apex:selectList>
You'll probably want to check out the documentation for <apex:selectList>
and you should probably consider making the values constant strings in your class to make maintenance easier and improve code readability. When you're checking the value of the userInput
variable (which could be better named) this:
if(userInput == '1')
is a bit less descriptive than:
if(userInput == OP_EDIT_USERS)