搭建spring springMvc配置文件
1.创建web项目
2.导入jar包
3.配置web.xml配置文件
1.配置监听器,加载spring配置文件
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
org.springframework.web.context.ContextLoaderListener
** 2.全局参数,指定spring配置文件路径**
<!-- 初始化参数 指定路径 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
3.配置前端控制器
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 初始化参数 指定路径 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<!-- 自启动,服务器启动时加载 -->
<load-on-startup>1</load-on-startup>
springmvc/
4.配置中文乱码过滤器
<!-- 统一解决中文乱码问题的过滤器 -->
<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>
4.配置spring的配置文件 appcationContext.xml
1.注解扫描(service,dao)
<context:component-scan base-package="dao,service"></context:component-scan>
2.加载jdbc属性配置文件
<context:property-placeholder location="classpath:jdbc.properties"/>
3.创建数据源对象bean
<!-- 数据源DataSource的bean:获取连接对象:从配置文件中获取连接参数的值-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClass}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.userName}"></property>
<property name="password" value="${jdbc.userPwd}"></property>
</bean>
4.创建jdbcTemplate模板对象bean
<!-- 创建JDBC模板对象的bean 来自于spring-jdbc.jar提供-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"
autowire="byName">
</bean>
5.配置springmvc配置文件
1.注解扫描(controller)
<context:component-scan
base-package="web.controller"></context:component-scan>
2.注解驱动
<!-- 注解驱动 -->
<mvc:annotation-driven></mvc:annotation-driven>
3.视图解析器
<!-- 视图解析器 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 针对view添加前缀和后缀 ,页面默认:/web-inf下 -->
<property name="prefix" value="WEB-INF/page/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
4.释放静态资源
<mvc:resources location="/js/" mapping="/js/**"></mvc:resources>