Home >Java >javaTutorial >Learning Jakarta Struts 1.1 (3)
In the first article "Introduction to Jakarta Struts", I roughly analyzed the Struts framework, discussed the functions it can complete, and also browsed the various components that make up Struts. In the second article "Learning Jakarta Struts", I began to describe in detail the process steps of how to use Struts to build a simple application. This article will show you how to display the text information in the applicationResource file in the jsp page through Struts tags.
The Action class is the bridge connecting the Struts architecture and the business logic code in the application. So you should make the Action class as small and simple as possible, because the logic processing in real applications should be completed by a separate logic layer. If you are developing an n-tier application, you certainly want the interfaces between the layers to be as simple as possible. In fact, the main method "perform()" in the Action class (execute() in 1.1) somewhat implies that something should be done in this method. We know that each Action class needs to continue from org.apache.struts.action.Action. In small applications, our Action class is probably enough to continue org.apache.struts.action.Action; in some specific complex applications, I summarized it from the Action class we implemented. Some common features. Therefore, in my opinion, construct a base class to implement the code of these common features, so that all Action classes used in the application do not directly continue org.apache.struts.action.Action, but continue this to complete some common features. The base class to achieve code reuse is a pretty good design. I applied this method in StrutsSample and constructed such a base class. The methods of this base class can be used in Action classes that complete complex logic and simply forward requests.
package com.oreilly.actions;
import java.io.IOException;
import java.util.PRoperties;
import java.util.ResourceBundle;
import java.util.MissingResourceException;
import java.util.Enumeration;
import java.util.Properties;
import java.rmi.RemoteException;
import javax.ejb.EJBHome;
import javax.ejb.CreateException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org. apache.struts.action.ActionServlet;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
This class is used When developing Struts, all Action classes must inherit the base class. It takes into account some of the things that are most likely to be used in practical applications. As far as this article is concerned, some methods in the class that are not closely related to Struts will only be commented and not fully implemented. If you are engaged in development work, if you are interested, please complete these methods and apply this class. It will speed up your development in actual projects. Note that because all Action classes inherit from org.apache.struts.action.Action, our class is the same.
public abstract class AbstStrutsActionBase extends Action {
/ * Define some recorded in struts-config.xml
* forward flags that can be used in global applications */
protected static final String SUCCESS = "success";
protected static final String FAILURE = "failure";
protected static final String ERROR = "error";
protected static final String LOGIN = "login";
protected static final String CONFIRM = "confirm";
protected Context jndiContext = null;
/**
*Default construction method
*/
public AbstStrutsActionBase() {
}
/**
The following method of finding EJB instances will not be fully implemented.
Generally speaking, Action classes should call EJB session beans (or just ordinary JavaBeans) that implement the application's business logic. In large projects, developers must draw clear boundaries between layers. In the Action class, we should get an instance of the environment containing JNDI information, and then query and obtain its home interface through the EJB's JNDI name. The process is not simple, so the code snippet below is just a small example that gives the necessary implementation.
Parameter type String, the name of the JNDI to be queried is passed in
Return type Object, that is, the found home interface
If the search fails, a NamingException exception will be thrown
If the resource information fails to be obtained, a MissingResourceException exception will be thrown
*/
public Object lookup(String jndiName)
throws NamingException, MissingResourceException {
// 为调用EJB对象,通过构建记录JNDI信息的Properties对象
// 来获得初始环境信息
if (jndiContext == null) {
ResourceBundle resource =
ResourceBundle.getBundle("strutssample.properties");
Properties properties = new Properties();
properties.setProperty(
Context.INITIAL_CONTEXT_FACTORY,
resource.getString(Context.INITIAL_CONTEXT_FACTORY));
properties.setProperty(
Context.PROVIDER_URL,
resource.getString(Context.PROVIDER_URL));
properties.setProperty(
Context.SECURITY_PRINCipAL,
resource.getString(Context.SECURITY_PRINCIPAL));
properties.setProperty(
Context.SECURITY_CREDENTIALS,
resource.getString(Context.SECURITY_CREDENTIALS));
jndiContext = new InitialContext(properties);
}
注重:在真正的产品中,我们应该在此处考虑代码的健壮性,将代码加入到try/catch块内,并记录所有错误或重要信息到系统log中。而本例中,我们仅仅把异常往外抛,并假定一定会找到EJB对象的home接口并返回。
return (jndiContext.lookup(jndiName));
}
由于Action类将是由Struts来调用的。所以它的主要方法应该是一个抽象方法,而由每个继续的子类来具体实现,或者在其中做一些所有Action都会做的通用机制,例如记录log信息。在本例中,我们一切从简,将其抽象之。
参数mapping:其类型为ActionMapping,将在本Action做跳转选择用
参数actionForm:由Struts根据本次HTTP请求数据填充完成的ActionForm对象(可选,假如存在请求数据的话)
参数request:此Action所有处理的本次HTTP请求(对象)
参数response:此Action输出数据所要用到的HTTP响应(对象)
假如有I/O错误出现,则本方法抛出IOException异常
假如处理时发生servlet异常,则本方法抛出ServletException异常
本方法处理完请求后按照处理逻辑返回相应的页面导向(对象)
public abstract ActionForward perform(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException;
}
或者让这个抽象方法更有用一点,那就在里面干点什么吧,比如像下面这样在其中记录log。
{
ActionForward forward = null;
// 只是简单的记录一些提示信息到servlet log
getServlet().log(
"AbstStrutsActionBase.perform() [Action Class: "
+ this.getClass().getName()
+ " ]");
getServlet().log(
"AbstStrutsActionBase.perform() [Form Class : "
+ (form == null ? "null" : form.getClass().getName())
+ " ]");
}
然后,我们再编写的每个Action类都应该从AbstStrutsActionBase继续,并依照处理逻辑编写各自的perform方法。让我们用LoginAction为例,看看具体应该怎么应用吧。
package com.oreilly.actions;
import java.io.IOException;
import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import com.oreilly.forms.LoginForm;
/*
LoginAction 将演示一个Action将如何被Struts架构所调用
在这个例子中,我们只是简单的演示perform方法是如何调用、执行并返回的
*/
public class LoginAction extends AbstStrutsActionBase {
The next step is to verify the user. There is no specific implementation in this example. But a typical application solution is to call JavaBean or EJB to complete. The lookup method used to find the EJB (completed in the base class) should be called in this method, which returns an interface to authenticate the user against the backend database.
Parameter type String, user name to be verified
The above is the content of learning Jakarta Struts 1.1 (3). For more related articles, please pay attention to the PHP Chinese website (www.php.cn)!