站内搜索: 请输入搜索关键词

当前页面: 开发资料首页J2EE 专题参数找方法是怎么实现的呢?

参数找方法是怎么实现的呢?

摘要: 参数找方法是怎么实现的呢?


通过url中的一个参数method=add就可找到相对象的add方法了,这样怎么实现的呢?


reflect


反射


是的了,各位兄弟有没有些好代码呢?
自己写了个,总觉得速度等的问题都不行呢?


下面就是我之前做的代码,你参考一下吧。

public static void main(String args[])
{
TMain main=new TMain();
TWhiteMouse mouse=new TWhiteMouse();
Method[] method=mouse.getClass().getMethods();
for(int i=0;i {
System.out.println(method[i].getName());
System.out.println(method[i].getDeclaringClass());
System.out.println(method[i].getClass());

}
try
{
main.doIt();
} catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public void doIt() throws Exception
{
TWhiteMouse mouse=new TWhiteMouse();
Method method1=mouse.getClass().getMethod("showStr",null);
Method method2=mouse.getClass().getMethod("setName",new Class[]{String.class});
Method method3=mouse.getClass().getMethod("showName",null);
Method method4=mouse.getClass().getMethod("getName",null);

method1.invoke(mouse,null);
method2.invoke(mouse, new String[]{"哈哈"});
method3.invoke(mouse, null);

System.out.println("name:"+method4.invoke(mouse, null)+"");
}

----------------------------------------------------------------------

package refect;

public class TWhiteMouse
{
String id,name;

public String getId()
{
return id;
}

public void setId(String id)
{
this.id = id;
}

public String getName()
{
return name;
}

public void setName(String name)
{
this.name = name;
}

public void showStr()
{
System.out.println("###########");
}

public void showName()
{
System.out.println(this.name);
}
}





以前反射超慢,现在不知道快了点没有


路过,友情up...


反射慢可以解决地

贴一个我写的Servlet

package com.lmin.framework.servlet;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lmin.framework.dao.BaseDAO;
import com.lmin.framework.exception.DefaultException;
import com.lmin.framework.fenye.Pager;
import com.lmin.test.StudentDAO;

public abstract class BaseServlet extends HttpServlet{

public static Map methodMap = new HashMap();

protected boolean redirect = true; //请求转发 为false表示重定向

public void setRirect(boolean type) {
redirect = type;
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String action = request.getParameter("action");
String path = null;
try {
Config config = new Config();
config.setRequest(request);
config.setResponse(response);
path = perform(action,config);
//请求转发操作
if(redirect) {
RequestDispatcher rs = request.getRequestDispatcher(path);
rs.forward(request,response);
} else {
//请求重定向操作
response.sendRedirect(path);
}
} catch (DefaultException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}

}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}

/**
* 获得方法对象
* @param methodName
* @return
* @throws SecurityException
* @throws NoSuchMethodException
*/
protected Method getMethod(String action) throws SecurityException, NoSuchMethodException {
String key = this.getClass().getName() + "." + action;
Method method = (Method) methodMap.get(key);
if(method == null) {
synchronized(methodMap) {
Class[] clazz = new Class[1];
clazz[0] = Config.class;
method = this.getClass().getDeclaredMethod(action,clazz);
methodMap.put(key,method);
}
}
return method;
}

/**
* 执行方法操作
* @param methodName
* @return
* @throws DefaultException
*/
public String perform(String methodName, Config config) throws DefaultException {
Method method = null;
try {
method = getMethod(methodName);
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
String forward = null;
try {
Object[] objects = new Object[1];
objects[0] = config;
forward = (String) method.invoke(this,objects);
} catch (IllegalArgumentException e) {
e.printStackTrace();
throw new DefaultException(e);
} catch (IllegalAccessException e) {
e.printStackTrace();
throw new DefaultException(e);
} catch (InvocationTargetException e) {
e.printStackTrace();
throw new DefaultException(e);
}
return forward;
}

/**
* 分页查询
* @param config
* @param sqlWhere
* @param dao
*/
public void doPage(Config config, String sqlWhere, BaseDAO dao) {
String pageMethod;
pageMethod=config.getRequest().getParameter("pageMethod");
List list = new ArrayList();
if(pageMethod==null || pageMethod.equals("null")) { //第一次读取数据
int size = dao.querySize(sqlWhere);
Pager pager = new Pager(size);
list = dao.query(sqlWhere,0l,pager.getPageSize());
pager.first();
//将pager信息和sql信息存放在session中管理
//这里需要改进
//可以设置一个List来存放各个分页操作的pager对象和sql对象(将pager对象和sql对象封装为一个对象)
//List限制一个长度超过就将队首元素删除
//防止session元素控制分页信息出错或信息疯长
config.getRequest().getSession().setAttribute("pager",pager);
config.getRequest().getSession().setAttribute("sql",sqlWhere);

} else {
Pager pager = (Pager) config.getRequest().getSession().getAttribute("pager");
String sql = (String) config.getRequest().getSession().getAttribute("sql");
if("first".equals(pageMethod)) {
list = dao.query(sql,0l,pager.getPageSize());
pager.first();
} else if("previous".equals(pageMethod)) {
pager.previous();
list = dao.query(sql,pager.getStartRow(),pager.getPageSize());
} else if("next".equals(pageMethod)) {
pager.next();
list = dao.query(sql,pager.getStartRow(),pager.getPageSize());
} else if("last".equals(pageMethod)) {
pager.last();
list = dao.query(sql,pager.getStartRow(),pager.getPageSize());
}
}
config.getRequest().setAttribute("result",list);
}

}



//Config对象

package com.lmin.framework.servlet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Config {

public HttpServletRequest request;

public HttpServletResponse response;

public HttpServletRequest getRequest() {
return request;
}

public void setRequest(HttpServletRequest request) {
this.request = request;
}

public HttpServletResponse getResponse() {
return response;
}

public void setResponse(HttpServletResponse response) {
this.response = response;
}

}



第一次执行是基于反射
以后就是直接从内存里根据方法名来调该方法执行了

用户的servlet需要先继承该BaseServlet


谢谢了各位..........


↑返回目录
前一篇: 用weblogic如何发布web Application???
后一篇: 高手解答!有关hibernate auto commit mode 的问题