做项目, 尤其是基于不同业务而创建的项目, 很少有标准的项目初始化流程. 但在技术层面, 使用一个标准且可复用的方式去创建包含基本功能的工程, 是程序员的基本素养. 而且, 更主要的是, 依靠这样的方式, 可以节省大量的时间和精力.
废话不多说, 开始一个基本的 SpringMVC 项目.
开始之前, 确定已经将项目链接到 github, 并做了最基本的 maven update 或其他的排错操作.
首先, 添加依赖 – eclipse 创建基本的 Maven 项目, 编辑 pom.xml (注意, 其中 db2 的依赖需要额外花时间解决一下, 具体方法参见我另外的文档)
<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 类, 加入如下内容:
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 类
@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/");
}
}
创建基本 controller
@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;
}
}