美文网首页
Spring学习笔记 - 第008天

Spring学习笔记 - 第008天

作者: 迷茫o | 来源:发表于2017-03-27 09:25 被阅读0次

    用代码配置Spring Web MVC

    初始化配置

    public class MyWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
    
        @Override
        protected Class<?>[] getRootConfigClasses() {
            // 返回配置IoC容器的类的类对象
            return new Class<?>[] { AppConfig.class }; 
        }
    
        @Override
        protected Class<?>[] getServletConfigClasses() {
            // 返回配置Spring MVC(视图解析器、控制器等)的类的类对象
            return new Class<?>[] { WebConfig.class };
        }
    
        @Override
        protected String[] getServletMappings() {
                    // 前端控制器映射
            return new String[] { "/" };
        }
    
    }
    

    配置IoC容器的类

    @Configuration
    @EnableAspectJAutoProxy
    @EnableTransactionManagement
    @ComponentScan(basePackages = "com.kygo.springmvc")
    public class AppConfig {
        
        @Bean
        public DataSource dataSource() {
            BasicDataSource dataSource = new BasicDataSource();
            dataSource.setDriverClassName("com.mysql.jdbc.Driver");
            dataSource.setUrl("jdbc:mysql://localhost:3306/dang?useUnicode=true&characterEncoding=utf8");
            dataSource.setUsername("root");
            dataSource.setPassword("123456");
            dataSource.setInitialSize(10);
            dataSource.setMaxIdle(50);
            return dataSource;
        }
        
        @Bean
        public LocalSessionFactoryBean sessionFactory(DataSource dataSource) {
            LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
            sessionFactory.setDataSource(dataSource);
            sessionFactory.setPackagesToScan(new String[] { "com.kygo.springmvc.entity" });
            Properties props = new Properties();
            props.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
            props.setProperty("hibernate.show_sql", "true");
            props.setProperty("hibernate.format_sql", "true");
            sessionFactory.setHibernateProperties(props);
            return sessionFactory;
        }
        
        @Bean
        public HibernateTransactionManager transactionManager(SessionFactory sessionFactory) {
            return new HibernateTransactionManager(sessionFactory);
        }
    }
    

    配置Spring MVC(视图解析器、控制器等)的类

    @Configuration
    @EnableWebMvc
    @ComponentScan(basePackages = "com.kygo.springmvc.controller")
    public class WebConfig extends WebMvcConfigurerAdapter {    
        // 视图解析器
        @Bean
        public ViewResolver internalResourceViewResolver() {
            InternalResourceViewResolver resolver =
                    new InternalResourceViewResolver();
            resolver.setViewClass(JstlView.class);
            resolver.setPrefix("/WEB-INF/jsp/");
            resolver.setSuffix(".jsp");
            return resolver;
        }
        
            // 静态网页依然由默认servlet处理
        @Override
        public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
            configurer.enable();
        }
    }
    

    表单数据验证

    需要jar包

    validation-api-1.1.0.Final.jar
    hibernate-validator-5.4.1.Final.jar

    需要验证的数据

        @Pattern(regexp = "^\\w+([-_.]?\\w+)*@\\w+([\\.-]?\\w+)*(\\.\\w{2,6})+$")
        private String email;
    

    控制器

    public String register(@Valid UserRegDto userDto, Errors errors)
    

    相关文章

      网友评论

          本文标题:Spring学习笔记 - 第008天

          本文链接:https://www.haomeiwen.com/subject/cnvlottx.html