Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Bean Configuration equivalent to <mvc:view-controller path="..." />

Tags:

spring-mvc

Trying to figure out what the equivalent @Bean configuration would be for the XML element

<mvc:view-controller path="..." />

Thanks!

like image 252
dardo Avatar asked Jun 11 '13 16:06

dardo


People also ask

What is Spring MVC configuration?

Annotating a class with the @Configuration indicates that the class can be used by the Spring IoC container as a source of bean definitions. To enable autodetection of the annotated controllers, it is required to add component scanning to the configuration. It also gives the path of base package (i.e com.

What is view controller Spring MVC?

The Spring Web model-view-controller (MVC) framework is designed around a DispatcherServlet that dispatches requests to handlers, with configurable handler mappings, view resolution, locale and theme resolution as well as support for uploading files.

What is Spring MVC Bean?

In Spring, the objects that form the backbone of your application and that are managed by the Spring IoC container are called beans. A bean is an object that is instantiated, assembled, and otherwise managed by a Spring IoC container. Otherwise, a bean is simply one of many objects in your application.

What is URL mapping in Spring MVC?

Tags:spring mvc | url mapping. In Spring MVC application, the SimpleUrlHandlerMapping is the most flexible handler mapping class, which allow developer to specify the mapping of URL pattern and handlers explicitly. The SimpleUrlHandlerMapping can be declared in two ways.


1 Answers

An example of forwarding a request for "/" to a view called "home" in Java:

@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {

  @Override
  public void addViewControllers(ViewControllerRegistry registry) {
    registry.addViewController("/").setViewName("home");
  }

}

And the same in XML use the element:

<mvc:view-controller path="/" view-name="home"/>

The above example was copied from the Spring reference docs

like image 103
matsev Avatar answered Jan 19 '23 21:01

matsev