java的反射问题,谁帮帮小弟啊,getMethod和invoke怎么用哦
我的基类
public abstract class ActionBase extends Action {
public abstract Object init(ActionConfig actionConfig) throws Exception;
public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException, SecurityException,
NoSuchMethodException, InvocationTargetException,Exception,
IllegalArgumentException, IllegalAccessException {
ActionForward actionForward = null;
String method = request.getParameter( "method ");
System.out.println(method);
String message = " ";
if(method == null){
method = " ";
}
//创建一个ActionConfig方法用来封装配置信息
ActionConfig actionCfg = new ActionConfig(mapping,form,request,response);
//得到当前类的对象
Class c = this.getClass();
System.out.println(c.getName());
//通过传过来的方法名(method)在它的子类中查找这个方法
Method methodName = c.getMethod(method,new Class[] {ActionConfig.class});
System.out.println(methodName.getName());
if(methodName == null){
message = "没有找到这个方法名! ";
actionCfg.getRequest().setAttribute( "message ",message);
actionCfg.getMapping().findForward( " ");
}
Object[] arg = {actionCfg};
//转入到方法名为methodName的方法中执行操作
actionForward = (ActionForward)methodName.invoke(this,arg);
return actionForward;
}
//public abstract ActionForward method(ActionConfig actionCfg);
}
封装ActionConfig信息的类:
public class ActionConfig {
private ActionMapping mapping;
private ActionForm form;
private HttpServletRequest request;
private HttpServletResponse response;
public ActionConfig(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response){
this.mapping = mapping;
this.form = form;
this.request = request;
this.response = response;
}
public ActionMapping getMapping(){
return mapping;
}
public ActionForm getForm(){
return form;
}
public HttpServletRequest getRequest(){
return request;
}
public HttpServletResponse getResponse(){
return response;
}
}
在的Action类:
public class LoginAction extends ActionBase {
com.book.util.Hibernate hbm=new com.book.util.Hibernate();
/** public ActionForward execute(ActionMapping mapping,
ActionForm actionForm,
HttpServletRequest request,
HttpServletResponse response)throws HibernateException,IOException {
public ActionForward performLogin(ActionConfig cfg) throws
HibernateException {
String target= "suss ";
System.out.println(target);
ActionErrors errors = new ActionErrors();
LoginForm loginForm = (LoginForm)cfg.getForm();
String username=loginForm.getUsername();
String psw=loginForm.getPsw();
if(!hbm.isLogin(username,psw)){
target= "fail ";
errors.add( "loginError ",new ActionError( "error.logerror "));
saveErrors(cfg.getRequest(),errors);
}
else{
HttpSession session = cfg.getRequest().getSession();
session.setAttribute( "username ", username);
}
return cfg.getMapping().findForward(target);
}
当输入...?method=performLogin 老是报错
javax.servlet.ServletException: struts.Action.RoleAction.performSyslist(struts.ActionConfig)
org.apache.struts.action.RequestProcessor.processException(RequestProcessor.java:516)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:423)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:226)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1164)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:415)
javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
root cause
java.lang.NoSuchMethodException: struts.Action.RoleAction.performSyslist(struts.ActionConfig)
java.lang.Class.getMethod(Class.java:1581)
struts.ActionBase.execute(ActionBase.java:39)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:421)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:226)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1164)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:415)
javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
[解决办法]
没找到方法 struts.Action.RoleAction.performSyslist
其他地方的问题吧
[解决办法]
用apache的common包里那个property什么,比较方便 你报的那个错肯定是method调用不到
[解决办法]
不是调用不到
是没找到
