This project has retired. For details please refer to its
Attic page.
SelectLocaleServlet xref
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.tiles.test.servlet;
22
23 import java.util.Locale;
24
25 import javax.servlet.ServletConfig;
26 import javax.servlet.ServletException;
27 import javax.servlet.http.HttpServlet;
28 import javax.servlet.http.HttpServletRequest;
29 import javax.servlet.http.HttpServletResponse;
30 import javax.servlet.http.HttpSession;
31
32 import org.apache.tiles.TilesContainer;
33 import org.apache.tiles.access.TilesAccess;
34 import org.apache.tiles.locale.impl.DefaultLocaleResolver;
35 import org.apache.tiles.request.ApplicationContext;
36 import org.apache.tiles.request.Request;
37 import org.apache.tiles.request.servlet.ServletRequest;
38
39
40
41
42
43
44 public class SelectLocaleServlet extends HttpServlet {
45
46
47
48
49 private String containerKey;
50
51
52
53
54 private String definitionName;
55
56
57 @Override
58 public void init(ServletConfig config) throws ServletException {
59 super.init(config);
60 containerKey = config
61 .getInitParameter("org.apache.tiles.test.servlet.ServletConfig.CONTAINER_KEY");
62 definitionName = config
63 .getInitParameter("org.apache.tiles.test.servlet.ServletConfig.DEFINITION_NAME");
64 }
65
66
67 @Override
68 protected void doGet(HttpServletRequest request,
69 HttpServletResponse response) {
70 process(request, response);
71 }
72
73
74 @Override
75 protected void doPost(HttpServletRequest request,
76 HttpServletResponse response) {
77 process(request, response);
78 }
79
80
81
82
83
84
85
86 private void process(HttpServletRequest request,
87 HttpServletResponse response) {
88 String localeParameter = request.getParameter("locale");
89 HttpSession session = request.getSession();
90 Locale locale = null;
91 if (localeParameter != null && localeParameter.trim().length() > 0) {
92 String[] localeStrings = localeParameter.split("_");
93 if (localeStrings.length == 1) {
94 locale = new Locale(localeStrings[0]);
95 } else if (localeStrings.length == 2) {
96 locale = new Locale(localeStrings[0], localeStrings[1]);
97 } else if (localeStrings.length > 2) {
98 locale = new Locale(localeStrings[0], localeStrings[1], localeStrings[2]);
99 }
100 }
101 session.setAttribute(DefaultLocaleResolver.LOCALE_KEY, locale);
102 ApplicationContext applicationContext = org.apache.tiles.request.servlet.ServletUtil
103 .getApplicationContext(getServletContext());
104 Request currentRequest = new ServletRequest(applicationContext, request, response);
105 TilesAccess.setCurrentContainer(currentRequest, containerKey);
106 TilesContainer container = TilesAccess.getCurrentContainer(currentRequest);
107 container.render(definitionName, currentRequest);
108 }
109 }