Application.java播放框架中的重载方法

Application.java播放框架中的重载方法,java,playframework,overloading,Java,Playframework,Overloading,如何在Play项目的Application.java中设置重载方法 以下是我目前正在做的一些示例: Application.java public class Application extends Controller { public static void index() { render(); } public static void getData() { renderText("Without Parameter");

如何在Play项目的Application.java中设置重载方法

以下是我目前正在做的一些示例:

Application.java

public class Application extends Controller {

    public static void index() {
        render();
    }

    public static void getData() {
        renderText("Without Parameter");
    }

    public static void getData(String name) {
        renderText("With Parameter name = " + name);
    }
}
public class Application extends Controller {

    public static void index() {
        render();
    }

    public static void getData() {
        /** 
         *  Do some process before call getDataName method 
         *  and send the result of the process as the parameter.
         *  That's why I do this way that look like redundancy process.
        **/
        getDataName(null);
    }

    public static void getDataName(String name) {
        // Didn't use ternary operation here because will become error 'not a statement'
        if(name == null)
            renderText("Without Parameter");
        else
            renderText("With Parameter name = " + name);
    }

}
public class Application extends Controller {

    public static void index() {
        render();
    }

    public static void getData(String name, Integer age) {
        if (name == null && age == null) {
            renderText("Data null");
        } else if (name != null && age == null) {
            renderText("Name: " + name);
        } else if (name != null && age != null) {
            renderText("Name: " + name + "\n" + "Age: " + age);
        }
    }
}
路线

# Routes
# This file defines all application routes (Higher priority routes first)
# ~~~~

# Home page
GET     /                                       Application.index
GET     /data                                   Application.getData

# Ignore favicon requests
GET     /favicon.ico                            404

# Map static resources from the /app/public folder to the /public path
GET     /public/                                staticDir:public

# Catch all
*       /{controller}/{action}                  {controller}.{action}
GET     /                                       Application.index
GET     /default                                Application.getData
GET     /dataString                             Application.getDataName
GET     /data                                   Application.getData
GET     /data/{name}                            Application.getData
GET     /data/{name}/{age}                      Application.getData
测试:

  • 调用
    getData
    ,不带参数
    http://localhost:9000/data
  • 使用参数调用
    getData
    http://localhost:9000/data?name=test
  • 结果:

  • 参数名为null的
  • 参数名为test的
  • 我想要什么结果:

  • 无参数
  • 参数名为test的
  • 非常感谢你的帮助。谢谢你


    更新解决方案 以下是我根据建议所做的:

    Application.java

    public class Application extends Controller {
    
        public static void index() {
            render();
        }
    
        public static void getData() {
            renderText("Without Parameter");
        }
    
        public static void getData(String name) {
            renderText("With Parameter name = " + name);
        }
    }
    
    public class Application extends Controller {
    
        public static void index() {
            render();
        }
    
        public static void getData() {
            /** 
             *  Do some process before call getDataName method 
             *  and send the result of the process as the parameter.
             *  That's why I do this way that look like redundancy process.
            **/
            getDataName(null);
        }
    
        public static void getDataName(String name) {
            // Didn't use ternary operation here because will become error 'not a statement'
            if(name == null)
                renderText("Without Parameter");
            else
                renderText("With Parameter name = " + name);
        }
    
    }
    
    public class Application extends Controller {
    
        public static void index() {
            render();
        }
    
        public static void getData(String name, Integer age) {
            if (name == null && age == null) {
                renderText("Data null");
            } else if (name != null && age == null) {
                renderText("Name: " + name);
            } else if (name != null && age != null) {
                renderText("Name: " + name + "\n" + "Age: " + age);
            }
        }
    }
    
    路线

    # Routes
    # This file defines all application routes (Higher priority routes first)
    # ~~~~
    
    # Home page
    GET     /                                       Application.index
    GET     /data                                   Application.getData
    
    # Ignore favicon requests
    GET     /favicon.ico                            404
    
    # Map static resources from the /app/public folder to the /public path
    GET     /public/                                staticDir:public
    
    # Catch all
    *       /{controller}/{action}                  {controller}.{action}
    
    GET     /                                       Application.index
    GET     /default                                Application.getData
    GET     /dataString                             Application.getDataName
    
    GET     /data                                   Application.getData
    GET     /data/{name}                            Application.getData
    GET     /data/{name}/{age}                      Application.getData
    

    更新解决方案(2007年7月26日) Application.java

    public class Application extends Controller {
    
        public static void index() {
            render();
        }
    
        public static void getData() {
            renderText("Without Parameter");
        }
    
        public static void getData(String name) {
            renderText("With Parameter name = " + name);
        }
    }
    
    public class Application extends Controller {
    
        public static void index() {
            render();
        }
    
        public static void getData() {
            /** 
             *  Do some process before call getDataName method 
             *  and send the result of the process as the parameter.
             *  That's why I do this way that look like redundancy process.
            **/
            getDataName(null);
        }
    
        public static void getDataName(String name) {
            // Didn't use ternary operation here because will become error 'not a statement'
            if(name == null)
                renderText("Without Parameter");
            else
                renderText("With Parameter name = " + name);
        }
    
    }
    
    public class Application extends Controller {
    
        public static void index() {
            render();
        }
    
        public static void getData(String name, Integer age) {
            if (name == null && age == null) {
                renderText("Data null");
            } else if (name != null && age == null) {
                renderText("Name: " + name);
            } else if (name != null && age != null) {
                renderText("Name: " + name + "\n" + "Age: " + age);
            }
        }
    }
    
    路线

    # Routes
    # This file defines all application routes (Higher priority routes first)
    # ~~~~
    
    # Home page
    GET     /                                       Application.index
    GET     /data                                   Application.getData
    
    # Ignore favicon requests
    GET     /favicon.ico                            404
    
    # Map static resources from the /app/public folder to the /public path
    GET     /public/                                staticDir:public
    
    # Catch all
    *       /{controller}/{action}                  {controller}.{action}
    
    GET     /                                       Application.index
    GET     /default                                Application.getData
    GET     /dataString                             Application.getDataName
    
    GET     /data                                   Application.getData
    GET     /data/{name}                            Application.getData
    GET     /data/{name}/{age}                      Application.getData
    
    至于电话:

     1. http://localhost:9000/data                -> Display "Data null"
     2. http://localhost:9000/data/Crazenezz      -> Display "Name: Crazenezz"
     3. http://localhost:9000/data/Crazenezz/24   -> Display "Name: Crazenezz
                                                              Age: 24"
    

    我认为不可能像您希望的那样在routes文件中重载它

    但你可以这样做:

    public static void getData(String name) {
      if (name == null) {
        renderText("Without Parameter");
      } else {
        renderText("With Parameter name = " + name);
      }
    }
    

    我相信我想实现和你一样的目标,Crazenezz,所以首先我尝试了以下路线定义:

    GET     /terms.json                 controllers.exposedjson.TermServiceJSON.findTermsByQuery(langId:Long ?= null)
    
    。。。以及控制器中的一个方法,该方法检查参数是否为null,但该方法失败,因为
    null
    不是路由定义中的有效可选值。因此,我认为我将采用以下解决方案(到目前为止仍然有效),在JSON服务接口中公开一个字符串值,我将该值转换为控制器中的long

    GET     /terms.json                 controllers.exposedjson.TermServiceJSON.findTermsByQuery(langId:String ?= "")
    
    
    @Transactional(readOnly=true)
    public static Result findTermsByQuery(String languageId) {
        Result result;
        if (languageId == "")
            result = ok(toJson(TermService.getTerms()));
        else
            result = ok(toJson(TermService.findTermsByLanguageId(Long.parseLong(languageId.trim()))));
        return result;
    }
    

    它不是很漂亮,但从服务客户端的角度来看,它可能比使用两个单独的路径要好。

    感谢您的回答,让我们看看其他专家对此问题的看法。我尝试了许多方法,但没有找到其他可能的方法。因此,我选择使用您的样式:-)在我使用3重载方法尝试了您的建议之后,我发现参数
    langId:Long?=null
    没有真正的用处。它应该是这样写的
    langId:'5'
    ,它是静态值,如果用户请求
    /terms.json
    而不带参数,它将传递
    5
    作为默认值。如果仍然使用上述配置,则可以检查命令提示符或终端,确保
    langId:Long?=null为ignore
    。首先,我认为这是我想要的,但是经过一次又一次的测试,我得到了另一个解决方案,请参阅上面我的更新解决方案。顺便说一句,不错的尝试…:)