关于找不到Spring.xml文件的问题,报500错误

关于找不到Spring.xml文件的问题,报500错误

http://img1.sycdn.imooc.com//climg/5e15756109dad49c18830958.jpg

package com.imooc.sm.global;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class DispatcherServlet extends GenericServlet {

    private ApplicationContext context;

    public void init() throws ServletException {
        super.init();
        context = new ClassPathXmlApplicationContext("spring.xml");
    }



    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;

        /*
                /staff/add.do   /login.do
                staffController
                public void add(HttpServletRequest request, HttpServletResponse response)
         */

        String path = request.getServletPath().substring(1);
        String beanName = null;
        String methodName = null;
        int index = path.indexOf('/');
        if (index != -1) {
            beanName = path.substring(0, index) + "Controller";
            methodName = path.substring(index + 1, path.indexOf(".do"));
        } else {
            beanName = "selfController";
            methodName = path.substring(0, path.indexOf(".do"));
        }
        Object obj = context.getBean(beanName);
        try {
            Method method = obj.getClass().getMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
            method.invoke(obj, request, response);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }
}

http://img1.sycdn.imooc.com//climg/5e1575e30946a95604270868.jpg

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>sm</artifactId>
        <groupId>com.imooc</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>sm_web</artifactId>
    <packaging>war</packaging>

    <name>sm_web Maven Webapp</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>


    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
    </properties>

        <dependencies>
            <dependency>
                <groupId>com.imooc</groupId>
                <artifactId>myService</artifactId>
                <version>1.0</version>
            </dependency>

            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>javax.servlet-api</artifactId>
                <version>4.0.0</version>
            </dependency>

            <dependency>
                <groupId>jstl</groupId>
                <artifactId>jstl</artifactId>
                <version>1.2</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>5.0.2.RELEASE</version>
                <scope>compile</scope>
            </dependency>
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
                <version>3.5.3</version>
                <scope>compile</scope>
            </dependency>
        </dependencies>

    <build>
        <finalName>sm_web</finalName>
        <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
            <plugins>
                <plugin>
                    <artifactId>maven-clean-plugin</artifactId>
                    <version>3.1.0</version>
                </plugin>
                <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
                <plugin>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>3.0.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.0</version>
                </plugin>
                <plugin>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.22.1</version>
                </plugin>
                <plugin>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>3.2.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-install-plugin</artifactId>
                    <version>2.5.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-deploy-plugin</artifactId>
                    <version>2.8.2</version>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>sm</artifactId>
        <groupId>com.imooc</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>sm_service</artifactId>
    <packaging>jar</packaging>

    <dependencies>
        <!-- MyBatis -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.44</version>
        </dependency>

        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.3</version>
        </dependency>

        <!-- Spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.7</version>
        </dependency>

        <!-- 事务 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.3.1</version>
        </dependency>
    </dependencies>


</project>

这是怎么回事,我关联了service模块还是不行,报同样的错,求解决!

正在回答

登陆购买课程后可参与讨论,去登陆

6回答

同学你好!

老师这里看到你的两个文件的颜色不一样了

http://img1.sycdn.imooc.com//climg/5e16cdcd098f56af02960212.jpg

你看一下你的web模块的配置是否正确

http://img1.sycdn.imooc.com//climg/5e16ce05091e115819200983.jpg

如果没有问题,建议同学重新创建一个项目,测试一下,看是否有问题。

另外建议同学修改之后,可以将最新的运行效果以及目录结构,以及修改过的内容贴出来,这样更加有利于帮助同学排查问题。

祝学习愉快~

好帮手慕柯南 2020-01-09 13:57:26

同学你好!

同学请问你是重新打war包了吗?打了之后是否重新启动项目了?如果是指重新编译,没有重新运行是没有sm_web的,老师这里也是这样,你可以运行项目试试

http://img1.sycdn.imooc.com//climg/5e16c098095b81d003200098.jpg

祝学习愉快~





  • 提问者 龙丶 #1
    全部都重新弄了,也重新运行了,还是这样
    2020-01-09 14:52:07
提问者 龙丶 2020-01-09 12:26:55

还是不行,我按照老师你给我的步骤执行,前两步都是有的,执行第三步之后在进行测试就没见到target文件夹里的sm_web了.这是怎么回事?

http://img1.sycdn.imooc.com//climg/5e16ab8409fe503b04530655.jpg

好帮手慕柯南 2020-01-09 11:46:25

同学你好!

  1. 你将编译生成的target删除,重新运行一下

    http://img1.sycdn.imooc.com//climg/5e16a09d0962387e02240124.jpg

    看一下你的项目中是否正确引入了service

    http://img1.sycdn.imooc.com//climg/5e16a0d109a6440c04170718.jpg

  2. 如果没有看你的父pom文件中是否有以下配置

    http://img1.sycdn.imooc.com//climg/5e16a1040950a98c05260342.jpg

  3. 确保service引入正确的情况下

    http://img1.sycdn.imooc.com//climg/5e16a186095cbee403610119.jpg

    重新打一下war包测试一下

    File-》Project Structure,删除

    http://img1.sycdn.imooc.com//climg/5e16a1dc09d8888207660354.jpg

    新建:重新运行测试一下

    http://img1.sycdn.imooc.com//climg/5e16a1fa09f8e62110080531.jpg


如果我的回答解决了你的疑惑,请采纳,祝学习愉快~

  • 提问者 龙丶 #1
    该引入得全部都有,但还是不行.还是报找不到spring.xml文件这个错
    2020-01-09 12:27:48
  • 提问者 龙丶 #2
    我这到底是怎么回事啊,老师,在线等呢!
    2020-01-09 13:42:59
  • 提问者 龙丶 #3
    他还是找不到spring.xml文件
    2020-01-09 13:50:05
好帮手慕柯南 2020-01-08 16:05:43

同学你好!

建议你重新编译一下项目

http://img1.sycdn.imooc.com//climg/5e158d33099b21b202600237.jpg

清除一下idea的缓存测试一下

http://img1.sycdn.imooc.com//climg/5e158d4e09c4b40502810530.jpg

祝学习愉快~


  • 提问者 龙丶 #1
    还是抱同样的错
    2020-01-09 10:27:41
好帮手慕柯南 2020-01-08 14:34:59

同学你好!

你的依赖写错了

service的artifactId是sm_service

http://img1.sycdn.imooc.com//climg/5e157769091a68c203610097.jpg

你在引入时写的是:myService

http://img1.sycdn.imooc.com//climg/5e1577af0902cbde03640104.jpg

并且你的版本写的也不对,正确的应该是:

http://img1.sycdn.imooc.com//climg/5e1577c309d8ef3205450166.jpg

如果我的回答解决了你的疑惑,请采纳,祝学习愉快~


  • 提问者 龙丶 #1
    我改好了还是一样
    2020-01-08 14:36:02
  • 提问者 龙丶 #2
    怎么回事! 还是找不到spring.xml文件
    2020-01-08 14:36:37
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师