开发者

Action property of interface type

开发者 https://www.devze.com 2022-12-24 10:16 出处:网络
With my understading, the nature of a Actionis that properties can be pushed w/ request parameter values.And, one wonderful feature is that Struts2 allows you to directly populate parameter values aga

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 Actio开发者_如何转开发nSupport { 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!


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号