Struts2,使用method={1}的最佳实践 [英] Struts2, best practice for using method={1}

查看:37
本文介绍了Struts2,使用method={1}的最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Struts 2 的新手,我遇到过这种语法(在教程中推荐).

I'm new to Struts 2 and I've come across this syntax (recommended in the tutorial).

<action name="Register_*" method="{1}" class="Register">
    <result name="input">/member/Register.jsp</result>
    <result type="redirectAction">Menu</result>
</action>

我知道它调用了 Register.{1} 方法.问题是用户可能会输入另一个(随机)值并导致 500 错误(这将正确记录为错误).

I understand that it calls Register.{1} method. The problem is a user could put in another (random) value and cause a 500 error (which would correctly be logged as an error).

如何防止这种情况发生?

How can this be prevented?

推荐答案

首先它不会调用 Register.{1} 方法.它会调用 Register_{1},其中 {1} 可能是一种操作类型(通常是编辑、显示 e.t.c)

First of all it won't call the Register.{1} method. It would call Register_{1} where {1} might be an action type (usually edit, show e.t.c)

意思是网址实际上是

Register_View
Register_Edit
e.t.c.

因此,如果用户手动将 URL 更改为不存在的内容,例如

So if a user manually changes the URL to something that is not there such as

Register_methodThatDoesNotExist

然后 struts 2 将返回错误.

then struts 2 will return an error.

但是为什么会出现这个问题呢?在任何使用任何技术的 Web 应用程序中,如果用户手动篡改 URL,将返回错误(也是 404)

But why is this a problem? In any web application that uses any technology if the user tampers manually with the URL an error will be returned (also the 404)

你到底想阻止什么?

更新:

为了防止 500 错误,您可以捕获所有操作(不匹配任何规则)并将它们重定向到错误页面.请参阅 struts 2 wiki 中的通配符默认"默认段落

To prevent 500 errors you can catch all actions (that do not match any rule) and redirect them in an error page. See the "Wildcard Default" default paragraph at the struts 2 wiki

http://cwiki.apache.org/WW/action-configuration.html

这个必须在struts配置的最后

This must be at the end of the struts configuration

这篇关于Struts2,使用method={1}的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