2014-10-16 85 views
0

我有以下文件夾結構:@RequestMapping多頁

WEB-INF/
- 頁/
----針對home.jsp
---- admin.jsp
- --- admin2.jsp
---- admin3.jsp

而且我有以下控制器@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; 

} 

我需要爲admin2.jsp添加第三個@Requestmapping?而且一個爲admin3.jsp的?等等?或者我可以將這些頁面分組嗎?

回答

0

您只能有一個方法,它會根據某種邏輯返回不同的視圖。

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

根據您的問題,我猜你想快速映射到視圖沒有在中間控制器(「組頁」)。

如果是這樣的話,這裏的答案(如果沒有,把它作爲另一種選擇):

Java的配置

@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配置

<?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> 
0
@RequestMapping(value="/admin{index}", method = RequestMethod.GET) 
public ModelAndView adminPage(@PathVariable String index){ 

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

你可以使用這個鱈魚在你的情況下,但我絕對不建議你這樣做。爲未來的需求創建不同的控制器會更好。