org.springframework.web.servlet.view.AbstractView Java Examples

The following examples show how to use org.springframework.web.servlet.view.AbstractView. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: UriTemplateServletAnnotationControllerHandlerMethodTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Override
public View resolveViewName(final String viewName, Locale locale) throws Exception {
	return new AbstractView () {
		@Override
		public String getContentType() {
			return null;
		}
		@Override
		protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request,
				HttpServletResponse response) throws Exception {
			for (String key : attrsToValidate.keySet()) {
				assertTrue("Model should contain attribute named " + key, model.containsKey(key));
				assertEquals(attrsToValidate.get(key), model.get(key));
				validatedAttrCount++;
			}
		}
	};
}
 
Example #2
Source File: UriTemplateServletAnnotationControllerHandlerMethodTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Override
public View resolveViewName(final String viewName, Locale locale) throws Exception {
	return new AbstractView () {
		@Override
		public String getContentType() {
			return null;
		}
		@Override
		protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request,
				HttpServletResponse response) throws Exception {
			for (String key : attrsToValidate.keySet()) {
				assertTrue("Model should contain attribute named " + key, model.containsKey(key));
				assertEquals(attrsToValidate.get(key), model.get(key));
				validatedAttrCount++;
			}
		}
	};
}
 
Example #3
Source File: ViewRenderInstrumentation.java    From apm-agent-java with Apache License 2.0 6 votes vote down vote up
@Advice.OnMethodEnter(suppress = Throwable.class)
public static void beforeExecute(@Advice.Local("span") @Nullable Span span,
                                 @Advice.This @Nullable Object thiz) {
    if (tracer == null || tracer.getActive() == null) {
        return;
    }
    final AbstractSpan<?> parent = tracer.getActive();

    String className = thiz.getClass().getName();
    span = parent.createSpan()
        .withType(SPAN_TYPE)
        .withSubtype(getSubtype(className))
        .withAction(SPAN_ACTION)
        .withName(DISPATCHER_SERVLET_RENDER_METHOD);

    if (thiz instanceof AbstractView) {
        AbstractView view = (AbstractView) thiz;
        span.appendToName(" ").appendToName(view.getBeanName());
    }
    span.activate();
}
 
Example #4
Source File: UriTemplateServletAnnotationControllerHandlerMethodTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Override
public View resolveViewName(final String viewName, Locale locale) throws Exception {
	return new AbstractView () {
		@Override
		public String getContentType() {
			return null;
		}
		@Override
		protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request,
				HttpServletResponse response) throws Exception {
			for (String key : attrsToValidate.keySet()) {
				assertTrue("Model should contain attribute named " + key, model.containsKey(key));
				assertEquals(attrsToValidate.get(key), model.get(key));
				validatedAttrCount++;
			}
		}
	};
}
 
Example #5
Source File: FreeMarkerViewTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void validTemplateName() throws Exception {
	FreeMarkerView fv = new FreeMarkerView();

	WebApplicationContext wac = mock(WebApplicationContext.class);
	MockServletContext sc = new MockServletContext();

	Map<String, FreeMarkerConfig> configs = new HashMap<>();
	FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
	configurer.setConfiguration(new TestConfiguration());
	configurer.setServletContext(sc);
	configs.put("configurer", configurer);
	given(wac.getBeansOfType(FreeMarkerConfig.class, true, false)).willReturn(configs);
	given(wac.getServletContext()).willReturn(sc);

	fv.setUrl("templateName");
	fv.setApplicationContext(wac);

	MockHttpServletRequest request = new MockHttpServletRequest();
	request.addPreferredLocale(Locale.US);
	request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
	request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
	HttpServletResponse response = new MockHttpServletResponse();

	Map<String, Object> model = new HashMap<>();
	model.put("myattr", "myvalue");
	fv.render(model, request, response);

	assertEquals(AbstractView.DEFAULT_CONTENT_TYPE, response.getContentType());
}
 
Example #6
Source File: FreeMarkerViewTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void validTemplateName() throws Exception {
	FreeMarkerView fv = new FreeMarkerView();

	WebApplicationContext wac = mock(WebApplicationContext.class);
	MockServletContext sc = new MockServletContext();

	Map<String, FreeMarkerConfig> configs = new HashMap<>();
	FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
	configurer.setConfiguration(new TestConfiguration());
	configurer.setServletContext(sc);
	configs.put("configurer", configurer);
	given(wac.getBeansOfType(FreeMarkerConfig.class, true, false)).willReturn(configs);
	given(wac.getServletContext()).willReturn(sc);

	fv.setUrl("templateName");
	fv.setApplicationContext(wac);

	MockHttpServletRequest request = new MockHttpServletRequest();
	request.addPreferredLocale(Locale.US);
	request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
	request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
	HttpServletResponse response = new MockHttpServletResponse();

	Map<String, Object> model = new HashMap<>();
	model.put("myattr", "myvalue");
	fv.render(model, request, response);

	assertEquals(AbstractView.DEFAULT_CONTENT_TYPE, response.getContentType());
}
 
