Maven項目的創建及SSM環境的搭建

来源:互联网 发布:json数据格式 编辑:程序博客网 时间:2024/06/05 16:27

1.搭建maven环境  这里有两个参数一个是user setting 和 local Respository   user setting 配置maven 的包下的 conifg 目录下的user setting ,注意刚解压的maven 包中的user setting 需要配置本地仓库,和第二个参数的地址相同。

2.创建maven项目 我这种创建方法只限于没有多个模块的项目,但是又多个模块的也没那么难,就是new module 创建模块,指定它的父模块就是配置下面第二张图的parent project,但是只有一个模块的不用配置partent project。

3.配置编译器

点击项目右键properties  点开之后如下图

接下来进行ssm的搭建

1.配置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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
 id="WebApp_ID" version="3.0">
 <display-name>jk</display-name>
 <welcome-file-list>
  <welcome-file>index.jsp</welcome-file>
  <welcome-file>index.html</welcome-file>
 </welcome-file-list>
 <!-- 配置編碼過濾器 -->
 <filter>
  <filter-name>codeFilter</filter-name>
  <filter-class>cn.itcast.util.CodeFilter</filter-class>
 </filter>
 <filter-mapping>
  <filter-name>codeFilter</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>
 <!-- 配置核心的servlet -->
 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:applicationContext.xml</param-value>
 </context-param>
 <servlet>
  <servlet-name>springmvc</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>classpath:spring-*.xml</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
  <servlet-name>springmvc</servlet-name>
  <url-pattern>/</url-pattern>
 </servlet-mapping>
 <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
</web-app>

2.配制applicationContextl.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:mvc="http://www.springframework.org/schema/mvc"
 xmlns:context="http://www.springframework.org/schema/context"
 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.xsd
   http://www.springframework.org/schema/mvc
   http://www.springframework.org/schema/mvc/spring-mvc.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context.xsd
   http://www.springframework.org/schema/aop
   http://www.springframework.org/schema/aop/spring-aop.xsd
   http://www.springframework.org/schema/tx
   http://www.springframework.org/schema/tx/spring-tx.xsd ">
 <context:component-scan base-package="cn.itcast.*"/>
 <context:property-placeholder location="classpath:jdbc.properties" />
 <mvc:annotation-driven/>
 <!-- 创建数据源 -->
 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
  <property name="driverClass" value="com.mysql.jdbc.Driver" />
  <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/jk" />
  <property name="user" value="root" />
  <property name="password" value="root" />
 </bean>
 <!--配置工厂 -->
 <bean id="sqlSessionFactotryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
  <property name="dataSource" ref="dataSource" />
  <property name="configLocation" value="classpath:sqlMapConfig.xml" />
  <property name="mapperLocations" value="classpath:cn.itcast.mapper.*.xml" />
 </bean>
  <!-- 配置映射扫描 -->
 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  <property name="basePackage" value="cn.itcast.mapper" />
 </bean>
</beans>

3.配制spring-mvc.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:mvc="http://www.springframework.org/schema/mvc"
 xmlns:context="http://www.springframework.org/schema/context"
 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.xsd
   http://www.springframework.org/schema/mvc
   http://www.springframework.org/schema/mvc/spring-mvc.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context.xsd
   http://www.springframework.org/schema/aop
   http://www.springframework.org/schema/aop/spring-aop.xsd
   http://www.springframework.org/schema/tx
   http://www.springframework.org/schema/tx/spring-tx.xsd ">
 <!-- 配置视图解析器 -->
 <!-- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="prefix" value="/WEB-INF/"></property>
  <property name="suffix" value=".jsp"></property>
 </bean> -->
 <!-- 配置文件上传下载 -->
 <bean id="multipartResolver"   
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">   
         
        <property name="defaultEncoding" value="utf-8" />   
         
        <property name="maxUploadSize" value="10485760000" />   
         
        <property name="maxInMemorySize" value="40960" />   
    </bean>  
    <!-- 静态资源访问 -->
    <mvc:resources location="/WEB-INF/images" mapping="/image/"/>
</beans>

4.spring-mybatis.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:mvc="http://www.springframework.org/schema/mvc"
 xmlns:context="http://www.springframework.org/schema/context"
 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.xsd
   http://www.springframework.org/schema/mvc
   http://www.springframework.org/schema/mvc/spring-mvc.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context.xsd
   http://www.springframework.org/schema/aop
   http://www.springframework.org/schema/aop/spring-aop.xsd
   http://www.springframework.org/schema/tx
   http://www.springframework.org/schema/tx/spring-tx.xsd ">
 <!-- 配置事务 -->
  <bean id="transactionManager"
   class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
   <property name="dataSource" ref="dataSource" />
  </bean>
  <!-- 配置注解驱动 -->
  <tx:annotation-driven transaction-manager="transactionManager" />
</beans>

配置過程中遇到的問題及解決方式

1.can'  find  ContextLoaderListern can't  find  或者bean is not defined ,這種錯誤一般是你真的沒定義,或者定義錯了,還有一種情況就是 你的maven dependency  沒有引用,通俗點就是jar 包沒有引入classpath 找不到。

點擊add 之後配置加入maven dependency,這個破問題,我剛開時沒有想到,廢了我好長時間

2.canl auto  private interface 這個問題也是我理解不深刻,剛開始在一個文件applicationContext.xml 中配置, 後來分文件配置,它sprngContext 的初始化是有 順序的,他先加載applicationContext 結果我沒有在spingmvc 中配置mapper的映射和掃描,還有數據源,當然找不見了,有的還跟你說springmvc 基于接口配置,要寫set 方法,其實注解不用。

3.no suit driver出現的原因,我認爲常遇到的是你的配置有問題,比如說url 寫錯了。

4.測試的時候,RequestMapping上多寫一個/,而在dispatcherServlet中配置的是/,導致404問題,配置*.action也是可以的。則不用去除requestMapping上那個多餘的/

原创粉丝点击