Struts 2 操作方法
是时候提出另一个愚蠢的问题了,这又增加了一长串问题。
我是 Struts 2 的新手,多年来一直使用旧的 1.X。 Struts 2 操作大致相当于 Struts 1 DispatchActions,只需添加方法(并在 struts.xml 中定义它们)。
所以,假设我有这个方法:
public String create() throws Exception {
// insert create logic here
}
我想要的是让 create() 执行双重任务。如果调用 create() 时未传递任何参数,则它返回 INPUT(并显示 JSP 表单页面),否则它将处理表单数据并返回 SUCCESS(并显示默认页面)。
我现在唯一的方法是检查请求中是否有任何值,这对我来说似乎很愚蠢。如果我可以说“如果我通过 HTTP GET 调用它,则显示表单,如果我通过 HTTP POST 调用它,则处理然后重定向到默认值”。
就像我说的,我在这里可能很愚蠢,但任何帮助将不胜感激。
Time for yet another stupid question, adding to a long line of them.
I'm a newbie with Struts 2, having spent years using old 1.X. Struts 2 actions can be roughly equivalent to Struts 1 DispatchActions, simply by adding methods (and defining them in struts.xml).
So, suppose I have this method:
public String create() throws Exception {
// insert create logic here
}
What I want is to have create() do double duty. If create() is called without being passed any parameters, it returns INPUT (and displays the JSP form page), otherwise it processes the form data and returns SUCCESS (and displays a default page).
The only way I have now is to check and see if any values are in the request, which seems silly to me. If I could say "if I call this via HTTP GET show the form, if I call this via HTTP POST, process then redirect to the default".
Like I said, I'm probably being pretty dumb here, but any help would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在寻找的是使用相同的操作来显示表单,然后(提交后)处理表单。
如果您的操作映射为“myaction”,那么您的表单应提交到“myaction!submit”(即动态方法调用,它调用您的
submit()
方法)。如果您想要强制仅由 HTTP GET 和 POST 方法(分别)调用
execute
和submit
方法,则需要创建自定义拦截器。What you are looking for is to use the same action to show a form and then (after submit) process the form.
If your action is mapped as "myaction", then your form should submit to "myaction!submit" (that's Dynamic Method Invocation, which invokes your
submit()
method).You'll need to create a custom interceptor if you want to enforce that the
execute
andsubmit
methods are invoked only by HTTP GET and POST methods (respectively).