Java开发千人交流群 677807540 大家可以一起讨论技术欢迎加入
1.新建项目
2.选择web项目.
3.填写项目的group id、artifact id、version、package等信息:
4.打开pom.xml文件,切换至Dependencies标签页,选择左侧部分的Add按钮,加入webmvc项目最基本的jar包:servlet-api和spring-webmvc,如:
5.
补充springmvc的配置WEB-INF/web.xml(maven项目中是在src/main/webapp/WEB-INF下):
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="schedule-console" version="3.0">
<!-- 配置web.xml,使其具有springmvc特性,主要配置两处,一个是ContextLoaderListener,一个是DispatcherServlet -->
<!-- spring mvc 配置 -->
<!-- 1.配置DispatcherServlet表示,该工程将采用springmvc的方式。 -->
<servlet>
<servlet-name>webmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 启动项目的时候要加载的配置文件 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>webmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 2.spring配置 :配置ContextLoaderListener表示,该工程要以spring的方式启动.启动时会默认在/WEB-INF目录下查找applicationContext.xml
作为spring容器的配置文件,该文件里可以初始化一些bean-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 指定Spring Bean的配置文件所在目录。默认配置在WEB-INF目录下 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext.xml</param-value>
</context-param>
<!-- 字符集过滤器 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
6.
在src/main/resources下新建一个applicationContext.xml文件(放置事务管理信息等):
applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">
<!-- 启用spring mvc注解 -->
<context:annotation-config />
<!-- 配置读取外部配置文件 -->
<context:property-placeholder location="classpath:jdbc.properties" />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"
p:driverClassName="${jdbc.driverClassName}" p:url="${jdbc.url}"
p:username="${jdbc.username}" p:password="${jdbc.password}"
p:testOnBorrow="true" p:testWhileIdle="true" p:testOnReturn="true"
p:timeBetweenEvictionRunsMillis="1800000" p:numTestsPerEvictionRun="3" p:minEvictableIdleTimeMillis="1800000"/>
<!-- jdbcTemplate -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"
p:dataSource-ref="dataSource"/>
<!-- 配置事务管理器 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
p:dataSource-ref="dataSource" />
<!-- 事务扫描开始(开启@Tranctional) -->
<tx:annotation-driven transaction-manager="txManager" />
</beans>
7.在src/main/resources下新建一个spring-mvc.xml来配置一些springmvc的基本信息,以及jdbc的属性文件jdbc.properties:
jdbc.driverClassName=
jdbc.url=
jdbc.username=
jdbc.password=
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 基本包扫描 -->
<context:component-scan base-package="com.spring" />
<!-- 注册HandlerMapper、HandlerAdapter两个映射类,负责将请求映射到类和方法中 -->
<mvc:annotation-driven />
<!-- 访问静态资源,如js, css文件等 -->
<mvc:default-servlet-handler />
<!-- 视图解析器 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
8.在基本扫描包com.spring(spring-mvc.xml里有配置)下新建一个package:controller,并新建一个LoginController.java
package com.spring.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class LoginController {
@RequestMapping("/index")
public ModelAndView index(HttpServletRequest request, HttpServletResponse response) {
ModelAndView mav = new ModelAndView("index");
return mav;
}
}
9.在view下建一个index.jsp:
<html>
<body>
<h2>Hello World!</h2>
</body>
</html>
10.如何加入自定义的jar包,如在中央仓库下新建一个mine/batch/${version},放我们自定义的jar包batch-${version}.jar:
同时,同名的pom文件必须要有:
Xml代码
- <project>
- <modelVersion>1.0</modelVersion>
- <groupId>mine</groupId>
- <artifactId>batch</artifactId>
- <version>1.0</version>
- <!--<dependencies> 如果此jar包还依赖其它jar包则在这里加依赖
- <dependency>
- <groupId>xerces</groupId>
- <artifactId>xercesImpl</artifactId>
- <version>2.6.0</version>
- <optional>true</optional>
- </dependency>
- </dependencies>-->
- </project>
然后我们再在项目的pom文件的<dependencies />节点下加入:
Xml代码
- <dependency>
- <groupId>mine</groupId>
- <artifactId>batch</artifactId>
- <version>${batch.version}</version>
- </dependency>
其中${batch.version}是引用pom.xml下的属性节点的,这种写法方便我们以后切换版本:
Xml代码
- <properties>
- <batch.version>1.0</batch.version>
- </properties>
11:别忘记右键项目-->properties-->Deployment Assembly-->Add-->Java Build Path Entries-->Maven Dependencies:
step9:启动项目,在浏览器端输入:http://localhost:7004/springmvc/index:
step10:看一下最终最基本的结构吧~