2009-06-15 92 views
6

在Struts 1.x中是否存在可配置的方式,因此我的操作类只能在HTTP'POST'上执行。仅在Struts 1.x中将HTTP请求限制为'POST'

我明白我可以在我的动作类中使用request.getMethod(),然后以此为基础进行一定的“东西”。

问候, 乔纳森

+0

如果有人发送GET,你会发生什么? – skaffman 2009-06-15 14:57:01

+0

这只是一个简单的数据捕获应用程序,如果它是GET,我们不会将数据保存到数据库。我只是想检查是否有以编程方式以外的xml“可配置”方式。 – Jonathan 2009-06-15 16:03:56

回答

3

下面是与观念,既一些方案和配置解决方案。您可以创建自定义的ActionMapping ...

public class YourPOSTRequiredActionMapping extends ActionMapping { } 

...在你的Struts应用配置为仅是POST的映射。

<action path="/your/path" type="YourAction" className="YourPOSTRequiredActionMapping" /> 

然后,您可以扩展支柱的RequestProcessor并覆盖processMapping

public class YourRequestProcessor extends RequestProcessor { 
    protected ActionMapping processMapping(HttpServletRequest request, HttpServletResponse response, String path) throws IOException { 
     ActionMapping mapping = super.processMapping(request, response, path); 
     if (mapping instanceof YourPOSTRequiredActionMapping) { 
      if (!request.getMethod().equals("POST")) { 
       mapping = null; 
      } 
     } 
     return mapping; 
    } 
} 

确保配置您的支柱配置使用YourRequestProcessor。

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

我基于这个旧的工作代码,但我甚至没有编译上面的示例代码。这样在不改变应用程序的

2

一种方式是写一个拒绝非POST请求的Servlet过滤器。然后,您可以将此过滤器插入到您的web.xml文件中,并配置其url-patterns以匹配您的Struts控制器的路径。

5

您可以使用web.xml定义访问权限。这个限制可以防止GET请求:

<security-constraint> 
    <web-resource-collection> 
     <web-resource-name>struts action servlet</web-resource-name> 
     <url-pattern>*.do</url-pattern> 
     <http-method>GET</http-method> 
    </web-resource-collection> 
    <auth-constraint> 
     <!-- no one! --> 
    </auth-constraint> 
    </security-constraint> 
0

麦克道威尔的答案远远不能接受,除非你有一些特定的要求。你应该得到一个503 HTTP错误,你可以捕获它以向用户显示有意义的信息,或者将它留给实际的错误管理。