基於註解配置的SpringMVC項目 - 標準項目初始化流程 (包含基本攔截器, 視圖解析器,靜態資源處理器)

作項目,尤爲是基於不一樣業務而建立的項目,不多有標準的項目初始化流程. 但在技術層面,使用一個標準且可複用的方式去建立包含基本功能的工程,是程序員的基本素養. 並且, 更主要的是,依靠這樣的方式,能夠節省大量的時間和精力.java

廢話很少說, 開始一個基本的SpringMVC項目.git

開始以前, 肯定已經將項目連接到github, 並作了最基本的maven update或其餘的排錯操做. 程序員

首先, 添加依賴 - eclipse建立基本的Maven項目, 編輯pom.xml (注意, 其中db2的依賴須要額外花時間解決一下, 具體方法參見我另外的文檔)github

<dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.1.9.RELEASE</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>5.1.9.RELEASE</version>
    </dependency>
    
    <dependency>
        <groupId>db2jcc</groupId>
        <artifactId>db2jcc4</artifactId>
        <version>10.1</version>
    </dependency>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
        <version>1.1.16</version>
    </dependency>
        <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>

建立基本過濾器. 新建APPInit 類, 加入以下內容:web

public class AppInit extends AbstractAnnotationConfigDispatcherServletInitializer{

    @Override
    protected Class<?>[] getRootConfigClasses() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        // TODO Auto-generated method stub
        return new Class[] {WebConfig.class};
    }

    @Override
    protected String[] getServletMappings() {
        // TODO Auto-generated method stub
        return new String[] {"/"};
    }

}

編輯 WebConfig 類spring

@Configuration
@ComponentScan("com.yzcao.controller")
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer{
    
    @Bean
    public ViewResolver viewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".jsp");
        
        return resolver;
    }
    
    @Bean(name = "dataSource")
    public DruidDataSource getDataSource() {
        try {
 
            DruidDataSource dataSource = new DruidDataSource();
            dataSource.setDriverClassName("com.ibm.db2.jcc.DB2Driver");
            dataSource.setUrl("XXX");
            dataSource.setUsername("XXX");
            dataSource.setPassword("XXX"); 
        return dataSource;
        } catch (Exception e) {
          return null;
        }
    }
    
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry
          .addResourceHandler("/resource/**")
          .addResourceLocations("/resource/"); 
    }
}

建立基本 controllermvc

@Controller
public class IndexController {

    private DataSource dataSource;
     
      private JdbcTemplate jdbcTemplateObject;
     
      @Autowired
      public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
        this.jdbcTemplateObject = new JdbcTemplate(dataSource);
      }
    
    @RequestMapping(value="/", method=RequestMethod.GET)
    public ModelAndView indexController() {
        
        ModelAndView mav = new ModelAndView("index");
//        String SQL = "INSERT INTO MESADMIN.CICTAGS (NAME, \"VALUES\") VALUES ('CAMS', '')";
//        jdbcTemplateObject.execute(SQL);
        List<Map<String, Object>> tagList = new ArrayList<>();
        String getTags = "select NAME from MESADMIN.CICTAGS";
        tagList = jdbcTemplateObject.queryForList(getTags);
        
        List<String> newList = new ArrayList<>();
        for (int i = 0; i < tagList.size(); i++) {
            System.out.println(tagList.get(i).get("NAME"));
            newList.add((String) tagList.get(i).get("NAME"));
        }
        
        
        
        mav.addObject("tags", tagList);
        
        return mav;
    }
}
相關文章
相關標籤/搜索