且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用命名约定插件拒绝直接访问 Struts2 中的 JSP 文件

更新时间:2023-12-03 10:08:34

你想怎么禁用这个功能.

Here how d'you want to disable this feature.

创建一个虚拟 bean:

package com.struts.handler;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.Result;
import com.opensymphony.xwork2.UnknownHandler;
import com.opensymphony.xwork2.XWorkException;
import com.opensymphony.xwork2.config.entities.ActionConfig;

/**
 * Created by Roman C on 22.03.2015.
 */
public class MyUnknownHandler implements UnknownHandler {
  @Override
  public ActionConfig handleUnknownAction(String namespace, String actionName) throws XWorkException {
    return null;
  }

  @Override
  public Result handleUnknownResult(ActionContext actionContext, String actionName, ActionConfig actionConfig, String resultCode) throws XWorkException {
    return null;
  }

  @Override
  public Object handleUnknownActionMethod(Object action, String methodName) throws NoSuchMethodException {
    return null;
  }
}

然后在struts.xml中配置:

Then configure it in the struts.xml:

  <bean type="com.opensymphony.xwork2.UnknownHandler" name="handler" class="com.struts.handler.MyUnknownHandler"/>
  <unknown-handler-stack>
    <unknown-handler-ref name="handler"/>
  </unknown-handler-stack>

解释这里:

上面提到的约定插件以及它创建的配置还放置了一个未知的处理程序,该处理程序应该处理不存在配置的 URL(即不是由约定创建的).这就是问题的根源.

The convention plugin along with configuration it creates mentioned above also put an unknown handler which should handle URLs for which a configuration is not exist (i.e. not created by the convention). This is the source of the problem.


现在放置您自己的处理程序将禁用约定的处理程序.因此它将不再按惯例处理结果.