Hello to everybody.
I am writing an application using java and Spring MVC. My scope is that my URL would contain language smth like http://localhost:8080/School/en/xxx/... . In my application I use next code:
controller-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>
Filter
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);
}
Interceptor: 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;
}
I suppose next behavior:
1. User introduces URL like: "httр://localhost:8080/School/" - filter doesn't find any "/it/" or "/en/" and add by default "/en/" than redirect to httр://localhost:8080/School/en/ where next time will find that is OK.
2. User introduces URL like: httр://localhost:8080/School/add - filter doesn't find any "/it/" or "/en/" and add by default "/en/" after redirect to httр://localhost:8080/School/en/add where find such URL that is OK.
3. User introduces URL like: httр://localhost:8080/School/en1/add - filter doesn't find any "/it/" or "/en/" and add by default "/en/" after redirect to httр://localhost:8080/School/en/en1/add where doesn't find such URL and redirect to 404.jsp that is NOT OK.
So before redirect I need to verify what follows in url after "/en"/, so I need a list of values of @RequestMapping of @Controller. I tried to use "RequestMappingHandlerMapping" but it is impossible because at filter level beans are not initialized.
Can anybody recommend how can solve this problem ?
Aucun commentaire:
Enregistrer un commentaire