Preheat the oven to 350°F.
Spray a 3 or 4-quart baking dish with cooking spray (the pan I use is 3-quart and measures roughly 12"x 8"x 2½").
In a medium saucepan (I use a 3-quart saucepan), melt 3 tablespoons of the butter over medium-low heat.
Once the butter has melted, add the flour and cook, stirring frequently, for one minute.
Slowly add the milk while whisking to combine. Season to taste with salt and pepper (don't forget to take into account how salty your ham is).
Increase the heat and bring to a boil, stirring frequently. Reduce the heat and simmer for a minute, stirring frequently, or until the sauce has thickened. Turn off the heat.
Place half of the sliced potatoes in an even layer in the baking dish. Top with half of the onions and then half of the ham.
Pour half the sauce over the ham and then sprinkle with half of the cheese.
Repeat the layers of potatoes, onions, ham, and sauce using the remaining half of those ingredients. Don't add the remaining cheese yet.
Cut the remaining 2 tablespoons of butter into 8 small cubes and dot the top of the casserole with them.
Cover the casserole tightly with foil. Bake for 45 minutes, then remove the foil and bake for another 30 minutes.
Sprinkle the remaining cheese on top of the casserole and bake for an additional 15 minutes or until the potatoes are tender.
Remove the casserole from the oven and let rest for 10 to 15 minutes before serving.
Garnish with chopped fresh chives, if desired.