Search in sources :

Example 6 with RequestContext

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);
}
Also used : HashMap(java.util.HashMap) TestBean(org.springframework.tests.sample.beans.TestBean) SimpleHash(freemarker.template.SimpleHash) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) DummyMacroRequestContext(org.springframework.web.servlet.view.DummyMacroRequestContext) RequestContext(org.springframework.web.servlet.support.RequestContext) BindStatus(org.springframework.web.servlet.support.BindStatus) HashMap(java.util.HashMap) Map(java.util.Map) Template(freemarker.template.Template) Test(org.junit.Test)

Example 7 with RequestContext

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);
}
Also used : JsonView(com.thoughtworks.go.server.web.JsonView) RequestContext(org.springframework.web.servlet.support.RequestContext) LinkedHashMap(java.util.LinkedHashMap)

Example 8 with RequestContext

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);
}
Also used : RequestContext(org.springframework.web.servlet.support.RequestContext) Before(org.junit.Before)

Example 9 with RequestContext

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);
}
Also used : HashMap(java.util.HashMap) MockPageContext(org.springframework.mock.web.test.MockPageContext) RequestContext(org.springframework.web.servlet.support.RequestContext) HashMap(java.util.HashMap) Map(java.util.Map)

Example 10 with RequestContext

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);
}
Also used : ServletException(javax.servlet.ServletException) HttpSession(javax.servlet.http.HttpSession) RequestContext(org.springframework.web.servlet.support.RequestContext)

Aggregations

RequestContext (org.springframework.web.servlet.support.RequestContext)13 HashMap (java.util.HashMap)5 Map (java.util.Map)4 MockPageContext (org.springframework.mock.web.test.MockPageContext)4 Test (org.junit.Test)3 List (java.util.List)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 PageContext (javax.servlet.jsp.PageContext)2 MessageSourceResolvable (org.springframework.context.MessageSourceResolvable)2 DefaultMessageSourceResolvable (org.springframework.context.support.DefaultMessageSourceResolvable)2 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)2 MockHttpServletResponse (org.springframework.mock.web.test.MockHttpServletResponse)2 TestBean (org.springframework.tests.sample.beans.TestBean)2 StaticWebApplicationContext (org.springframework.web.context.support.StaticWebApplicationContext)2 JspAwareRequestContext (org.springframework.web.servlet.support.JspAwareRequestContext)2 JsonView (com.thoughtworks.go.server.web.JsonView)1 SimpleHash (freemarker.template.SimpleHash)1 Template (freemarker.template.Template)1 ArrayList (java.util.ArrayList)1