Playframework 在Play框架中把服务类放在哪里?
在Grails中,我们有包含从控制器调用的业务逻辑的服务类。Playframework 在Play框架中把服务类放在哪里?,playframework,Playframework,在Grails中,我们有包含从控制器调用的业务逻辑的服务类。 在Play框架项目中,我应该把服务类放在哪里?如果我在控制器中定义的方法不是请求操作,而是实用方法,例如控制器中的int findMax(int a,int b),那么这是否可以在控制器中定义,以及如何声明这些方法?对此没有规则。我个人会把实用方法放在实用类中。实用程序类和服务类应遵循正常的包规则,即com.stackoverflow.services.statistic.UsageCalculator业务逻辑通常应作为模型类上的方法
在Play框架项目中,我应该把服务类放在哪里?如果我在控制器中定义的方法不是请求操作,而是实用方法,例如控制器中的int findMax(int a,int b),那么这是否可以在控制器中定义,以及如何声明这些方法?对此没有规则。我个人会把实用方法放在实用类中。实用程序类和服务类应遵循正常的包规则,即
com.stackoverflow.services.statistic.UsageCalculator
业务逻辑通常应作为模型类上的方法实现,静态或非静态,具体取决于上下文
虽然没有关于这方面的规则,但是实用程序方法应该在包中放入它们自己的实用程序类中,或者可以是模型类的一部分,具体取决于上下文
例如,一个比较两个原语的简单实用方法,例如
findMax(int,int)
类,最好是在实用类中使用,尽管方法是findOldest(Person,Person)
更适合作为Person模型类的静态方法。您可以在app文件夹中创建包,并编写自己的服务类或逻辑类。
在应用程序控制器中使用这个新创建的服务/逻辑类及其方法
在应用程序文件夹中生成包:
例如,play.service.chiken并在此包中创建新类
package play.service.chiken;
import java.util.ArrayList;
import java.util.List;
import models.QuotesModel;
public class Utility {
public List<QuotesModel> getListOfQuotes(int itemCount) {
ArrayList<QuotesModel> list=new ArrayList<QuotesModel>(10);
for(int x=0;x<itemCount;x++) {
QuotesModel quotesModel=new QuotesModel();
quotesModel.authorName="";
quotesModel.category="";
quotesModel.bookmark="Y";
quotesModel.id=x+"";
quotesModel.content="Quotes n umber ,njdsfkhwjd jr x=" +x;
list.add(quotesModel);
}
return list;
}
}
在路由文件中:
# Home page
GET / controllers.Application.index()
GET /addbar controllers.Application.addBar()
GET /entryindb controllers.Application.entryInDB()
假设我在project_root/app下创建util文件夹,play framework会自动拾取其中定义的类吗,或者我需要在配置中添加一些东西?如果我在project_root下而不是app下创建util文件夹呢?app文件夹是所有Java源代码所在的位置,因此您的util文件夹应该放在那里。您不需要更改配置,Play将自动拾取它。这里的任何代码都会在util包中。我想知道为什么这个答案会被否决。
# Ebean configuration
# ~~~~~
# You can declare as many Ebean servers as you want.
# By convention, the default server is named `default`
#
ebean.default="models.*"
# Home page
GET / controllers.Application.index()
GET /addbar controllers.Application.addBar()
GET /entryindb controllers.Application.entryInDB()