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

The following examples show how to use org.springframework.web.servlet.view.InternalResourceViewResolver. 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: WebMvcConfigurationSupport.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Register a {@link ViewResolverComposite} that contains a chain of view resolvers
 * to use for view resolution.
 * By default this resolver is ordered at 0 unless content negotiation view
 * resolution is used in which case the order is raised to
 * {@link org.springframework.core.Ordered#HIGHEST_PRECEDENCE
 * Ordered.HIGHEST_PRECEDENCE}.
 * <p>If no other resolvers are configured,
 * {@link ViewResolverComposite#resolveViewName(String, Locale)} returns null in order
 * to allow other potential {@link ViewResolver} beans to resolve views.
 * @since 4.1
 */
@Bean
public ViewResolver mvcViewResolver(ContentNegotiationManager mvcContentNegotiationManager) {
	ViewResolverRegistry registry = new ViewResolverRegistry(
			mvcContentNegotiationManager, this.applicationContext);
	configureViewResolvers(registry);

	if (registry.getViewResolvers().isEmpty() && this.applicationContext != null) {
		String[] names = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(
				this.applicationContext, ViewResolver.class, true, false);
		if (names.length == 1) {
			registry.getViewResolvers().add(new InternalResourceViewResolver());
		}
	}

	ViewResolverComposite composite = new ViewResolverComposite();
	composite.setOrder(registry.getOrder());
	composite.setViewResolvers(registry.getViewResolvers());
	if (this.applicationContext != null) {
		composite.setApplicationContext(this.applicationContext);
	}
	if (this.servletContext != null) {
		composite.setServletContext(this.servletContext);
	}
	return composite;
}
 
Example #2
Source File: ServletAnnotationControllerHandlerMethodTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void emptyParameterListHandleMethod() throws Exception {
	initServlet(wac -> {
		RootBeanDefinition vrDef = new RootBeanDefinition(InternalResourceViewResolver.class);
		vrDef.getPropertyValues().add("suffix", ".jsp");
		wac.registerBeanDefinition("viewResolver", vrDef);
	}, EmptyParameterListHandlerMethodController.class);

	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/emptyParameterListHandler");
	MockHttpServletResponse response = new MockHttpServletResponse();

	EmptyParameterListHandlerMethodController.called = false;
	getServlet().service(request, response);
	assertTrue(EmptyParameterListHandlerMethodController.called);
	assertEquals("", response.getContentAsString());
}
 
Example #3
Source File: DispatcherServletTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void withNoViewAndSamePath() throws Exception {
	InternalResourceViewResolver vr = (InternalResourceViewResolver) complexDispatcherServlet
			.getWebApplicationContext().getBean("viewResolver2");
	vr.setSuffix("");

	MockServletContext servletContext = new MockServletContext();
	MockHttpServletRequest request = new MockHttpServletRequest(servletContext, "GET", "/noview");
	MockHttpServletResponse response = new MockHttpServletResponse();

	try {
		complexDispatcherServlet.service(request, response);
		fail("Should have thrown ServletException");
	}
	catch (ServletException ex) {
		ex.printStackTrace();
	}
}
 
Example #4
Source File: SimpleWebApplicationContext.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Override
public void refresh() throws BeansException {
	registerSingleton("/locale.do", LocaleChecker.class);

	addMessage("test", Locale.ENGLISH, "test message");
	addMessage("test", Locale.CANADA, "Canadian & test message");
	addMessage("testArgs", Locale.ENGLISH, "test {0} message {1}");
	addMessage("testArgsFormat", Locale.ENGLISH, "test {0} message {1,number,#.##} X");

	registerSingleton(UiApplicationContextUtils.THEME_SOURCE_BEAN_NAME, DummyThemeSource.class);

	registerSingleton("handlerMapping", BeanNameUrlHandlerMapping.class);
	registerSingleton("viewResolver", InternalResourceViewResolver.class);

	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.add("location", "org/springframework/web/context/WEB-INF/sessionContext.xml");
	registerSingleton("viewResolver2", XmlViewResolver.class, pvs);

	super.refresh();
}
 
Example #5
Source File: ZkWebMvcConfiguration.java    From zkspringboot with Apache License 2.0 6 votes vote down vote up
@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
	if(zkProperties.isZulViewResolverEnabled()) {
		final String springbootPackaging = zkProperties.getSpringbootPackaging();
		String prefix;
		if(zkProperties.isWar()) {
			prefix = zkProperties.getZulViewResolverPrefix() + "/";
		} else {
			prefix = zkProperties.getUpdateUri() + ClassWebResource.PATH_PREFIX + zkProperties.getZulViewResolverPrefix() + "/";
		}
		final String suffix = zkProperties.getZulViewResolverSuffix();
		logger.info("ZK-Springboot: InternalViewResolver enabled - e.g. resolving view 'example' to '{}example{}'", prefix, suffix);
		InternalResourceViewResolver resolver = new InternalResourceViewResolver(prefix, suffix);
		resolver.setOrder(InternalResourceViewResolver.LOWEST_PRECEDENCE);
		registry.viewResolver(resolver);
	}
}
 
