Most of those were not war but economical refugees and they forced through EU borders, which the EU states at the periphery of the EU have to guard. Belarus used them to blackmail Poland and Lituania. Turkey used the refugees to blackmail the EU for more funds to essentially keep them out. The fences worked in fact, because the refugees changed their route. Other states have also built fences, but Hungary was the first to do so and it is by far the largest fence built during the refugee crisis.
The US has a 650 mile border wall with Mexico exactly for this reason. Most of this border barrier was actually built during the Obama administration, Trump just used it as a campaign objective to build a "big, beautiful wall" instead of a fence.
A fence does not necessarily bar a country from accepting refugees, but offers better border enforcement and so does an adequate coast guard. If a state can't even enforce its borders, there is no state to begin with. When human traffickers are discouraged, they'll look for other sources of income.
I'm not for building fences and walls, but I can't really imagine other alternatives that actually work at enforcing borders at least to some extent.