Example #7
Source File: FreeMarkerViewTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void validTemplateName() throws Exception {
	FreeMarkerView fv = new FreeMarkerView();

	WebApplicationContext wac = mock(WebApplicationContext.class);
	MockServletContext sc = new MockServletContext();

	Map<String, FreeMarkerConfig> configs = new HashMap<String, FreeMarkerConfig>();
	FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
	configurer.setConfiguration(new TestConfiguration());
	configs.put("configurer", configurer);
	given(wac.getBeansOfType(FreeMarkerConfig.class, true, false)).willReturn(configs);
	given(wac.getServletContext()).willReturn(sc);

	fv.setUrl("templateName");
	fv.setApplicationContext(wac);

	MockHttpServletRequest request = new MockHttpServletRequest();
	request.addPreferredLocale(Locale.US);
	request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
	request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
	HttpServletResponse response = new MockHttpServletResponse();

	Map<String, Object> model = new HashMap<String, Object>();
	model.put("myattr", "myvalue");
	fv.render(model, request, response);

	assertEquals(AbstractView.DEFAULT_CONTENT_TYPE, response.getContentType());
}
 
Example #8
Source File: CrafterPageView.java    From engine with GNU General Public License v3.0 5 votes vote down vote up
protected void renderActualView(String pageViewName, Map<String, Object> model, HttpServletRequest request,
                                HttpServletResponse response) throws Exception {
    View actualView = delegatedViewResolver.resolveViewName(pageViewName, locale);
    if (actualView == null) {
        throw new RenderingException("No view was resolved for page view name '" + pageViewName + "'");
    }
    if (actualView instanceof AbstractView) {
        ((AbstractView) actualView).setContentType(getContentType());
    }

    actualView.render(model, request, response);
}
 
Example #9
Source File: VelocityViewTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Test
public void testExposeHelpers() throws Exception {
	final String templateName = "test.vm";

	WebApplicationContext wac = mock(WebApplicationContext.class);
	given(wac.getServletContext()).willReturn(new MockServletContext());

	final Template expectedTemplate = new Template();
	VelocityConfig vc = new VelocityConfig() {
		@Override
		public VelocityEngine getVelocityEngine() {
			return new TestVelocityEngine(templateName, expectedTemplate);
		}
	};
	Map<String, VelocityConfig> configurers = new HashMap<String, VelocityConfig>();
	configurers.put("velocityConfigurer", vc);
	given(wac.getBeansOfType(VelocityConfig.class, true, false)).willReturn(configurers);


	// let it ask for locale
	HttpServletRequest req = mock(HttpServletRequest.class);
	given(req.getAttribute(View.PATH_VARIABLES)).willReturn(null);
	given(req.getAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE)).willReturn(new AcceptHeaderLocaleResolver());
	given(req.getLocale()).willReturn(Locale.CANADA);

	final HttpServletResponse expectedResponse = new MockHttpServletResponse();

	VelocityView vv = new VelocityView() {
		@Override
		protected void mergeTemplate(Template template, Context context, HttpServletResponse response) throws Exception {
			assertTrue(template == expectedTemplate);
			assertTrue(response == expectedResponse);

			assertEquals("myValue", context.get("myHelper"));
			assertTrue(context.get("math") instanceof MathTool);

			assertTrue(context.get("dateTool") instanceof DateTool);
			DateTool dateTool = (DateTool) context.get("dateTool");
			assertTrue(dateTool.getLocale().equals(Locale.CANADA));

			assertTrue(context.get("numberTool") instanceof NumberTool);
			NumberTool numberTool = (NumberTool) context.get("numberTool");
			assertTrue(numberTool.getLocale().equals(Locale.CANADA));
		}

		@Override
		protected void exposeHelpers(Map<String, Object> model, HttpServletRequest request) throws Exception {
			model.put("myHelper", "myValue");
		}
	};

	vv.setUrl(templateName);
	vv.setApplicationContext(wac);
	Map<String, Class<?>> toolAttributes = new HashMap<String, Class<?>>();
	toolAttributes.put("math", MathTool.class);
	vv.setToolAttributes(toolAttributes);
	vv.setDateToolAttribute("dateTool");
	vv.setNumberToolAttribute("numberTool");
	vv.setExposeSpringMacroHelpers(false);

	vv.render(new HashMap<String, Object>(), req, expectedResponse);

	assertEquals(AbstractView.DEFAULT_CONTENT_TYPE, expectedResponse.getContentType());
}
 
Example #10
Source File: SoyTemplateViewResolver.java    From spring-soy-view with Apache License 2.0 4 votes vote down vote up
private View applyLifecycleMethods(String viewName, AbstractView view) {
	return (View) getApplicationContext().getAutowireCapableBeanFactory().initializeBean(view, viewName);
}