在遵循Spring MVC架构的同时,我需要为每个jsp配备一个控制器吗?例如。是否需要为aboutUs或PrivatePolicy或faqs jsp页面(具有静态数据)控制器。我可以将超链接指向下一页吗?
答案 0 :(得分:0)
没有。控制器和JSP很少是同构的。它通常是一对多的关系,一个控制器协调来自多个JSP的单个“渲染”。请记住,JSP最终是一个Servlet,可能包含其他Servlet或JSP(或调度,或使用FilterChain
)来呈现其响应。
答案 1 :(得分:0)
您可以在Spring XML配置或Java配置中执行以下操作,我更喜欢Java配置:
@Configuration
@EnableWebMvc
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class WebHomeConfig extends WebMvcConfigurerAdapter implements
ApplicationContextAware {
private ApplicationContext _appContext;
/*
* (non-Javadoc)
*
* @see
* org.springframework.context.ApplicationContextAware#setApplicationContext
* (org.springframework.context.ApplicationContext)
*/
@Override
public void setApplicationContext(ApplicationContext appContext)
throws BeansException {
_appContext = appContext;
}
@Bean
public ViewResolver getViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
return resolver;
}
/**
* Since we don't have any controller logic, simpler to just define
* controller for page using View Controller. Note: had to extend
* WebMvcConfigurerAdapter to get this functionality
*
* @param registry
*/
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("home");
registry.addViewController("/about").setViewName("aboutUs");
registry.addViewController("/privacy").setViewName("privacyPolicy");
}
}
然后在/ WEB-INF / views目录中,您将拥有以下JSP文件:
./home.jsp
./aboutUs.jsp
./privacyPolicy.jsp
以下是您在点击这些网址时将获得的JSP页面:
<baseURL>/ => home.jsp
<baseURL>/about => aboutUs.jsp
<baseURL>/privacy => privacyPolicy.jsp
希望能回答你的问题。