新建普通maven项目:
打开idea-->new-->project-->选择maven-->选择maven模板quickstart
新建web maven项目:
打开idea-->new-->project-->选择maven-->选择maven模板webapp-->在main目录下新建文件夹java、resources-->添加servlet、jsp、jstl依赖-->编写servlet-->添加容器插件-->浏览器访问
新建spring普通maven项目:
方式一(使用ClassPathXmlApplicationContext)
打开idea-->new-->project-->选择maven-->选择maven模板quickstart-->添加spring依赖spring-core、spring-context、spring-beans、spring-test及junit-->创建bean-->创建spring配置文件-->在main方法创建ClassPathXmlApplicationContext并传入参数为配置文件地址
方式二(使用AnnotationConfigApplicationContext)
打开idea-->new-->project-->选择maven-->选择maven模板quickstart-->添加spring依赖spring-core、spring-context、spring-beans、spring-test及junit-->创建bean-->创建java配置类,放在最外层包-->在main方法创建AnnotationConfigApplicationContext并传入最外层包
新建spring mvc maven项目:
方式一(xml配置方式),代码下载:https://github.com/ChangMike/springMVC/tree/master
打开idea-->new-->project-->选择maven-->选择maven模板webapp-->添加两个依赖javax.servlet-api和spring-webmvc即可-->只配置dispatcherServlet即可,不须配置spring-->创建mvc的配置文件-->创建Controller和view
<!-- springMVC的一键式配置方法 --> <mvc:annotation-driven/> <!-- controller包扫描,如果使用了spring可以通过context:include-filter子标签来设置只扫描controller --> <context:component-scan base-package="com.test"/> <context:component-scan base-package="com.test" use-default-filter="false"> <context:include-filter type="annotation" expression="org.springframwork.stereotype.Controller"/> </context:component-scan>
编写view。如果没有指定视图解析器,SpringMVC将默认使用org.springframework.web.servlet.view.InternalResourceViewResolver,这个视图解析器就是使用jsp时使用的。默认没有前缀后缀,所以把jsp文件放到根目录即可。
方式二(java类配置),,代码下载:https://github.com/ChangMike/springMVC/tree/annotation
打开idea-->new-->project-->选择maven-->选择maven模板webapp-->添加三个依赖javax.servlet-api、spring-webmvc和spring-context-support-->只配置dispatcherServlet即可,不须配置spring-->创建java类配置mvc-->创建Controller和view