struts的controller说明,翻译Mastering jarkata struts部分

dignityliu 2003-06-10 12:31:40
The Controller
包括ActionServlet Class、Action Class、Plugins Class和Request Processer。
ActionServlet Class
org.apache.struts.action.ActionServlet继承了javax.servlet.http.HttpServlet,当然也就和HttpServlet具有相同的生存周期。包括init(), doGet(), doPost(), and destroy()方法,另外还具有一个特殊的process()方法。用于处理每一个request的映射处理。当一个ActionServlet收到一个request请求后,完成以下的步骤:
1. doPost() or doGet()收到请求,调用process()方法。
2. process()方法得到当前的RequestProcessor,然后RequestProcessor调用自身的process()方法。
3. RequestProcessor.process()方法就是处理当前请求的地方。它在struts-config.xml的<action>元素当中找到当前请求提交的路径匹配。
<html:form action="/Lookup"
name="lookupForm"
type="A.LookupForm" >
<action path="/Lookup"
type="A.LookupAction"
name="lookupForm" >
<forward name="success" path="/quote.jsp"/>
<forward name="failure" path="/index.jsp"/>
</action>
4. 当RequestProcessor.process() method 找到 <action>元素匹配的路径之后,在<form-bean>当中寻找<action>元素当中name属性匹配的属性名称。
<form-beans>
<form-bean name="lookupForm"
type="A.LookupForm"/>
</form-beans>
<action path="/Lookup"
type="A.LookupAction"
name="lookupForm" > <forward name="success" path="/quote.jsp"/>
<forward name="failure" path="/index.jsp"/>
</action>
5. RequestProcessor.process() method 在<form-bean>当中找到<action>元素当中name属性匹配的ActionForm之后,就在pool当中创建一个该ActionForm的实例,并且将提交请求中的属性一一附值。
6. 附值之后,RequestProcessor.process()方法调用该ActionForm的validate()方法校验提交数据格式。
7. RequestProcessor.process()方法实例化<action>元素的Action Class,然后调用Action.execute()方法。
8. Action.execute()方法执行结束后,返回一个ActionForward对象,这个ActionForward对象决定了这次事务的目标地址。RequestProcessor.process()方法重新获得控制,然后转向目标地址。
9. 到此ActionServlet完成了这次处理,准备接收下一次请求。
Extending the ActionServlet
Struts1.0当中只要简单的扩展,在struts1.1当中提供了RequestProcessor的扩展。
Struts-config.xml说明
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>
org.apache.struts.action.ActionServlet
</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>mapping</param-name>
<param-value>A.AActionMapping</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
The Action Class
Controller的第二个组件是org.apache.struts.action。在我们的应用程序当中应该扩展这个这个接口。下面说明了扩展这个接口应该实现的五个方法。
The execute()方法
这个方法是应用程序逻辑开始的地方,struts框架提供了两个excute()方法。一个是非HTTP请求的
public ActionForward execute(ActionMapping mapping,
ActionForm form,
ServletRequest request,
ServletResponse response)
throws IOException, ServletException
另一个是专门HTTP请求的
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
l ActionMapping包含了struts-config.xml的映射信息,决定了在这个Action处理结束之后的目标。
l ActionForm包含了本次提交请求的参数值。
另两个参数说明,同servlet,略。
Extending the Action Class
需要重载excute()方法。例如:
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
Double price = null;
// Default target to success
String target = new String("success");
if ( form != null ) {
// Use the LookupForm to get the request parameters
LookupForm lookupForm = (LookupForm)form;
String symbol = lookupForm.getSymbol();
price = getQuote(symbol);
}
// Set the target to failure
if ( price == null ) {
target = new String("failure");
}
else {
request.setAttribute("PRICE", price);
}
// Forward to the appropriate View
return (mapping.findForward(target));
}
配置如下:
<action-mappings>
<action path="/Lookup"
type="A.LookupAction"
name="lookupForm"
input="/index.jsp">
<forward name="success" path="/quote.jsp"/>
<forward name="failure" path="/index.jsp"/>
</action>
</action-mappings>
Struts Plugins
在struts1.1当中增加了org.apache.struts.action.Plugin接口,在这个接口当中,我们可以完成准备数据源和JNDI连接。必须实现init()和destroy()这两个方法。
注意在这个实现类当中一定要有一个默认的构造方法,用来确保Plugin会被ActionServlet创建。
例如:
public class APlugin implements PlugIn {
public static final String PROPERTIES = "PROPERTIES";
public APlugin() {
}
public void init(ActionServlet servlet,
ApplicationConfig applicationConfig)
throws javax.servlet.ServletException {
System.err.println("---->The Plugin is starting<----");
Properties properties = new Properties();
try {
// Build a file object referening the properties file
// to be loaded
File file =
new File("PATH TO PROPERTIES FILE");
// Create an input stream
FileInputStream fis =
new FileInputStream(file);
// load the properties
properties.load(fis);
// Get a reference to the ServletContext
ServletContext context =
servlet.getServletContext();
// Add the loaded properties to the ServletContext
// for retrieval throughout the rest of the Application
context.setAttribute(PROPERTIES, properties);
}
catch (FileNotFoundException fnfe) {
throw new ServletException(fnfe.getMessage());
}
catch (IOException ioe) {
throw new ServletException(ioe.getMessage());
}
}
public void destroy() {
// We don't have anything to clean up, so
// just log the fact that the Plugin is shutting down
System.err.println("---->The Plugin is stopping<----");
}
}
配置如下:
<plug-in className="A.APlugin"/>
The RequestProcessor
Creating a New RequestProcessor
1. 创建一个org.apache.struts.action.RequestProcessor的实现类。
2. 在这个实现类当中加上一个空的构造方法。
3. 重载方法。后面提到的重载了processPreprocess()方法。
processPreprocess()方法在每一个Action.execute()方法之前调用。
public class WileyRequestProcessor extends RequestProcessor {
public WileyRequestProcessor() {
}
public boolean processPreprocess(HttpServletRequest request,
HttpServletResponse response) {
log("----------processPreprocess Logging--------------");
log("Request URI = " + request.getRequestURI());
log("Context Path = " + request.getContextPath());
Cookie cookies[] = request.getCookies();
if (cookies != null) {
for (int i = 0; i < cookies.length; i++) {
log("Cookie = " + cookies[i].getName() + " = " +
cookies[i].getValue());
}
}
Enumeration headerNames = request.getHeaderNames();
while (headerNames.hasMoreElements()) {
String headerName =
(String) headerNames.nextElement();
Enumeration headerValues =
request.getHeaders(headerName);
while (headerValues.hasMoreElements()) {
String headerValue =
(String) headerValues.nextElement();
log("Header = " + 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("-------------------------------------------------");
return true;
}
}
配置如下:
<controller
processorClass="wiley.WileyRequestProcessor" />
...全文
88 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
TXWZ1299 2010-06-13
  • 打赏
  • 举报
回复
谢谢,很详细。不过我记得好像可以不设置controller
horse_h 2003-06-10
  • 打赏
  • 举报
回复
up
dignityliu 2003-06-10
  • 打赏
  • 举报
回复
zi ji ding
hoho2000 2003-06-10
  • 打赏
  • 举报
回复
ding
httruly 2003-06-10
  • 打赏
  • 举报
回复
写的不错!

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