如何使用MVC模式在控制器内部自动生成Getters和Setter

发布于 2025-01-29 11:46:24 字数 1033 浏览 2 评论 0原文

我目前正在寻找一种自动生成代码(可能是代码生成器)的方法,该代码与MVC模式兼容。

我已经在网络中看到了许多代码生成器,但它们仅适用于模型。他们将其连接到数据库,生成模型类等。但是,我正在寻找的是从我编写的模型类中创建视图和控制器类的工具。

为什么我要寻找?

如您所知,在Java的最常见IDE中,例如Eclipse,Intellij等,总是有一个代码生成器来生成属性的Getters和Setters,我们可以轻松地使用它们来封装。但是,在控制器类中,我们再次需要以这种格式的一些getters和setter:

   public void set<ModelName><PropertyName>(<PropertyType> propertyName){
      <modelObject>.set<PropertyName>(<property>);      
   }

   public <PropertyType> get<ModelName><PropertyName>(){
      return <modelObject>.get<PropertyName>();     
   }

例如:

   Course model;
   public void setCourseId(String id){
      model.setId(id);      
   }

   public String getCourseId(){
      return model.getId();     
   }

对于这些设定器和获取器,无法在这些IDE中自动生成固定器和获取器。我的目的是找到这样的工具。从另一个角度来看,我想找到一种方法来自动在另一个类(模型)的类(控制器)中生成getters和setter,而不是手工编写。

我认为像Spring这样的框架可以使用注释@controller可以执行此操作,但是我正在寻找普通的Java,没有任何框架,没有任何框架。

I am currently looking for a way to automatically generate code (may be a code generator) that is compatible with MVC Pattern.

I have seen lots of code generators in the web but they work only for models. They connect it to the database, they generate the model class etc. However, what I am looking for is a tool to create View and Controller classes from the Model class I have written.

Why I am looking for that?

As you know, in the most common IDEs for java, like Eclipse, IntelliJ etc., there are always a code generator to generate getters and setters of a property and we can use them easily to encapsulate. However , in the controller class, we again need some getters and setters that is in this format:

   public void set<ModelName><PropertyName>(<PropertyType> propertyName){
      <modelObject>.set<PropertyName>(<property>);      
   }

   public <PropertyType> get<ModelName><PropertyName>(){
      return <modelObject>.get<PropertyName>();     
   }

for example:

   Course model;
   public void setCourseId(String id){
      model.setId(id);      
   }

   public String getCourseId(){
      return model.getId();     
   }

For these setters and getters, there is no way to automatically generate setters and getters in those IDEs. My aim is to do find such a tool. In another perspective, I want to find a way to automatically generate getters and setters inside a class(controller) for another class(model), instead of writing by hand.

I think frameworks like Spring can have the ability to do this using annotation @Controller but I am looking for plain Java, without any frameworks except OSGi.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文