前两天发了一个帖子“(100分)if else 很多怎么办????有什么替代的!(主要是字符串判断)”
http://topic.csdn.net/u/20090817/11/38502c58-423d-4f6d-aa27-a4b4d2780c2d.html后来改用如下的java反射机制,动态的远程方法调用,记得当年老师强调java反射机制是多么多么的好,要不是那个帖子里面有兄弟提起来,我还忘记了,现在已经觉得用反射了,但昨天测试的时候,发现有时候好像很慢,所以又过来请教一下!public class NoteMobileAction extends DispatchAction {
Logger log = Logger.getLogger(NoteMobileAction.class);
String download_url = "";
String filePath = ""; String returnCate = "p"; /**
 * 指令处理入口方法,使用了java反射机制,动态的远程调用!
 * 
 * @param mapping
 * @param form
 * @param request
 * @param response
 * @return
 * @throws IOException
 */
public ActionForward command(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws IOException { String cmd = request.getParameter("cmd").trim();// 指令名,对应CommandProcess中的方法名
/*
 * 指令列表 下载文件:cmd=download&file=product/mlns/C053.gif&uid=1
 * 注册:cmd=register 登陆:cmd=login&uid=1&psw=jh45dd
 * 修改密码:cmd=changePsw&uid=1&oldpswjh45dd&newpsw=kl8547
 * 更新个人信息:cmd=updateInfo&uid=1&psw=sixer&.....
 * 获取好友列表:cmd=getFriendList&uid=1&type=all
 */
CommandProcess commandProcess = new CommandProcess(request, response);
Method method = null;
try {
method = commandProcess.getClass().getMethod(cmd);
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
log.info("找不到名为" + cmd + "的方法!请注意大小写!" + e.getMessage());
}
try {
method.invoke(commandProcess);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
import java.io.IOException;
import java.util.HashMap;
import java.util.List;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.apache.log4j.Logger;import com.iwit.noteService.bean.User;
import com.iwit.noteService.dao.DaoFactory;
import com.iwit.noteService.util.FunctionUtil;/**
 * 命令处理模块
 * @version 创建时间:Aug 18, 2009 11:46:33 AM
 * 
 */
public class CommandProcess {
Logger log = Logger.getLogger(CommandProcess.class);
HashMap<String, String> configs = DaoFactory.getConfigDao().getAllConfig();
HttpServletRequest request;
HttpServletResponse response; public CommandProcess(HttpServletRequest request,
HttpServletResponse response) {
super();
this.request = request;
this.response = response;
} /**
 * 响应信息
 * 
 * @param msg
 *            待发送的字符串
 */
private void response(String msg) {
try {
response.getWriter().write(msg);
} catch (IOException e) {
log.info("Web响应应异常!" + e.getMessage());
}
} /**
 * 响应信息
 * 
 * @param msg
 *            待发送的字节数组
 */
private void response(byte[] msg) {
try {
response.getOutputStream().write(msg);
} catch (IOException e) {
log.info("Web响应异常!" + e.getMessage());
}
} /**
 * 数字型字符串转换为整型
 * 
 * @param numStr
 *            数字型字符串
 * @return 字符串对应的整型
 */
public int parseInt(String numStr) {
try {
return Integer.parseInt(numStr);
} catch (NumberFormatException e) {
log.info("非法的ID!" + e.getMessage());
return 0;
}
} /**
 * 
 * 下载文件
 * 
 * @return 路径
 */
public void download(/* uid,file */) {
String result = "Failure";
int uid = parseInt(request.getParameter("uid"));
result = configs.get("download_url") + request.getParameter("file");
response(result);
} /**
 * 记录产品的浏览和下载次数
 */
public void hit(/* type(click,down),pid */) { } /**
 * 用户注册
 * 
 * @return ID+PSW
 */
public void register() {
String result = "Failure";
String timmill = System.currentTimeMillis() + "";
String psw = FunctionUtil.GenerateRandomStr();
User user = new User(FunctionUtil.EncoderByMd5(psw), timmill);
if (DaoFactory.getUserDao().insertUser(user)) {
log.debug("新用户注册成功!");
user = DaoFactory.getUserDao().getNewUserByRe(timmill);
result = user.getU_id() + "+" + psw;
} else {
log.info("新用户注册失败!");
}
response(result);
} /**
 * 用户登陆
 */
public void login(/* uid,psw */) {
String result = "Failure";
int uid = Integer.parseInt(request.getParameter("uid"));
String psw = request.getParameter("psw");
User user = DaoFactory.getUserDao().getUserById(uid);
if (user.getU_psw().equals(FunctionUtil.EncoderByMd5(psw))) {
log.debug("ID:" + uid + "登陆成功!");
result = "Logined";
} else {
log.info("ID:" + uid + "登陆失败,密码错误!");
}
response(result);
} /**
 * 修改密码
 */
public void changePsw(/* uid,oldpsw,newpsw */) {
String result = "Failure";
int uid = Integer.parseInt(request.getParameter("uid"));
String oldPsw = request.getParameter("oldpsw");
User user = DaoFactory.getUserDao().getUserById(uid);
if (user.getU_psw().equals(FunctionUtil.EncoderByMd5(oldPsw))) {
String newPsw = request.getParameter("newpsw");
user.setU_psw(FunctionUtil.EncoderByMd5(newPsw));
if (DaoFactory.getUserDao().updateUser(user)) {
log.debug("ID:" + uid + ",密码修改成功!");
result = "PswChanged";
} else {
log.debug("ID:" + uid + ",密码修改失败!");
}
} else {
log.info("ID:" + uid + ",旧密码错误!");
}
response(result);
} /**
 * 修改个人信息
 */
public void updateInfo(/* uid,psw,name,sex,age,local */) {
String result = "Failure";
int uid = Integer.parseInt(request.getParameter("uid"));
String Psw = request.getParameter("psw");
User user = DaoFactory.getUserDao().getUserById(uid);
if (user.getU_psw().equals(FunctionUtil.EncoderByMd5(Psw))) {
String name = request.getParameter("name");
if (name != null && name.length() != 0) {
user.setU_name(name);
}
String sex = request.getParameter("sex");
if (sex != null && sex.length() != 0) {
user.setU_sex(sex);
}
String age = request.getParameter("age");
if (age != null && age.length() != 0) {
user.setU_age(age);
}
String local = request.getParameter("local");
if (local != null && local.length() != 0) {
user.setU_local(local);
}
if (DaoFactory.getUserDao().updateUser(user)) {
log.debug("ID:" + uid + ",个人信息修改成功!");
result = "InfoUpdated";
} else {
log.debug("ID:" + uid + ",个人信息修改失败!");
}
} else {
log.info("ID:" + uid + ",密码错误!");
}
response(result);
} /**
 * 根据好友类别返回列表
 */
public void getFriendList(/* uid,type(white,black,all) */) {
String result = "Failure";
int uid = Integer.parseInt(request.getParameter("uid"));
String friendType = request.getParameter("type");
List<User> users = null;
if ("all".equals(friendType)) {
users = DaoFactory.getUserRelateDao().getAllFriendByUser(uid);
} else if ("white".equals(friendType)) {
users = DaoFactory.getUserRelateDao().getFriendByUser(uid);
} else if ("black".equals(friendType)) {
users = DaoFactory.getUserRelateDao().getBlackFriendByUser(uid);
}
if (users != null) {
StringBuffer friendList = new StringBuffer();
for (User user : users) {
friendList.append(user.getU_id() + ":" + user.getU_name()
+ "\n");
}
result = friendList.toString();
} else {
log.info("错误的好友列表类型!");
}
response(result);
}
}

解决方案 »

  1.   

     DispatchAction 不能满足你的需要?? 还需要自己写反射!!!!
      

  2.   


    这两个想法,哪个更好?
    1.用DispatchAction,每个指令处理过程都作为一个Action方法。
    优点:直接用Action参数来区分用户的指令
    2.如上面的方法,用反射,根据用户cmd参数,通过反射来区分用户的指令