src/main/java/de/uapcore/lightpit/AbstractLightPITServlet.java

changeset 86
0a658e53177c
parent 83
24a3596b8f98
child 96
b7b685f31e39
equal deleted inserted replaced
85:3d16ad54b3dc 86:0a658e53177c
86 */ 86 */
87 private final Map<HttpMethod, Map<String, Method>> mappings = new HashMap<>(); 87 private final Map<HttpMethod, Map<String, Method>> mappings = new HashMap<>();
88 88
89 /** 89 /**
90 * Returns the name of the resource bundle associated with this servlet. 90 * Returns the name of the resource bundle associated with this servlet.
91 *
91 * @return the resource bundle base name 92 * @return the resource bundle base name
92 */ 93 */
93 protected abstract String getResourceBundleName(); 94 protected abstract String getResourceBundleName();
94 95
95 96
264 public void setStylesheet(HttpServletRequest req, String stylesheet) { 265 public void setStylesheet(HttpServletRequest req, String stylesheet) {
265 req.setAttribute(Constants.REQ_ATTR_STYLESHEET, Functions.enforceExt(stylesheet, ".css")); 266 req.setAttribute(Constants.REQ_ATTR_STYLESHEET, Functions.enforceExt(stylesheet, ".css"));
266 } 267 }
267 268
268 /** 269 /**
270 * Sets the view model object.
271 * The type must match the expected type in the JSP file.
272 *
273 * @param req the servlet request object
274 * @param viewModel the view model object
275 */
276 public void setViewModel(HttpServletRequest req, Object viewModel) {
277 req.setAttribute(Constants.REQ_ATTR_VIEWMODEL, viewModel);
278 }
279
280 /**
269 * Obtains a request parameter of the specified type. 281 * Obtains a request parameter of the specified type.
270 * The specified type must have a single-argument constructor accepting a string to perform conversion. 282 * The specified type must have a single-argument constructor accepting a string to perform conversion.
271 * The constructor of the specified type may throw an exception on conversion failures. 283 * The constructor of the specified type may throw an exception on conversion failures.
272 * 284 *
273 * @param req the servlet request object 285 * @param req the servlet request object
279 protected <T> Optional<T> getParameter(HttpServletRequest req, Class<T> clazz, String name) { 291 protected <T> Optional<T> getParameter(HttpServletRequest req, Class<T> clazz, String name) {
280 if (clazz.isArray()) { 292 if (clazz.isArray()) {
281 final String[] paramValues = req.getParameterValues(name); 293 final String[] paramValues = req.getParameterValues(name);
282 int len = paramValues == null ? 0 : paramValues.length; 294 int len = paramValues == null ? 0 : paramValues.length;
283 final var array = (T) Array.newInstance(clazz.getComponentType(), len); 295 final var array = (T) Array.newInstance(clazz.getComponentType(), len);
284 for (int i = 0 ; i < len ; i++) { 296 for (int i = 0; i < len; i++) {
285 try { 297 try {
286 final Constructor<?> ctor = clazz.getComponentType().getConstructor(String.class); 298 final Constructor<?> ctor = clazz.getComponentType().getConstructor(String.class);
287 Array.set(array, i, ctor.newInstance(paramValues[i])); 299 Array.set(array, i, ctor.newInstance(paramValues[i]));
288 } catch (ReflectiveOperationException e) { 300 } catch (ReflectiveOperationException e) {
289 throw new RuntimeException(e); 301 throw new RuntimeException(e);

mercurial