使用idea搭建maven項目時 java目錄下的xml文件沒有加載的解決方法


今天在idea集成開發環境下 使用maven搭建了ssm項目,遇到了3個問題

首先我們先復習一下知識點:

第一步:在web.xml中配置spring監聽器

  <!-- spring監聽器  加載spring容器 -->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring/applicationContext.xml</param-value>
  </context-param>

然后創建對應的spring核心配置文件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:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
               http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
               http://www.springframework.org/schema/context
               http://www.springframework.org/schema/context/spring-context-4.0.xsd
               http://www.springframework.org/schema/tx
               http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
    <!-- 引入外部文件 -->
    <context:property-placeholder location="classpath:db.properties"/>
    <!-- 配置數據源 dbcp -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
          destroy-method="close">
        <property name="driverClassName" value="${jdbc.driver}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        <property name="maxActive" value="10"/>
        <property name="maxIdle" value="5"/>
    </bean>


    <!-- 配置sqlsessionfacory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="configLocation" value="classpath:mybatis/sqlMapConfig.xml"></property>
        <property name="dataSource" ref="dataSource"></property>
    </bean>


    <!-- 配置mapper 使用mapper掃描 自動掃描與裝配進spring容器中  id=類名首字母小寫-->
    <!--只能配置在這里  不能配置在springmvc.xml中-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="org.dao"></property>
        <!-- name屬性中只能寫成sqlSessionFactoryBeanName -->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    </bean>

    <!-- 配置事務管理器 -->
    <bean id="transactionManager"
          class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!-- 開啟注解驅動掃描 -->
    <tx:annotation-driven transaction-manager="transactionManager"/>
    
</beans>

第二步:在web.xml中配置springmvc前端控制器

  <!-- springmvc前端控制器(servlet) -->
  <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/springmvc.xml</param-value>
    </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <!-- 攔截所有的請求 / -->
    <url-pattern>*.action</url-pattern>
  </servlet-mapping>

然后創建對應的springmvc配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
               http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
               http://www.springframework.org/schema/context
               http://www.springframework.org/schema/context/spring-context-4.0.xsd
               http://www.springframework.org/schema/mvc
               http://www.springframework.org/schema/mvc/spring-mvc.xsd">


    <!-- 對於注解開發的handler可以單個進行配置 實際開發中建議使用組件掃描的方式-->
    <!--
        組件掃描 把符合條件的類加入到容器中 可以掃描@controller @service.... spring注解開發中使用過
        指定需要掃描的包 org.
        代表掃描org下的所有包 不要寫成org ,或者org.* , org.ssm.controller最准確 或者org.ssm.
         文件夾下如果有子文件 寫到子文件上個文件夾+. 如果文件夾下直接是對應的java文件  那么直接org.ssm.controller這種形式
    -->

    <!--這種方式 org.* 在eclipse中沒有報錯 在idea中會報錯-->
    <!--<context:component-scan base-package="org.*"/>-->
    <!--這個掃描寫在applicationContext.xml中 運行時 找不到路徑-->
    <context:component-scan base-package="org.controller,org.service"></context:component-scan>

    <!-- 使用mvc注解驅動代替 注解適配器和注解映射器 -->
    <mvc:annotation-driven></mvc:annotation-driven>
    <!-- 視圖解析器 解析jsp頁面  默認使用jstl表簽  classpath下得有jstl包-->
    <bean
            class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 前綴  請求時 可以省略前綴路徑 -->
        <property name="prefix" value="/WEB-INF/jsp/"></property>
        <!-- 后綴:設置轉發頁面時  可以省略.jsp -->
        <property name="suffix" value=".jsp"></property>
    </bean>

</beans>

對springmvc中注解器和映射器不熟悉的可以看一下這篇文章SpringMVC中注解和非注解方式下的映射器和適配器總結) http://blog.csdn.net/eson_15/article/details/51699103

---------------------------------

首先第一個問題就是上邊藍色背景出現的問題   相同的項目我在eclipse上配置的時候 可以寫為org.*的方式   而在idea中就不能這樣寫  否則報以下錯誤:

 

 

也不知道是為什么  所以我們需要記得:在idea中不能這樣寫

-----------------------------

第二個需要注意的問題:

先看我的目錄結構:

同樣的代碼在eclipse上可以正常運行  但是idea不行  真尼瑪坑爹啊

啟動項目  之后  前台傳遞給后台id  然后按照傳統的方式進行查詢對象  我debug了一下

可以保證:id傳遞給了controller  controller調用service  service中的mapper接口對象 也注入成功了  就是通過mapper接口對象調用方法查詢的時候  報錯了  

大致意思:無效的綁定聲明(未找到):org.dao.UserMapper.loginquery   

然后找了好久。。。最后終於找到了問題所在   大家請看我服務器啟動之后的輸出文件

可以看到xml文件沒有生成  我在網上找到了答案  參考文章:http://blog.csdn.net/ppppfly/article/details/46847299

於是  我按照上邊說的那樣  就加上了如下代碼:

<build>
    <resources>
      <resource>
        <directory>src/main/java</directory>
        <includes>
          <include>**/*.xml</include>
        </includes>
        <filtering>true</filtering>
      </resource>
    </resources>
  </build>

然后重新啟動   果然生成了xml文件

運行項目  發現這個問題正確解決

 -------------------------------------------------------

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM