我认为不可能为此使用您的web.xml。但是,您可以通过仔细编码 MyFilter 类来完成您想要的,这样您在添加新国家/地区时就不需要修改它。
我看到您正在使用 Spring。这是个好消息,因为MyFilter 实际上是一个由 Spring 管理的 bean。这意味着可能会向其注入其他 bean。我的建议是每个国家/地区有一个 bean 和一个负责委派给正确国家/地区 bean 的主过滤器。
首先,让您的web.xml 保持现在的状态:
<filter>
<filter-name>myfilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>myfilter</filter-name>
<url-pattern>*</url-pattern>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>
这将使名为 myfilter 的 bean 为每个请求调用。
然后,以与国家/地区无关的方式实施MyFilter,以便在添加或删除国家/地区时无需修改:
@Component("myfilter")
public class MyFilter implements Filter {
public static final String DEFAULT = "default";
public static final String SUFFIX = "_Filter";
// Autowire all beans that implement CountryFilter, mapped by bean name
@Autowired
private Map<String, CountryFilter> filters;
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
// Get country code from request
Locale locale = request.getLocale();
String countryCode = locale.getCountry().toUpperCase();
// Format key to gey country-specific filter
String key = countryCode + SUFFIX;
// If bean doesn't exist for request country...
if (!this.filters.containsKey(key)) {
// ..fallback to default filter
key = DEFAULT + SUFFIX;
}
// Get filter for country
CountryFilter filter = this.filters.get(key);
// Delegate to actual country (or default) filter
boolean countinueChain = filter.doFilterForCountry(request, response);
if (continueChain) {
chain.doFilter(request, response);
}
}
@Override
public void init(FilterConfig config) throws ServletException {
}
@Override
public void destroy() {
}
}
这个类足够通用。添加或删除国家/地区时无需更改它。诀窍是对集合使用 Spring 自动装配行为。如果您自动装配Map<String, T>,那么Spring 将使用类T 的所有bean 实例填充此映射,它们是与bean 名称和值对应的bean 实例相同的键。
那么,你需要CountryFilter 接口:
public interface CountryFilter {
boolean doFilterForCountry(ServletRequest request, ServletResponse response)
throws IOException, ServletException;
}
您需要为每个国家/地区实现CountryFilter,让它成为名称与CC_Filter 模式匹配的Spring bean,其中CC 代表2 位ISO 国家/地区代码。例如,对于美国,您可能有:
@Component("US" + MyFilter.SUFFIX)
public class UsFilter implements CountryFilter {
@Override
public boolean doFilterForCountry(ServletRequest request, ServletResponse response)
throws IOException, ServletException {
// TODO Handle US specifics here
// VERY IMPORTANT: you might want to let the chain continue...
return true;
// ...or redirect to US page
// ((HttpServletResponse) response).sendRedirect("US-url");
// return false;
// ONLY ONE of the options!
}
}
对于英国:
@Component("UK" + MyFilter.SUFFIX)
public class UkFilter implements CountryFilter {
@Override
public boolean doFilterForCountry(ServletRequest request, ServletResponse response)
throws IOException, ServletException {
// TODO Handle UK specifics here
// VERY IMPORTANT: you might want to let the chain continue...
return true;
// ...or redirect to UK page
// ((HttpServletResponse) response).sendRedirect("UK-url");
// return false;
// ONLY ONE of the options!
}
}
其他国家也一样。
最后,您可能没有针对给定国家/地区实施。在这种情况下,您可能希望使用默认过滤器作为后备案例:
@Component(MyFilter.DEFAULT + MyFilter.SUFFIX)
public class DefaultFilter implements CountryFilter {
@Override
public boolean doFilterForCountry(ServletRequest request, ServletResponse response)
throws IOException, ServletException {
// TODO Handle DEFAULT specifics here
// VERY IMPORTANT: you might want to let the chain continue...
return true;
// ...or redirect to DEFAULT page
// ((HttpServletResponse) response).sendRedirect("DEFAULT-url");
// return false;
// ONLY ONE of the options!
}
}
希望这可以帮助您解决问题。我相信这是一种非常灵活的方法,它甚至还有一个备用案例。要添加一个新国家/地区,您只需实现一个新的CountryFilter。