use of org.springframework.web.servlet.support.RequestContext in project spring-framework by spring-projects.
the class FreeMarkerMacroTests method testExposeSpringMacroHelpers.
@Test
public void testExposeSpringMacroHelpers() throws Exception {
FreeMarkerView fv = new FreeMarkerView() {
@Override
@SuppressWarnings("rawtypes")
protected void processTemplate(Template template, SimpleHash fmModel, HttpServletResponse response) throws TemplateException {
Map model = fmModel.toMap();
assertTrue(model.get(FreeMarkerView.SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE) instanceof RequestContext);
RequestContext rc = (RequestContext) model.get(FreeMarkerView.SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE);
BindStatus status = rc.getBindStatus("tb.name");
assertEquals("name", status.getExpression());
assertEquals("juergen", status.getValue());
}
};
fv.setUrl(TEMPLATE_FILE);
fv.setApplicationContext(wac);
fv.setExposeSpringMacroHelpers(true);
Map<String, Object> model = new HashMap<>();
model.put("tb", new TestBean("juergen", 99));
fv.render(model, request, response);
}
use of org.springframework.web.servlet.support.RequestContext in project gocd by gocd.
the class JavascriptMessagesInterceptor method postHandle.
public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
if (modelAndView == null) {
return;
}
Map<String, Object> messages = new LinkedHashMap<>();
RequestContext requestContext = new RequestContext(httpServletRequest);
messages.put(Scheduled.toString().toLowerCase(), "Scheduled");
messages.put(CurrentStatus.BUILDING.getStatus().toLowerCase(), requestContext.getMessage(BUILDING));
messages.put(CurrentStatus.DISCONTINUED.getStatus().toLowerCase(), requestContext.getMessage(DISCONTINUED));
messages.put(CurrentStatus.PAUSED.getStatus().toLowerCase(), requestContext.getMessage(PAUSED));
messages.put(CurrentStatus.QUEUED.getStatus().toLowerCase(), requestContext.getMessage(QUEUED));
messages.put(CurrentStatus.WAITING.getStatus().toLowerCase(), requestContext.getMessage(WAITING));
messages.put(CurrentResult.PASSED.getStatus().toLowerCase(), requestContext.getMessage(PASSED).toLowerCase());
messages.put(CurrentResult.FAILED.getStatus().toLowerCase(), requestContext.getMessage(FAILED).toLowerCase());
messages.put(CurrentResult.UNKNOWN.getStatus().toLowerCase(), requestContext.getMessage(UNKNOWN).toLowerCase());
messages.put("last", requestContext.getMessage("label.last"));
String javascriptMessages = new JsonView().renderJson(messages);
modelAndView.addObject(JAVASCRIPT_MESSAGES_KEY, javascriptMessages);
}
use of org.springframework.web.servlet.support.RequestContext in project gocd by gocd.
the class RequestContextTestCase method setUp.
@Before
public void setUp() {
mockContext.checking(new ExpectationsForRequestContext());
requestContext = new RequestContext(request);
}
use of org.springframework.web.servlet.support.RequestContext in project spring-framework by spring-projects.
the class OptionsTagTests method exposeBindingResult.
@Override
protected void exposeBindingResult(Errors errors) {
// wrap errors in a Model
Map model = new HashMap();
model.put(BindingResult.MODEL_KEY_PREFIX + COMMAND_NAME, errors);
// replace the request context with one containing the errors
MockPageContext pageContext = getPageContext();
RequestContext context = new RequestContext((HttpServletRequest) pageContext.getRequest(), model);
pageContext.setAttribute(RequestContextAwareTag.REQUEST_CONTEXT_PAGE_ATTRIBUTE, context);
}
use of org.springframework.web.servlet.support.RequestContext in project spring-framework by spring-projects.
the class AbstractTemplateView method renderMergedOutputModel.
@Override
protected final void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
if (this.exposeRequestAttributes) {
for (Enumeration<String> en = request.getAttributeNames(); en.hasMoreElements(); ) {
String attribute = en.nextElement();
if (model.containsKey(attribute) && !this.allowRequestOverride) {
throw new ServletException("Cannot expose request attribute '" + attribute + "' because of an existing model object of the same name");
}
Object attributeValue = request.getAttribute(attribute);
if (logger.isDebugEnabled()) {
logger.debug("Exposing request attribute '" + attribute + "' with value [" + attributeValue + "] to model");
}
model.put(attribute, attributeValue);
}
}
if (this.exposeSessionAttributes) {
HttpSession session = request.getSession(false);
if (session != null) {
for (Enumeration<String> en = session.getAttributeNames(); en.hasMoreElements(); ) {
String attribute = en.nextElement();
if (model.containsKey(attribute) && !this.allowSessionOverride) {
throw new ServletException("Cannot expose session attribute '" + attribute + "' because of an existing model object of the same name");
}
Object attributeValue = session.getAttribute(attribute);
if (logger.isDebugEnabled()) {
logger.debug("Exposing session attribute '" + attribute + "' with value [" + attributeValue + "] to model");
}
model.put(attribute, attributeValue);
}
}
}
if (this.exposeSpringMacroHelpers) {
if (model.containsKey(SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE)) {
throw new ServletException("Cannot expose bind macro helper '" + SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE + "' because of an existing model object of the same name");
}
// Expose RequestContext instance for Spring macros.
model.put(SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE, new RequestContext(request, response, getServletContext(), model));
}
applyContentType(response);
renderMergedTemplateModel(model, request, response);
}
Aggregations