接口类型的Action属性

发布于 2024-08-26 03:14:05 字数 778 浏览 3 评论 0原文

据我了解,操作的本质是可以通过请求参数值推送属性。而且,一个很棒的功能是 Struts2 允许您直接根据 Class 类型属性填充参数值;)

假设存在一个 Action 和属性类,如下所示, Action 类扩展了 ActionSupport { 用户用户;

   @Action(value="hello" {@result=(.......)})
   public void execute() {
      ........
   }
    .....
   public void setUser(User user) {
     this.user = user;
   }
   public User getUser() {
     return this.user;
   }
}

class User {
   String name;
    .....
   public void setName(String name) {
     this.name = name;
   }
   public String getName() {
     return this.name;
   }
}

您可以通过这样做来填充 User 类属性。

http://...../hello.action?user.name=John or via jsp page

然后,我意识到实际上有人将 Action 属性作为 Interface 类型。我的问题是这背后的原因是什么。如果有一个示例代码来演示那就太好了。

提前致谢!

With my understading, the nature of a Action is that properties can be pushed w/ request parameter values. And, one wonderful feature is that Struts2 allows you to directly populate parameter values against Class type property ;)

Assuming there exists a Action and property class as below,
class Action extends ActionSupport {
User user;

   @Action(value="hello" {@result=(.......)})
   public void execute() {
      ........
   }
    .....
   public void setUser(User user) {
     this.user = user;
   }
   public User getUser() {
     return this.user;
   }
}

class User {
   String name;
    .....
   public void setName(String name) {
     this.name = name;
   }
   public String getName() {
     return this.name;
   }
}

you could populate User class property by doing like this.

http://...../hello.action?user.name=John or via jsp page

Then, I realize that there are actually people make an Action property as a Interface type. My question is what is the reason behind this. If there is a sample code demonstrating it will be great.

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

余厌 2024-09-02 03:14:05

抱歉,您的问题没有多大意义。
澄清一下:

  • “属性”:在 Java 中,类的“属性”通常是通过 getter/setter 方法(setXXX() / getXXX() => property XXX)访问的(但不一定)对应于私有字段。

  • 在 Struts2 中,您有一个 Action 对象,并且通常(不一定,并不总是)从请求中填充(设置)属性(通过“参数”拦截器),然后在视图阶段从 JSP 读取(或无论如何)页面。
    因此,在您的示例中,对于请求 http://.. .../hello.action?user.name=John ,Struts2 会尝试在您的操作中(...实际上在您的值堆栈中)查找属性“user”,该属性具有属性“ name”,并尝试设置它(如果类型是可转换的)。也就是说,他会尝试调用类似 yourAction.getUser().setName("John") 的内容。 Struts2 不知道(不关心)属性“User”或“Name”是什么类型,即使它们是否是真实字段。 (不过,它们应该像“bean”一样运行:即它们应该有一个默认构造函数)。

  • 为什么以及何时应该编写接口而不是具体的类,任何 Java 书中都会对此进行解释,这只是一个标准的良好实践,并且有大量关于它的页面。与Struts2无关。在这种情况下,对于操作,人们通常只对某些“服务”字段感兴趣,这些对象通常是长期存在的(可能是单例),而不是由操作本身实例化(也不是由请求实例化!)。因此,这些接口不是我们在这里考虑的属性,它们(通常)不会公开公开,并且通常不会填充或从客户端读取。

Sorry, but your question does not make much sense.
To clarify:

  • "Properties": in Java a "property" of a class is something that is accesible via getter/setters method (setXXX() / getXXX() => property XXX), tipically (but not necessarily) corresponds to a private field.

  • In Struts2 you have an Action object and typically (not necessarily, not always) the properties are populated (set) from the request (via the "Parameters" interceptor), and later in the view stage read from the JSP (or whatever) page.
    So, in your example, for the request http://...../hello.action?user.name=John , Struts2 would try to find in your action (...actually in your value stack) a property "user" which has a property "name", and try to set it (if the types are convertible). That is, he would try to call something like yourAction.getUser().setName("John") . Struts2 does not know -does not care- what type are the properties "User" or "Name", even if they are real fields or not. (They are expected to behaviour as "beans", though: i.e. they should have a default constructor).

  • Why and when you should code interfaces instead of concrete classes is something that is explained in any Java book, it's just a standard good practice and there are tons of pages about it. It has nothing to do with Struts2. In this context, for an Action, one is tipically only interested in doing so for some "service" fields, objects that are typically long-lived (perhaps singletons), are not instantiated by the action itself (nor by the request!). So, those interfaces are NOT the properties we are considering here, they ( usually ) are not exposed publically and usually are not populated nor read from the client.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文