搭建spring springMvc配置文件

搭建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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值