java struts2中通过GET或POST控制动作方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7338782/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Control action method by GET or POST in struts2
提问by dimo414
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?
所以我的问题是,如果是 GET 请求,有没有办法告诉 struts2 调用一个方法,如果是 POST 请求,则调用另一种方法?
回答by Steven Benitez
Struts2 doesn't offer what you described out of the box. 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.
Struts2 不提供您所描述的开箱即用的内容。如果您想强制特定操作方法只能由某些 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. 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.
在您的表单中,您将提交到 ProcessData!submit。这 !将操作与操作方法名称分开。它提供了您已有的东西,但您不需要在 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).
在成功发布后重定向用户完全使这一点无效。查看“发布后重定向”或“发布/重定向/获取”模式)。
回答by Dave Newton
Not by default, no. IMO the cleanest solution is to tweak the method name via an interceptor that looks at the request type. For example, I had a simple one that looked for executeGet and executePost methods.
不是默认的,没有。IMO 最干净的解决方案是通过查看请求类型的拦截器来调整方法名称。例如,我有一个简单的方法来查找 executeGet 和 executePost 方法。
Whether or not it's a great idea... different issue.
这是否是一个好主意......不同的问题。