简体   繁体   English

在struts2中通过GET或POST控制操作方法

[英]Control action method by GET or POST in struts2

I'm new to Struts2, coming from a PHP background, where I'd often have the same file handling GET and POST requests, and processing a form if the request is a POST request. 我是Struts2的新手,来自PHP背景,我经常有相同的文件处理GET和POST请求,如果请求是POST请求,则处理表单。

I currently have the following in struts.xml: 我目前在struts.xml中有以下内容:

<action name="ProcessData" class="ProcessDataAction">
    <result name="*">processdata.jsp</result>
</action>
<action name="ProcessDataUpload" class="ProcessDataAction" method="upload">
    <result name="*">processsdata.jsp</result>
</action>

Which works fine, but it bothers me that the URL that handles POST is different, since now if the user reloads the page, they get an error rather than simply seeing the contents of the GET page. 哪个工作正常,但令我困扰的是,处理POST的URL是不同的,因为现在如果用户重新加载页面,他们会收到错误而不是简单地看到GET页面的内容。

So my question is, is there any way to tell struts2 to call one method if it's a GET request, and another method if it's a POST request? 所以我的问题是,有没有办法告诉struts2调用一个方法,如果它是一个GET请求,另一个方法,如果它是一个POST请求?

Struts2 doesn't offer what you described out of the box. Struts2没有提供您开箱即用的内容。 If you want to enforce that a particular action method is invokable only by certain HTTP methods, then you'd need to create a custom interceptor and probably a few custom annotations. 如果要强制某个特定的操作方法只能通过某些HTTP方法调用,那么您需要创建一个自定义拦截器,可能还需要一些自定义注释。

If you just want the same action to handle displaying the form and processing it, then you can do the following: 如果您只是想要相同的操作来处理显示表单并进行处理,那么您可以执行以下操作:

public class MyAction {
  public String execute() {
    return INPUT;
  }

  public void validate() {
    // perform any form validation needed
  }

  public String submit() {
    // process the form and then redirect
  }
}

In your form, you would submit to ProcessData!submit. 在您的表单中,您将提交ProcessData!submit。 The ! 的! separates the action from the action method name. 将操作与操作方法名称分开。 It provides what you have already, but you don't need to explicitly map each method in the struts.xml. 它提供了您已经拥有的内容,但您不需要显式映射struts.xml中的每个方法。

But it bothers me that the URL that handles POST is different, since now if the user reloads the page, they get an error rather than simply seeing the contents of the GET page. 但令我困扰的是,处理POST的URL是不同的,因为现在如果用户重新加载页面,他们会收到错误而不是简单地看到GET页面的内容。

Redirecting the user after a successful post completely nullifies this point. 成功发布后重定向用户将完全取消此点。 Look at the "Redirect After Post" or "Post/Redirect/Get" pattern). 查看“Post Post After Post”或“Post / Redirect / Get”模式)。

Not by default, no. 不是默认情况下,不是。 IMO the cleanest solution is to tweak the method name via an interceptor that looks at the request type. IMO最干净的解决方案是通过查看请求类型的拦截器来调整方法名称。 For example, I had a simple one that looked for executeGet and executePost methods. 例如,我有一个简单的查找executeGet和executePost方法的方法。

Whether or not it's a great idea... different issue. 这是不是一个好主意......不同的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM