Roberto
Roberto

Reputation: 178

@RequestMapping for multiple pages

I have the following folder structure:

WEB-INF/
-- pages/
----home.jsp
----admin.jsp
----admin2.jsp
----admin3.jsp

And I have the following controller @requestmapping:

@RequestMapping(value = "/", method = RequestMethod.GET)
public ModelAndView defaultPage() {

    ModelAndView model = new ModelAndView();
    model.setViewName("home");
    return model;

}

@RequestMapping(value = "/admin**", method = RequestMethod.GET)
public ModelAndView adminPage() {

    ModelAndView model = new ModelAndView();
    model.setViewName("admin");
    return model;

}

I need a third @Requestmapping for admin2.jsp? And a fouth for the admin3.jsp? An so on? Or can I group this pages?

Upvotes: 1

Views: 2021

Answers (3)

likeachamp
likeachamp

Reputation: 874

@RequestMapping(value="/admin{index}", method = RequestMethod.GET)
public ModelAndView adminPage(@PathVariable String index){

    return new ModelAndView("admin" + index);
}

You can use this code in your case but I definitely do not suggest you to do. It is better to create different controllers for future needs.

Upvotes: 2

dimitrisli
dimitrisli

Reputation: 21381

Based on your question ("group the pages") I am guessing you want to quickly map to the view without the in-between controller.

If that's the case here's the answer (if not, consider it as another option):

Java Config

@EnableWebMvc
@Configuration
public class AppConfig extends WebMvcConfigurerAdapter {

    /**
     * Here we register all direct mappings from URL directly to View Jsp
     * without the need to define an in-between controller.
     *
     * @param registry
     */
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("home");
        registry.addViewController("/admin").setViewName("admin");
        registry.addViewController("/admin2").setViewName("admin2");
        registry.addViewController("/admin").setViewName("admin3");
    }

   //assuming your view resolver here
   @Bean
    public ViewResolver viewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/view/");
        resolver.setSuffix(".jsp");
        return resolver;
    }
   //..
}

XML Config

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd 
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

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

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
    <property name="prefix" value="/WEB-INF/view/"/>
    <property name="suffix" value=".jsp"/>
</bean>

</beans>

Upvotes: 0

Predrag Maric
Predrag Maric

Reputation: 24403

You can have only one method, that would return different views based on some logic.

ModelAndView model;
if (some_condition) {
    model = new ModelAndView("admin");
}
else {
    model = new ModelAndView("admin2");
}
return model;

Upvotes: 0

Related Questions