如何用Spring分隔不同文件夹中的jsp?

时间:2015-07-16 18:13:28

标签: spring jsp spring-mvc

这是我的AppInitializer:

    @Configuration
    @EnableWebMvc
    @ComponentScan(basePackages = "com.project.app")
    public class AppInitializer extends
            AbstractAnnotationConfigDispatcherServletInitializer implements
            WebApplicationInitializer {

        @Override
        public void onStartup(ServletContext container) throws ServletException {

            AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
            ctx.register(AppConfig.class);
            ctx.setServletContext(container);

            ServletRegistration.Dynamic servlet = container.addServlet(
                    "dispatcher", new DispatcherServlet(ctx));

            servlet.setLoadOnStartup(1);
            servlet.addMapping("/");
        }

        @Override
        protected Class<?>[] getRootConfigClasses() {
            return new Class[] { AppConfig.class };
        }

        @Override
        protected Class<?>[] getServletConfigClasses() {
            return null;
        }

        @Override
        protected String[] getServletMappings() {
            return new String[] { "/" };
        }
    }

这是我的AppConfig:

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.acfcm.app")
@Import({ SecurityConfig.class })
public class AppConfig extends WebMvcConfigurerAdapter {

    @Bean
    public ViewResolver viewResolverForClasses() {
        ResourceBundleViewResolver viewResolver = new ResourceBundleViewResolver();
        viewResolver.setOrder(1);
        viewResolver.setBasename("views");

        return viewResolver;
    }

    @Bean
    public ViewResolver viewResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setOrder(2);
        viewResolver.setViewClass(JstlView.class);
        viewResolver.setPrefix("/WEB-INF/views/");
        viewResolver.setSuffix(".jsp");

        return viewResolver;
    }

    @Bean
    public MessageSource messageSource() {
        ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
        messageSource.setBasename("messages");
        return messageSource;
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations(
                "/resources/");
    }
}

我想要的是如何更改AppConfig以将.jsp放入项目的不同内容中,因为现在只能将它保存在/ WEB-INF / views /中并且有很多.jsp !!我想再有两个文件夹来查看我的项目:

  • WEB-INF / views / moduleOne /
  • WEB-INF /视图/ ModuleTWO中/
  • ...

谢谢!

2 个答案:

答案 0 :(得分:0)

我认为你的代码很好;它将从/ WEB-INF / views /

开始查找您的JSP

大多数人都喜欢你说并在/ admin,/ common等文件夹下分解JSP视图。

执行此操作的方法是在控制器中指定子文件夹。例如,在您的控制器中,您可以返回:

      @RequestMapping(value = "/admin/index.htm", method = RequestMethod.GET)
  public ModelAndView index(HttpServletRequest request,
                            HttpServletResponse response)
    {
    Map<String, Object> myModel = new HashMap<String, Object>();
    myModel.put("someValues", new ArrayList());
    ModelAndView mv = new ModelAndView("admin/index","model", myModel);
    return mv;
  }

像上面这样做,你可以把你的JSP放在 /WEB-INF/views/admin/index.jsp

当然,您的映射(/ admin)不必匹配目录结构(/ WEB-INF / views / admin),但我们选择将两者匹配,以便更快地找到代码(如果控制器映射匹配dir结构。)

要记住的重要一点是,无论你在ModelAndView第一个参数中放置什么,Spring都会在你的代码中添加和追加你定义的值:

    viewResolver.setPrefix("/WEB-INF/views/");
    viewResolver.setSuffix(".jsp");

所以,如果你返回ModelAndview(“/ pierre / was / here / something”),Spring将尝试在位置找到JSP:“/ WWE-INF / views / pierre / was / where / something.jsp”

答案 1 :(得分:0)

通常的做法是将配置作为您的配置,并在请求处理程序控制器方法中,将视图名称用作moduleOne/view1moduleTwo/view2等。