这个是典型的命令模式和模板模式啊。看ActionServlet的doGet()和doPost(): public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
process(request, response);
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
process(request, response);
}它们都调用了process(request, response)方法。现在我们来看看这个process方法: protected void process(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
ModuleUtils.getInstance().selectModule(request, getServletContext()); ModuleConfig config = getModuleConfig(request); RequestProcessor processor = getProcessorForModule(config); if (processor == null) {
processor = getRequestProcessor(config);
} processor.process(request, response);
}注意到了吗?它调用了RequestProcessor的process方法。
RequestProcessor的process方法里面有这么一行: public void process(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
... // Call the Action instance itself
ActionForward forward = processActionPerform(request, response, action, form, mapping);
...
} protected ActionForward processActionPerform(HttpServletRequest request,
HttpServletResponse response, Action action, ActionForm form,
ActionMapping mapping)
throws IOException, ServletException {
try {
return (action.execute(mapping, form, request, response));
} catch (Exception e) {
return (processException(request, response, e, form, mapping));
}
}
注意processActionPerform 方法里面的 return (action.execute(mapping, form, request, response));现在你知道为什么你写的一个class继承了Action之后,就会自动执行execute()方法了吧
throws IOException, ServletException {
process(request, response);
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
process(request, response);
}它们都调用了process(request, response)方法。现在我们来看看这个process方法: protected void process(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
ModuleUtils.getInstance().selectModule(request, getServletContext()); ModuleConfig config = getModuleConfig(request); RequestProcessor processor = getProcessorForModule(config); if (processor == null) {
processor = getRequestProcessor(config);
} processor.process(request, response);
}注意到了吗?它调用了RequestProcessor的process方法。
RequestProcessor的process方法里面有这么一行: public void process(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
... // Call the Action instance itself
ActionForward forward = processActionPerform(request, response, action, form, mapping);
...
} protected ActionForward processActionPerform(HttpServletRequest request,
HttpServletResponse response, Action action, ActionForm form,
ActionMapping mapping)
throws IOException, ServletException {
try {
return (action.execute(mapping, form, request, response));
} catch (Exception e) {
return (processException(request, response, e, form, mapping));
}
}
注意processActionPerform 方法里面的 return (action.execute(mapping, form, request, response));现在你知道为什么你写的一个class继承了Action之后,就会自动执行execute()方法了吧
为什么有些程序中并不用加入多态照样能运行
此多态是指类似SOCKET编程中的吗
还有此句implements Serializable怎么解释
public AbstractAlbum(java.lang.String id)
{
this.setId(id);
}为什么它传递的形参要为java.lang.String
还有 public int hashCode()
{
if (this.hashValue == 0)
{
int result = 17;
int idValue = this.getId() == null ? 0 : this.getId().hashCode();
result = result * 37 + idValue;
this.hashValue = result;
}
return this.hashValue;
}
这个HASH怎么用呢
谢谢!!
“此多态是指类似SOCKET编程中的吗“:多态跟Socket没有什么必然联系。我不懂你这句话的意思。如果你是不懂什么叫做多态的话,google一下“多态”就知道了。对于为什么要使用String作为形参,单从你的代码里面我看不出必要性。所以我不知道为什么要用String。这个得看它存在的上下文环境。而且,我估计这个地方用String,也是随意的,并没有什么特别的理由。事实上,如果这个类是作为持久化类而存在的话,id一般是long,int(或者其包装类型)等。很少有人用String作为id的。最后那个hash,主要是用来唯一分别一个对象的实例的。如果你的这个对象需要放在类似于HashMap,HashSet等这样的集合里面的话,那么就必须覆盖默认的hashCode()方法,以确保不同的实例一定有一个不重复的hashcode。这个你看一下JDK文档中hashCode()的注释。