实现同样的功能,地址栏路径不同,出现报错

实现同样的功能,地址栏路径不同,出现报错

---------------核心控制器----------------------

package com.imooc.hospital.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;

        String beanName=null;
        String methodName=null;

        String path=request.getServletPath().substring(1);
        Integer index=path.indexOf("/");

        if (path.indexOf("/")!=-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"));
        }

        System.out.println(beanName+":"+methodName);

        Object object=context.getBean(beanName);
        try {
            Method method=object.getClass().getMethod(methodName,HttpServletRequest.class,HttpServletResponse.class);
            method.invoke(object,request,response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

---------------------------Controller------------------------

package com.imooc.hospital.controller;

import com.imooc.hospital.entity.Category;
import com.imooc.hospital.entity.Department;
import com.imooc.hospital.service.CategoryService;
import com.imooc.hospital.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller("selfController")
public class SelfController {
    @Autowired
    private CategoryService categoryService;

    @Autowired
    private DepartmentService departmentService;

    public void index(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Category> categories=categoryService.getAll();
        List<Department> departments=departmentService.getAll();
        request.setAttribute("CATS",categories);
        request.setAttribute("DEPS",departments);
        request.getRequestDispatcher("../index.jsp").forward(request,response);
    }

}

地址栏输入:http://localhost:8080/front/self/index.do,正常实现功能

http://img1.sycdn.imooc.com//climg/5f0efe26097ed02514950886.jpg地址栏输入地址:http://localhost:8080/front/index.do,无法实现功能,并报错

http://img1.sycdn.imooc.com//climg/5f0efe4d09dcfc9510710467.jpghttp://img1.sycdn.imooc.com//climg/5f0efe6b09b1ac9310490514.jpg请老师看看,这是什么原因?按照核心控制器的逻辑,应该两种方式都是可以实现功能的

正在回答 回答被采纳积分+1

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

3回答
好帮手慕小班 2020-07-16 18:48:05

同学你好,测试代码,同学的使用http://localhost:8080/front/index.do不能正确跳转是因为如下的路径存在问题:

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

当使用http://localhost:8080/front/index.do路径时,不需要使用../来返回上一级目录,因为此时已经是根目录了,所以路径就会出现问题,导致不能执行,这里可以使用/表示根目录,比如:

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

对应在页面中css等内容的引用也不需要添加../来表示上一级,可以直接使用绝对路径,比如:

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

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

如上修改后:

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

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

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

提问者 csm032 2020-07-16 15:15:22

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

获取路径如上,与输入地址栏路径相同

/self/index.do路径可以实现功能,但是/index.do路径不可以实现功能

报错信息如下:

java.lang.reflect.InvocationTargetException

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.lang.reflect.Method.invoke(Method.java:498)

at com.imooc.hospital.global.DispatcherServlet.service(DispatcherServlet.java:49)

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)

at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)

at com.imooc.hospital.global.EncodingFilter.doFilter(EncodingFilter.java:19)

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)

at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)

at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)

at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:543)

at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)

at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)

at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:678)

at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)

at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)

at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:609)

at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)

at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:810)

at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1623)

at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)

at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)

at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)

at java.lang.Thread.run(Thread.java:745)

Caused by: java.lang.NullPointerException

at com.imooc.hospital.controller.SelfController.index(SelfController.java:30)

... 30 more

二者都是跳转到相同的bean,使用的相同的方法,仅仅是路径不同,却产生问题,请老师再看看是哪里出现问题?

另外,项目作业已提交,如果可以,老师也可用上传源代码测试一下,实在找不出问题所在,谢谢!

好帮手慕小班 2020-07-16 14:19:31

同学你好,1、复制测试贴出代码,拼接运行后,并没有出现同学所描述的错误,同学可以尝试输出path对应的内容,查看一下具体的地址。

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

    2、NullPointerException,空指针的异常,出现原因:如果使用null调用方法,就会报出空指针异常。同学可以根据报错定位排查一下自己哪里的对象是null,导致报出了空指针异常。

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

这里只是检查贴出代码,并没有问题,同学可以根据报错位置,再来具体定位一下,或者重启项目再来试试。

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

  • 提问者 csm032 #1
    获取路径与输入地址栏路径相同 /self/index.do路径可以实现功能,但是/index.do路径不可以实现功能
    2020-07-16 15:16:08
问题已解决,确定采纳
还有疑问,暂不采纳

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

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

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

在线咨询

领取优惠

免费试听

领取大纲

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