运行show.jsp报404什么情况?

运行show.jsp报404什么情况?

package com.demo.sm.globe;

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();
       }
   }
}

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

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

1回答
好帮手慕阿满 2019-11-19 10:42:27

同学你好,问一下同学是直接运行show.jsp报404错误,还是测试核心控制器TestController时报404呢?报404的错误提示是什么呢?建议同学将报错信息以及项目结构web目录展开截图贴一下。

祝:学习愉快~

问题已解决,确定采纳
还有疑问,暂不采纳

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

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

0 星
SSM主流框架入门与综合项目实战2018版
  • 参与学习           人
  • 提交作业       205    份
  • 解答问题       4317    个

Java中非常实用的SSM整合开发内容,从Spring开始,到MyBaits的进阶内容,再到SpringMVC的应用,最后是SSM整合开发案例,逐步深入,助你成长为一名Java工程师!

了解课程
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

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