Example #6
Source File: DispatcherServletTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void withNoViewAndSamePath() throws Exception {
	InternalResourceViewResolver vr = (InternalResourceViewResolver) complexDispatcherServlet
			.getWebApplicationContext().getBean("viewResolver2");
	vr.setSuffix("");

	MockServletContext servletContext = new MockServletContext();
	MockHttpServletRequest request = new MockHttpServletRequest(servletContext, "GET", "/noview");
	MockHttpServletResponse response = new MockHttpServletResponse();

	try {
		complexDispatcherServlet.service(request, response);
		fail("Should have thrown ServletException");
	}
	catch (ServletException ex) {
		ex.printStackTrace();
	}
}
 
Example #7
Source File: WebMvcConfigurationSupport.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Register a {@link ViewResolverComposite} that contains a chain of view resolvers
 * to use for view resolution.
 * By default this resolver is ordered at 0 unless content negotiation view
 * resolution is used in which case the order is raised to
 * {@link org.springframework.core.Ordered#HIGHEST_PRECEDENCE
 * Ordered.HIGHEST_PRECEDENCE}.
 * <p>If no other resolvers are configured,
 * {@link ViewResolverComposite#resolveViewName(String, Locale)} returns null in order
 * to allow other potential {@link ViewResolver} beans to resolve views.
 * @since 4.1
 */
@Bean
public ViewResolver mvcViewResolver() {
	ViewResolverRegistry registry = new ViewResolverRegistry(
			mvcContentNegotiationManager(), this.applicationContext);
	configureViewResolvers(registry);

	if (registry.getViewResolvers().isEmpty()) {
		String[] names = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(
				this.applicationContext, ViewResolver.class, true, false);
		if (names.length == 1) {
			registry.getViewResolvers().add(new InternalResourceViewResolver());
		}
	}

	ViewResolverComposite composite = new ViewResolverComposite();
	composite.setOrder(registry.getOrder());
	composite.setViewResolvers(registry.getViewResolvers());
	composite.setApplicationContext(this.applicationContext);
	composite.setServletContext(this.servletContext);
	return composite;
}
 
Example #8
Source File: WebMvcConfigurationSupport.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Register a {@link ViewResolverComposite} that contains a chain of view resolvers
 * to use for view resolution.
 * By default this resolver is ordered at 0 unless content negotiation view
 * resolution is used in which case the order is raised to
 * {@link org.springframework.core.Ordered#HIGHEST_PRECEDENCE
 * Ordered.HIGHEST_PRECEDENCE}.
 * <p>If no other resolvers are configured,
 * {@link ViewResolverComposite#resolveViewName(String, Locale)} returns null in order
 * to allow other potential {@link ViewResolver} beans to resolve views.
 * @since 4.1
 */
@Bean
public ViewResolver mvcViewResolver() {
	ViewResolverRegistry registry = new ViewResolverRegistry(
			mvcContentNegotiationManager(), this.applicationContext);
	configureViewResolvers(registry);

	if (registry.getViewResolvers().isEmpty() && this.applicationContext != null) {
		String[] names = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(
				this.applicationContext, ViewResolver.class, true, false);
		if (names.length == 1) {
			registry.getViewResolvers().add(new InternalResourceViewResolver());
		}
	}

	ViewResolverComposite composite = new ViewResolverComposite();
	composite.setOrder(registry.getOrder());
	composite.setViewResolvers(registry.getViewResolvers());
	if (this.applicationContext != null) {
		composite.setApplicationContext(this.applicationContext);
	}
	if (this.servletContext != null) {
		composite.setServletContext(this.servletContext);
	}
	return composite;
}
 
Example #9
Source File: CartControllerTest.java    From market with MIT License 6 votes vote down vote up
@BeforeEach
public void beforeEach() {
	CartController controller = new CartController(cartService, productService, marketProperties);
	mockMvc = MockMvcBuilders.standaloneSetup(controller)
		.addInterceptors(new SessionCartInterceptor())
		.setViewResolvers(new InternalResourceViewResolver("/WEB-INF/view/", ".jsp"))
		.build();

	account = FixturesFactory.account().setEmail("user").build();
	cart = new Cart(account);
	emptyCart = cartDtoAssembler.toModel(cart);
	Region region = FixturesFactory.region().build();
	Distillery distillery = FixturesFactory.distillery(region).build();
	product = FixturesFactory.product(distillery).build();

	principal = new PrincipalImpl(account.getEmail());
}
 
Example #10
Source File: ServletAnnotationControllerTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void emptyParameterListHandleMethod() throws Exception {
	@SuppressWarnings("serial") DispatcherServlet servlet = new DispatcherServlet() {
		@Override
		protected WebApplicationContext createWebApplicationContext(WebApplicationContext parent) {
			GenericWebApplicationContext wac = new GenericWebApplicationContext();
			wac.registerBeanDefinition("controller",
					new RootBeanDefinition(EmptyParameterListHandlerMethodController.class));
			RootBeanDefinition vrDef = new RootBeanDefinition(InternalResourceViewResolver.class);
			vrDef.getPropertyValues().add("suffix", ".jsp");
			wac.registerBeanDefinition("viewResolver", vrDef);
			wac.refresh();
			return wac;
		}
	};
	servlet.init(new MockServletConfig());

	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/emptyParameterListHandler");
	MockHttpServletResponse response = new MockHttpServletResponse();

	EmptyParameterListHandlerMethodController.called = false;
	servlet.service(request, response);
	assertTrue(EmptyParameterListHandlerMethodController.called);
	assertEquals("", response.getContentAsString());
}
 
Example #11
Source File: SpringMvcConfig.java    From Spring-5.0-Cookbook with MIT License 5 votes vote down vote up
@Bean
public InternalResourceViewResolver getViewResolver() {
    InternalResourceViewResolver resolver = new InternalResourceViewResolver();
    resolver.setPrefix("/WEB-INF/");
    resolver.setSuffix(".html");
    resolver.setOrder(3);
    return resolver;
}
 
Example #12
Source File: WebConfig.java    From redis-admin with Apache License 2.0 5 votes vote down vote up
@Bean
public InternalResourceViewResolver viewResolver() {
	InternalResourceViewResolver internalResourceViewResolver = new InternalResourceViewResolver();
    internalResourceViewResolver.setOrder(1);
    internalResourceViewResolver.setContentType("text/html; charset=utf-8");
    internalResourceViewResolver.setSuffix(".jsp");
    internalResourceViewResolver.setPrefix("/WEB-INF/view/");
    return internalResourceViewResolver;
}
 
Example #13
Source File: WebConfig.java    From Spring with Apache License 2.0 5 votes vote down vote up
@Bean
public InternalResourceViewResolver internalResourceViewResolver(){
	final InternalResourceViewResolver resolver = new InternalResourceViewResolver();
	resolver.setPrefix("/WEB-INF/jsp/");
	resolver.setSuffix(".jsp");
	return resolver;
}
 
Example #14
Source File: WebConfig.java    From Spring with Apache License 2.0 5 votes vote down vote up
@Bean
InternalResourceViewResolver getViewResolver(){
    InternalResourceViewResolver resolver = new InternalResourceViewResolver();
    resolver.setPrefix("/WEB-INF/");
    resolver.setSuffix(".jsp" );
    return resolver;
}
 
Example #15
Source File: MvcConfig.java    From tutorials with MIT License 5 votes vote down vote up
@Bean
public ViewResolver viewResolver() {
    final InternalResourceViewResolver bean = new InternalResourceViewResolver();

    bean.setViewClass(JstlView.class);
    bean.setPrefix("/WEB-INF/view/");
    bean.setSuffix(".jsp");

    return bean;
}
 
Example #16
Source File: SpringDispatcherConfig.java    From Spring-5.0-Cookbook with MIT License 5 votes vote down vote up
@Bean
public InternalResourceViewResolver jspViewResolver() {
    InternalResourceViewResolver viewResolverA = new InternalResourceViewResolver();
    viewResolverA.setPrefix("/WEB-INF/jsp/");
    viewResolverA.setSuffix(".jsp");
    viewResolverA.setOrder(1);
    return viewResolverA;
}
 
Example #17
Source File: SpringDispatcherConfig.java    From Spring-5.0-Cookbook with MIT License 5 votes vote down vote up
@Bean
public InternalResourceViewResolver jspViewResolver() {
    InternalResourceViewResolver viewResolverA = new InternalResourceViewResolver();
    viewResolverA.setPrefix("/WEB-INF/jsp/");
    viewResolverA.setSuffix(".jsp");
    viewResolverA.setOrder(3);
    return viewResolverA;
}
 
Example #18
Source File: SpringDispatcherConfig.java    From Spring-5.0-Cookbook with MIT License 5 votes vote down vote up
@Bean
public InternalResourceViewResolver jspViewResolver() {
    InternalResourceViewResolver viewResolverA = new InternalResourceViewResolver();
    viewResolverA.setPrefix("/WEB-INF/jsp/");
    viewResolverA.setSuffix(".jsp");
    viewResolverA.setOrder(1);
    return viewResolverA;
}
 
Example #19
Source File: FormControllerTests.java    From spring-tutorial with Creative Commons Attribution Share Alike 4.0 International 5 votes vote down vote up
@Before
public void setup() throws Exception {

	InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
	viewResolver.setPrefix("/WEB-INF/");
	viewResolver.setSuffix(".jsp");

	this.mockMvc = standaloneSetup(new FormController()).setViewResolvers(viewResolver).build();
}
 
Example #20
Source File: WebConfig.java    From Spring with Apache License 2.0 5 votes vote down vote up
@Bean
public ViewResolver viewResolver() {
    final InternalResourceViewResolver resolver = new InternalResourceViewResolver();
    resolver.setPrefix("/WEB-INF/");
    resolver.setSuffix(".jsp");
    resolver.setExposeContextBeansAsAttributes(true);
    resolver.setViewClass(JstlView.class);
    return resolver;
}
 
Example #21
Source File: MvcConfig.java    From tutorials with MIT License 5 votes vote down vote up
@Bean
public ViewResolver viewResolver() {
    final InternalResourceViewResolver bean = new InternalResourceViewResolver();

    bean.setViewClass(JstlView.class);
    bean.setPrefix("/WEB-INF/view/");
    bean.setSuffix(".jsp");

    return bean;
}
 
Example #22
Source File: WebConfig.java    From Spring with Apache License 2.0 5 votes vote down vote up
@Bean
public InternalResourceViewResolver getViewResolver(){
    final InternalResourceViewResolver resolver = new InternalResourceViewResolver();
    resolver.setSuffix("/WEB-INF/");
    resolver.setPrefix(".jsp");
    return resolver;
}
 
Example #23
Source File: SimpleUrlHandlerMappingConfig.java    From tutorials with MIT License 5 votes vote down vote up
@Bean
public ViewResolver viewResolverSimpleMappingConfig() {
    InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
    viewResolver.setPrefix("/");
    viewResolver.setSuffix(".jsp");
    return viewResolver;
}
 
Example #24
Source File: WebConfig.java    From Spring with Apache License 2.0 5 votes vote down vote up
@Bean
public ViewResolver viewResolver() {
    final InternalResourceViewResolver resolver = new InternalResourceViewResolver();
    resolver.setPrefix("/WEB-INF/");
    resolver.setSuffix(".jsp");
    resolver.setExposeContextBeansAsAttributes(true);
    resolver.setViewClass(JstlView.class);
    return resolver;
}
 
Example #25
Source File: WebConfig.java    From Spring with Apache License 2.0 5 votes vote down vote up
@Bean
public ViewResolver viewResolver() {
    final InternalResourceViewResolver resolver = new InternalResourceViewResolver();
    resolver.setPrefix("/WEB-INF/");
    resolver.setSuffix(".jsp");
    resolver.setExposeContextBeansAsAttributes(true);
    resolver.setViewClass(JstlView.class);
    return resolver;
}
 
Example #26
Source File: WebConfig.java    From Spring with Apache License 2.0 5 votes vote down vote up
@Bean
public ViewResolver viewResolver() {
    final InternalResourceViewResolver resolver = new InternalResourceViewResolver();
    resolver.setPrefix("/WEB-INF/");
    resolver.setSuffix(".jsp");
    resolver.setExposeContextBeansAsAttributes(true);
    resolver.setViewClass(JstlView.class);
    return resolver;
}
 
Example #27
Source File: MvcConfiguration.java    From oncokb with GNU Affero General Public License v3.0 5 votes vote down vote up
@Bean
public ViewResolver getViewResolver() {
    InternalResourceViewResolver resolver = new InternalResourceViewResolver();
    resolver.setPrefix("/");
    resolver.setSuffix(".html");
    return resolver;
}
 
Example #28
Source File: MvcConfig.java    From tutorials with MIT License 5 votes vote down vote up
@Bean
public ViewResolver viewResolver() {
    final InternalResourceViewResolver bean = new InternalResourceViewResolver();
    bean.setViewClass(JstlView.class);
    bean.setPrefix("/WEB-INF/view/");
    bean.setSuffix(".jsp");
    return bean;
}
 
Example #29
Source File: WebConfig.java    From Spring with Apache License 2.0 5 votes vote down vote up
@Bean
public ViewResolver viewResolver() {
	InternalResourceViewResolver resolver = new InternalResourceViewResolver();
	resolver.setPrefix("/WEB-INF/");
	resolver.setSuffix(".jsp");
	resolver.setExposeContextBeansAsAttributes(true);
	resolver.setViewClass(JstlView.class);
	return resolver;
}
 
Example #30
Source File: WebConfig.java    From Spring with Apache License 2.0 5 votes vote down vote up
@Bean
public ViewResolver viewResolver() {
    final InternalResourceViewResolver resolver = new InternalResourceViewResolver();
    resolver.setPrefix("/WEB-INF/");
    resolver.setSuffix(".jsp");
    resolver.setExposeContextBeansAsAttributes(true);
    resolver.setViewClass(JstlView.class);
    return resolver;
}