埋心。 2016-02-15 02:57 采纳率: 25%
浏览 1537

输入参数之后就直接提示某某方法不存在,可我明明已经写了相对应的方法了.

protected void service(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    String methodName = request.getParameter("method");

// System.out.println(methodName+"======");
if (methodName == null || methodName.trim().isEmpty()){
throw new RuntimeException("请传入参数");
}

    Class<?> clazz = this.getClass();

// System.out.println(clazz);
Method m = null;
try {
m = clazz.getMethod(methodName, HttpServletRequest.class,HttpServletResponse.class);
System.out.println(m.getParameterCount());
} catch (Exception e) {
throw new RuntimeException("没有查询到" + methodName + ", 这个方法");
}
try {
m.invoke(this, request,response);//反射调用,相当于 this.addUser(request,response);
} catch (IllegalAccessException | IllegalArgumentException
| InvocationTargetException e) {
System.out.println("您调用的" + methodName + "方法内部出现异常");
throw new RuntimeException(e);
}
}

protected void addUser(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    System.out.println("addUser()...");
}
  • 写回答

2条回答

  • lambda-fk 2016-02-15 03:05
    关注

    clazz.getMethod 错了

    评论

报告相同问题?

悬赏问题

  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
  • ¥15 NX MCD仿真与博途通讯不了啥情况
  • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?
  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用