struts中ActionServlet中的解释

来源:百度知道 编辑:UC知道 时间:2024/06/01 16:32:57
这是struts中ActionServlet中程序,下面程序中//**********//之间的那段程序是什么意思,初学java,最好每句都详细的解释解释,求各位高手帮帮忙

public void service(ServletRequest arg0, ServletResponse arg1)
throws ServletException, IOException {
// TODO Auto-generated method stub
HttpServletRequest request = (HttpServletRequest)arg0;
HttpServletResponse response = (HttpServletResponse)arg1;
ConfigModal modal = ConfigLoader.mvc_config.get(this.getPath(request.getRequestURI()));
ActionForm form = modal.getFormtype();
Field far[] = form.getClass().getDeclaredFields();
for(Field f:far)
{
f.setAccessible(true);
try {
f.set(form, request.getParameter(f.getName()));
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
f.setAccessible(false);

不知道你对struts框架了解得怎么样,要是都熟悉这个框架的设计的话,还得懂得反射,xml文档知识。打标号之间的程序主要是根据页面的请求获得需要执行此次请求的方法逻辑,然后把执行结果返回给前台:
请求数据封装在from模型中
Error error = new Error();//****************************//
if(modal.getValidate().equals("true"))
{
error = form.validate();
}
通过上面对form中的数据进行验证。
if(error.iserror())
{
response.getWriter().println(modal.getInput());
}
上面是验证失败返回modal.getInput()这个页面。
String method = "execute";
if(modal.getParameter()!=null&&request.getParameter(modal.getParameter())!=null)
{
method = request.getParameter(modal.getParameter());
}
上面程序是根据xml中action的配置文件找到需要处理此次请求的方法名字--method。

Action action = modal.getAction();
try {
Method m = action.getClass().getDeclaredMethod(method, new Class[]{ActionForm.class,ActionMapping.class,HttpServletRequest.class,HttpServletResponse.class});
String message = (String) m.invoke(action, new Object[]{form,modal.getAc