学习Servlet遇到的NoSuchMethodException问题
发布时间
阅读量:
阅读量
1、今天遇到一个NoSuchMethodException问题,费了有点久的时间结局,
NoSuchMethodException 异常 从字面的意思上解释是没有整个方法导致异常
我是再写DispatcherServlet中用反射的方法传参调用类的方法报了这个异常
以下是错误代码
DispathcerServlet :类中
//从请求域中获取参数
String operate = request.getParameter("operate");
if (StringUtil.isEmpty(operate)) {
operate = "index";
}
try {
//用map容器中调用实体类对象获取声明的方法 塞入 operate HttpServletRequest.class,
//HttpServletResponse.class 类
Method method = controllerBeanObj.getClass().getDeclaredMethod(operate,
HttpServletRequest.class, HttpServletResponse.class);

此时 打了断点 查询到了参数变量有传值,但还是报了NoSuchMethodException异常错误

我回去看了调用的类的index方法 方法名称没有错误,存在这个index方法
private String index(HttpServletRequest request ) throws IOException {
request.setCharacterEncoding("UTF-8");
//保存到session作用域
//获取session
HttpSession session = request.getSession();
//定义默认pageNo = 1
Integer pageNo = 1;
String oper = request.getParameter("oper");
//如果oper!=null 说明 通过表单的查询按钮点击过来的
//如果oper是空的,说明 不是通过表单的查询按钮点击过来的
String keyword = null;
if (StringUtil.isNotEmpty(oper) && "search".equals(oper)) {
//说明是点击表单查询发送过来的请求
//此时,pageNo应该还原为1 , keyword应该从请求参数中获取
keyword = request.getParameter("keyword");
//如果keyword为空 ,执行true keyword = ""
if (StringUtil.isEmpty(keyword)) {
keyword = "";
}
//否则执行keyword塞入session域
session.setAttribute("keyword", keyword);
} else {
//说明此处不是点击表单查询发送过来的请求(比如点击下面的上一页下一页或者直接在地址栏输入网址)
//此时keyword应该从session作用域获取
//请求request域中的pageNo
String pageNoStr = request.getParameter("pageNo");
//如果不是空,将request域中的值赋值给pageNo
if (StringUtil.isNotEmpty(pageNoStr)) {
pageNo = Integer.parseInt(pageNoStr);
}
//从session域中取出键为keyword的值 类型是Object
Object keywordObj = session.getAttribute("keyword");
if (keywordObj != null) {
keyword = (String) keywordObj;
} else {
keyword = "";
}
}
//将session作用域塞入一个键值对
session.setAttribute("pageNo", pageNo);
//创建父类对象引用子类对象
FruitDAO fruitDao = new FruitDAOImpl();
//调用dao层获取所有列表的信息 封装成list集合
List<Fruit> fruitList = fruitDao.getFruitList(keyword, pageNo);
//session域中插入属性以键值对的形式 k:fruitList v:fruitList 将集合塞入session属性
session.setAttribute("fruitList", fruitList);
//总记录条数
int fruitCount = fruitDao.getFruitCount(keyword);
//总页数 计算pageCount页数 一页展示5条
//假如传进来fruitCount=5 公式就是9 / 5 = 所以展示一页
int pageCount = (fruitCount + 5 - 1) / 5;
/*
总记录条数 总页数
1 1
5 1
6 2
10 2
11 3
fruitCount (fruitCount+5-1)/5
*/
session.setAttribute("pageCount", pageCount);
//此处的视图名称是 index
//那么thymeleaf会将这个 逻辑视图名称 对应到 物理视图 名称上去
//逻辑视图名称 : index
//物理视图名称 : view-prefix + 逻辑视图名称 + view-suffix
//所以真实的视图名称是: / index .html
//调用父类的模板处理方法
// super.processTemplate("index", request, response);
return "index";
}
后来我想了下,是不是我传参的时候多了一个参数变量HttpServletResponse.class
Method method = controllerBeanObj.getClass().getDeclaredMethod(operate,
HttpServletRequest.class, HttpServletResponse.class);
调用的方法中没有这个参数变量 报了这个错误
private String index(HttpServletRequest request ) throws IOException {
果然,我修改了调用的方法,代码就好了
private String index(HttpServletRequest request,
HttpServletResponse httpServletResponse ) throws IOException {
这个问题就解决了,哎感叹一下,字面意思异常误导了我,我一开始以为真的是参数传错了,导致方法没有找到,解决了问题就很开心了。
全部评论 (0)
还没有任何评论哟~
