ISE-ComProg-After-Midterm/screwdriver-3.5.6/box/snippets/java/GenericController.java

43 lines
1.1 KiB
Java
Raw Permalink Normal View History

2020-10-29 09:38:56 +00:00
package +zoccolo+.web.controller;
import +zoccolo+.web.controller.GenericComponent;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;
public class GenericController extends GenericComponent {
@ExceptionHandler(Exception.class)
public ModelAndView handleError(HttpServletRequest req, Exception exception) {
logger.error("Request: " + req.getRequestURL() + " raised " + exception);
exception.printStackTrace();
ModelAndView mav = new ModelAndView("page.error");
return mav;
}
protected String localizeViewName(String viewBaseName, HttpServletRequest request) {
String ret = viewBaseName;
String lang = getSessionAttribute("lang", request);
if ("en".equals(lang))
ret += ".en";
return ret;
}
@SuppressWarnings("unchecked")
protected <T> T getSessionAttribute(String name, HttpServletRequest request) {
try {
T toReturn = (T) request.getSession().getAttribute(name);
return toReturn == null ? null : toReturn;
} catch (ClassCastException e) {
return null;
}
}
}