埋心。 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 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料