定制Struts控制器组件

来源:互联网 发布:淘宝买苹果电脑里小麦 编辑:程序博客网 时间:2024/04/27 15:58
在Struts API中,org.apache.struts.action.RequestProcessor类真正包含了Struts控制器在处理servlet请求时所遵循的控制逻辑。控制器核心组件ActionServlet就是通过调用RequestProcessor对象的process()方法来委托其处理客户端请求的,该方法格式如下:

public void process(

javax.servlet.http.HttpServletRequest request,

javax.servlet.http.HttpServletResponse response)

   throws java.io.IOException, javax.servlet.ServletException{ }


    RequestProcessor类中还定义了多个processXXX()方法,process()方法正是通过调用他们来具体的处理工作的。下表中对其中重要的几个做简单介绍:

    表1 RequestProcessor类的主要处理方法

     

     

protected String processPath()

获取客户端请求的路径URI

protected ActionForm processActionForm()

获取当前请求表单所映射的ActionForm Bean

protected ActionMapping processMapping()

根据请求URI获取所需的映射信息

protected Action processActionCreate()

初始化相应的ActionBean

protected ActionForward processActionPerform()

调用Action Beanexecute()方法处理请求

protected void processForwardConfig()

处理由Action Beanexecute()方法返回的ActionForward对象。


    如果要定制ActionServlet的行为规则,其实应从RequestProcessor这个RequestProcessor类着手。要开发自己的RequestProcessor类以实现定制的控制逻辑,应遵循以下步骤:
    1) 创建一个子类继承org.apache.struts.action.RequestProcessor类,在该子类中显式定义(或使用缺省的)无参、方法体为空的构造方法。
    2) 重写所需要的方法,加入定制功能。
    3) 将该子类编译后得到的class文件保存到Struts应用程序的WEB-INF/class/目录下
    4) 修改配置文件struts-config.xml,在其中加入一个名为<controller>的元素,用以指定客户定制的RequestProcessor类。
    在展示一个具体的实现定制功能的例子之前,有必要介绍一下RequestProcessor类中定义的另外几个有关方法:
 protected void log(java.lang.String message){…}
    功能:将参数String对象message的内容存入当前应用程序日志文件。
 protected void log(java.lang.String message, java.lang.Throwable exception) {…}
    功能:将参数String对象message和异常对象exception所封装的信息存入当前应用程序日志文件。
 protected boolean processPreprocess(javax.servlet.http.HttpServletRequest request,
  javax.servlet.http.HttpServletResponse response) {…}
    功能:专门用于在子类中被重写,加入由开发者定制的预处理功能。
    在RequestProcessor类中定义的processPreprocess()方法什么也不做,只是简单返回一个boolean类型值true,用以告知RequestProcessor继续后续处理程序。在定制RequestProcessor类时通常会重写此方法,注意一定要在方法的结尾返回true,如果重写如果返回值为false,则RequestProcessor对象终止对请求的处理,将控制权送回给ActionServlet的doPost()或doGet()方法。

    下面给出用户定制RequestProcessor组件的具体实现步骤:
    1)为已有的Struts应用程序创建用户自己的RequestProcessor类,重写其中的processPreprocess()方法,加入所需的控制逻辑。范例源代码如下:

源文件:MyRequestProcessor.java

package test;

import java.util.Enumeration;

import javax.servlet.http.*;

import org.apache.struts.action.RequestProcessor;

 

public class MyRequestProcessor extends RequestProcessor {

     public MyRequestProcessor() {} 

     public boolean processPreprocess(HttpServletRequest request,

            HttpServletResponse response) {               

            log("-------------- My Logging Start--------------");

            log("Request URI = " + request.getRequestURI());

            log("Context Path = " + request.getContextPath());                         

            Enumeration headerNames = request.getHeaderNames();

            log("Request Header:");

            while (headerNames.hasMoreElements()) {

                   String headerName =(String)headerNames.nextElement();

                   Enumeration headerValues =  request.getHeaders(headerName);

                   while (headerValues.hasMoreElements()) {

                          String headerValue =(String)headerValues.nextElement();

                          log("/t" + headerName + " = " + headerValue);

                   }                  

            }

            log("Locale = " + request.getLocale());

            log("Method = " + request.getMethod());

            log("Path Info = " + request.getPathInfo());

            log("Protocol = " + request.getProtocol());

            log("Remote Address = " + request.getRemoteAddr());

            log("Remote Host = " + request.getRemoteHost());

            log("Remote User = " + request.getRemoteUser());

            log("Requested Session Id = "

                   + request.getRequestedSessionId());

            log("Scheme = " + request.getScheme());

            log("Server Name = " + request.getServerName());

            log("Server Port = " + request.getServerPort());

            log("Servlet Path = " + request.getServletPath());

            log("Secure = " + request.isSecure());

            log("-------------- My Logging End  --------------");

            return true;

     }

}


    2)编译源文件MyRequestProcessor.java,将所生成的字节码文件(包括所在的package子目录)保存到Struts 应用程序的"WEB-INF/classes/"目录下。
    3)修改此Struts应用程序配置文件struts-config.xml,在其中加入<controller>元素:

    源文件:struts-config.xml

<?xml version="1.0" encoding="ISO-8859-1" ?>

<!DOCTYPE struts-config PUBLIC

     "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"

     "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">

<struts-config>

   ……

     <controller processorClass="test.MyRequestProcessor" /> 

</struts-config>


    说明:其他组成页面、类设计和应用程序配置均不需做改变。
    4)发布Struts应用程序,并通过客户端浏览器访问该应用程序。
    5)打开Web服务器日志文件<WAS_HOME>/logs/localhost_log.<当天日期>.txt,可以看到新加入的用户定制信息:
    2004-09-24 00:30:28 StandardContext[/myStrutsApp3]action:
-------------- My Logging Start--------------
 Request URI = /myStrutsApp3/regist.do
 Context Path = /myStrutsApp3
 Request Header:
         accept = image/gif, image/x-xbitmap, image/jpeg, image/pjpeg,
    application/vnd.ms-powerpoint, application/vnd.ms-excel,
    application/msword , application/x-shockwave-flash, */*
         referer = http://localhost:8080/myStrutsApp3/
         accept-language = zh-cn
         content-type = application/x-www-form-urlencoded
         accept-encoding = gzip, deflate
         user-agent = Mozilla/4.0 (compatible; MSIE 6.0;
     Windows NT 5.1; .NET CLR 1.1.4322)
         host = localhost:8080
         content-length = 20
         connection = Keep-Alive
         cache-control = no-cache
         cookie = JSESSIONID=FD8A69F16E329A362DB219596897D6DA
 Locale = zh_CN
 Method = POST
 Path Info = null
 Protocol = HTTP/1.1
 Remote Address = 127.0.0.1
 Remote Host = 127.0.0.1
 Remote User = null
 Requested Session Id = FD8A69F16E329A362DB219596897D6DA
 Scheme = http
 Server Name = localhost
 Server Port = 8080
 Servlet Path = /regist.do
 Secure = false
 -------------- My Logging End  --------------
原创粉丝点击