实现同样的功能,地址栏路径不同,出现报错
---------------核心控制器----------------------
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://localhost:8080/front/index.do,无法实现功能,并报错
请老师看看,这是什么原因?按照核心控制器的逻辑,应该两种方式都是可以实现功能的
正在回答 回答被采纳积分+1
获取路径如上,与输入地址栏路径相同
/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,使用的相同的方法,仅仅是路径不同,却产生问题,请老师再看看是哪里出现问题?
另外,项目作业已提交,如果可以,老师也可用上传源代码测试一下,实在找不出问题所在,谢谢!
- 参与学习 人
- 提交作业 323 份
- 解答问题 8263 个
本阶段将带你学习主流框架SSM,以及SpringBoot ,打通成为Java工程师的最后一公里!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星