我正在使用java和Spring MVC编写应用程序。我的范围是我的网址包含像http://localhost:8080/School/en/xxx/
...这样的语言。
在我的应用程序中,我使用下一个代码:
控制器servlet.xml中
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"/>
的web.xml
<filter>
<filter-name>URLFilter</filter-name>
<filter-class>filter.URLFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>URLFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
过滤
public void doFilter(ServletRequest hsRequest, ServletResponse hsResponse, FilterChain filterChain) throws IOException, ServletException {
final HttpServletRequest request = (HttpServletRequest) hsRequest;
final HttpServletResponse response = (HttpServletResponse) hsResponse;
String servletPath = request.getRequestURI().substring(request.getContextPath().length());
List<String> langList = new ArrayList<String>();
langList.add("/it/");
langList.add("/en/");
for (String lang : langList) {
if (StringUtils.startsWithIgnoreCase(servletPath, lang)) {
filterChain.doFilter(hsRequest, hsResponse);
return;
}
}
String string = request.getContextPath() + "/en".concat(servletPath);
response.sendRedirect(string);
}
拦截器:
public class LangInterceptor extends HandlerInterceptorAdapter {
@Autowired
SessionLocaleResolver sessionLocaleResolver;
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
String language = request.getServletPath().substring(1, 3);
Locale locale = new Locale(language);
sessionLocaleResolver.setLocale(request, response, locale);
return true;
}
我想下一个行为是:
因此,在重定向之前,我需要在“/ en”/之后验证网址中的内容,因此我需要@RequestMapping
@Controller
的值列表。我尝试使用RequestMappingHandlerMapping
,但这是不可能的,因为在过滤级别bean没有初始化。
我该如何解决这个问题?