J-Hi对struts2与webwork的无配置文件的实现原理(一)

2014-11-24 00:45:01 · 作者: · 浏览: 2

实现方式
1、在struts.xml或xwork.xml加如下配置信息


/${proxy.config.packageName}/${proxy.method}.jsp

2、在BaseAction类中加入proxy的方法实现
private ActionProxy proxy;

public ActionProxy getProxy(){
if(proxy == null)
proxy = ActionContext.getContext().getActionInvocation().getProxy();
return proxy;
}

3、做一个JSP文件,文件名一定要与action的方法名相同,列如:a.jap那么action的方法的写法
public String a() throws Exception{

return AUTO;
}
4、在某个Jsp页面中调于这个无配置actoin的写法
actionName!a.action

分析
ActionProxy类是struts2或webwork提供的一个action代理类,它的作用是它的作用是记录当前这个action的对象、action的名称、配置信息及该action所属的包名等信息。该接口的声明如下
public interface ActionProxy {

/**
* @return the Action instance for this Proxy
*/
Object getAction();

/**
* @return the alias name this ActionProxy is mapped to
*/
String getActionName();

/**
* @return the ActionConfig this ActionProxy is built from
*/
ActionConfig getConfig();

/**
* Sets whether this ActionProxy should also execute the Result after executing the Action
*
* @param executeResult
*/
void setExecuteResult(boolean executeResult);

/**
* @return the status of whether the ActionProxy is set to execute the Result after the Action is executed
*/
boolean getExecuteResult();

/**
* @return the ActionInvocation associated with this ActionProxy
*/
ActionInvocation getInvocation();

/**
* @return the namespace the ActionConfig for this ActionProxy is mapped to
*/
String getNamespace();

/**
* Execute this ActionProxy. This will set the ActionContext from the ActionInvocation into the ActionContext
* ThreadLocal before invoking the ActionInvocation, then set the old ActionContext back into the ThreadLocal.
*
* @return the result code returned from executing the ActionInvocation
* @throws Exception
* @see ActionInvocation
*/
String execute() throws Exception;

/**
* Sets the method to execute for the action invocation. If no method is specified, the method provided by
* in the actions configuration will be used.
*
* @param method the string name of the method to invoke
*/
void setMethod(String method);

/**
* Returns the method to execute, or null if no method has been specified (meaning "execute" will be invoked)
*/
String getMethod();
}

J-Hi借用了这个代理类,在action的基类也就是BaseAction中添加了对该类实例的引用,从而实体全局配置
/${proxy.config.packageName}/${proxy.method}.jsp
其中${proxy.config.packageName}用来指定当前action所属的包名,例如,"testjs"就是配置文件的包名


class="org.hi.testjs.action.webwork.MaterialListAction">
/testjs/MaterialList.jsp


.
${proxy.method}是指调用该action的方法名
name="auto" 是我们特意为这样无配置的actoin起了一个特定的名字,也就是说

public String a() throws Exception{
return "auto";

return AUTO;
} 效果是一样的

我们特意将这段result的配置放在了中原因是省去写配置文件,只要是return "auto";就会调用这个结果。那么它的结果是什么呢?对,是一个JSP,也就是说你通过actionName!method.action后,系统会自动执行这个方法,并自动调用这个aciton所属包名下的与方法名相同的jsp文件。例如配置文件的包名为"testjs",actionName为"materialList",对应的class为"org.hi.testjs.action.webwork.MaterialListAction",你在这个action类中增加了一个a(),想通过调用该方法实现无配置调用jsp,那么你就应该将这个jsp文件放到web/testjs(与包名相同)目录下,并且该jsp的文件名为a.jsp(与方法名相同)。调用这个action方法的