我有以下文件夹结构:
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;
}
我需要第三个@Requestmapping for admin2.jsp? admin3.jsp还有一个问题?等等?或者我可以将这些页面分组吗?
答案 0 :(得分:0)
您只能有一个方法,它会根据某些逻辑返回不同的视图。
ModelAndView model;
if (some_condition) {
model = new ModelAndView("admin");
}
else {
model = new ModelAndView("admin2");
}
return model;
答案 1 :(得分: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>
答案 2 :(得分:0)
@RequestMapping(value="/admin{index}", method = RequestMethod.GET)
public ModelAndView adminPage(@PathVariable String index){
return new ModelAndView("admin" + index);
}
您可以在您的情况下使用此代码,但我绝对不建议您这样做。最好为未来需求创建不同的控制器。