struts-config.xml的controller 元素

<controller>用于配置ActionServlet. 属性 描述
bufferSize 指定上载文件的输入缓冲的大小。该属性为可选项,默认值为4096.
className 指定和<controller>元素对应的配置类。默认为org.apache.struts.config.ControllerConfig.
contentType 指定响应结果的内容类型和字符编码。该属性为可选项,默认值为text/html。如果在Action和JSP网页中也设置了内容类型和字符编码,将会覆盖该设置。
locale 指定是否把Locale对象保存到当前用户的Session中。默认值为false.
processorClass 指定负责处理请求的Java类的完整类名。默认值为org.apache.struts.action.RequestProcessor。如果把此项设置为自定义的类,那么应该保证该类扩展了org.apache.struts.action.RequestProcessor类。
tempDir 指定处理文件上传的临时工作目录。如果此项没有设置,将采用Servlet容器为Web应用分配的临时工作目录。
nochache 如果为true,在响应结果中将加入特定的头参数 :Pragma, Cache-Control和Expires,防止页面被存储在客户浏览器的缓存中。默认值为false.


如果应用包含多个子应用,可以在每个字应用的Struts配置文件中配置<controller>元素。这样,尽管这些子应用共享同一个ActionServlet对象,但是他们可以使用不同的RequestProcessor类。

以下是<controller>元素的配置代码示例:

<controller contentType="text/html;charset=UTF-8" locale="true" processorClass="CustomRequestProcessor"/>


定制Struts控制器组件
在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()方法正是通过调用他们来具体的处理工作的。下表中对其中重要的几个做简单介绍:

主要处理方法

protected String processPath()
获取客户端请求的路径URI

protected ActionForm processActionForm()
获取当前请求表单所映射的ActionForm Bean

protected ActionMapping processMapping()
根据请求URI获取所需的映射信息

protected Action processActionCreate()
初始化相应的ActionBean

protected ActionForward processActionPerform()
调用Action Bean的execute()方法处理请求

protected void processForwardConfig()
处理由Action Bean的execute()方法返回的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 --------------
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值