Spring Controller方法被多次调用

时间:2016-11-18 03:49:41

标签: java spring-mvc controller

以下是我的控制器内部的示例方法。当调用它时,该方法被调用三次。我的控制器内的所有方法也是如此。

@RequestMapping(value = "/pdf", method = RequestMethod.GET)
    public String generatePdfReport(HttpServletRequest request, HttpServletResponse response) throws JRException, IOException {
        System.out.println("Generating PDF ...");
        exporterService.download("pdf", request, response);
        return null;
    }

以下是我的配置文件:
AppConfig.java

@Configuration
    @EnableWebMvc
    @ComponentScan(basePackages = "com.package")
    public class AppConfig {
        @Bean
        public MessageSource messageSource() {
            ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
            messageSource.setBasename("messages");
            return messageSource;
        }
    }

ApplicationConfigAdapter.java

@Configuration
@EnableWebMvc
public class ApplicationConfigAdapter extends WebMvcConfigurerAdapter {

    private static final String RESOURCES_LOCATION = "/resources/";
    private static final String RESOURCES_HANDLER = RESOURCES_LOCATION + "**";
    @Bean
    public UserSessionInterceptor userSessionInterceptor() {
        return new UserSessionInterceptor();
    }
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(userSessionInterceptor()).addPathPatterns("/**").excludePathPatterns("/unsecured/**");
    }
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler(RESOURCES_HANDLER).addResourceLocations(RESOURCES_LOCATION);
    }
    @Bean
    public UrlBasedViewResolver tilesViewResolver() {
        UrlBasedViewResolver viewResolver = new UrlBasedViewResolver();
        viewResolver.setViewClass(TilesView.class);
        viewResolver.setOrder(1);
        return viewResolver;
    }
    @Bean
    public InternalResourceViewResolver jspViewResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setViewClass(JstlView.class);
        viewResolver.setPrefix("/WEB-INF/views/");
        viewResolver.setSuffix(".jsp");
        viewResolver.setOrder(2);
        return viewResolver;
    }
    @Bean
    public TilesConfigurer tilesConfigurer() {
        TilesConfigurer tilesConfigurer = new TilesConfigurer();
        // Default value will be /WEB-INF/tiles.xml
        tilesConfigurer.setDefinitions(new String[] { "/WEB-INF/defs/tiles.xml" });
        return tilesConfigurer;
    }
}

HibernateConfiguration.java

@Configuration
@EnableTransactionManagement
@ComponentScan({ "com.package" })
@PropertySource(value = { "classpath:application.properties" })
public class HibernateConfiguration {
    @Autowired
    private Environment environment;
    @Bean
    public LocalSessionFactoryBean sessionFactory() {
        LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
        sessionFactory.setDataSource(dataSource());
        sessionFactory.setPackagesToScan(new String[] { "com.package" });
        sessionFactory.setHibernateProperties(hibernateProperties());
        return sessionFactory;
    }
    @Bean
    @Resource(name = "jdbc/myDB")
    public DataSource dataSource() {
        final String DS_JNDI_NAME = "java:/comp/env/jdbc/myDB";
        final JndiDataSourceLookup dsLookup = new JndiDataSourceLookup();
        dsLookup.setResourceRef(false);
        DataSource dataSource = dsLookup.getDataSource(DS_JNDI_NAME);
        return dataSource;
    }
    private Properties hibernateProperties() {
        Properties properties = new Properties();
        properties.put("hibernate.dialect",
                environment.getRequiredProperty("hibernate.dialect"));
        properties.put("hibernate.show_sql",
                environment.getRequiredProperty("hibernate.show_sql"));
        properties.put("hibernate.format_sql",
                environment.getRequiredProperty("hibernate.format_sql"));
        return properties;
    }
    @Bean
    @Autowired
    public HibernateTransactionManager transactionManager(SessionFactory s) {
        HibernateTransactionManager txManager = new HibernateTransactionManager();
        txManager.setSessionFactory(s);
        return txManager;
    }
}

WebInitializer.java

public class WebInitializer implements WebApplicationInitializer {
    public void onStartup(ServletContext servletContext) throws ServletException {
        FilterRegistration.Dynamic fr = servletContext
                .addFilter("ResponseOverrideFilter", new ResponseOverrideFilter());
        fr.addMappingForUrlPatterns(null, true, "/*");

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

        Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));
        servlet.addMapping("/");
        servlet.setLoadOnStartup(1);
    }
}

Spring 4.0.3版

0 个答案:

没有答案