从另一个动作中调用一个动作,而不是返回任何简单结果 [英] Call an Action from another Action instead of returning any simple result

查看:59
本文介绍了从另一个动作中调用一个动作,而不是返回任何简单结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

def testSession = Action {
    implicit request =>
      val currentTimes = Calendar.getInstance().getTimeInMillis()
      val validTimes = Long.parseLong(request.session.get("validTill").getOrElse("0"))
      if(currentTimes>validTimes)
      {
        Ok("Session expired, Please log-in again")
      }
      else
      {
       //insert code here to call def saveDemographicDetailsBrowser = Action{}
      }

如果登录未过期,我需要在else部分中插入代码以调用其他操作.接收json并点击的操作会将数据保存在db中.

I need to insert code in else section to call some other action if login has not expired. An action that receives the json and hit save the data in db.

推荐答案

如果您尝试从另一个Action调用一个Action,那么这仅表示您的设计有误.恕我直言,在这种情况下,您应该有一个具有核心功能的方法,并且动作都应调用该方法.

If you are trying to call an action from another Action then that simply means you have a wrong design. IMHO in such cases you should have a method which does the core functionality and both the Action's should make call to that method.

无论如何,如果您想调用另一个Action,则可以重定向:

Anyways if you wish to make a call to another Action, then you could redirect:

Redirect(routes.Dashboard.homePage)

如果问题是您希望通过在请求中放入新的json,然后使用此请求对Action进行调用来对Action进行调用,那么这是严重不建议的,如果您仍然希望这样做,请查看 FakeRequest .

If the question is that you wish to make a call to Action by putting a new json in your request and then using this request to make a call to Action, then that is seriously inadvisable and still if you wish to then have a look at FakeRequest.

这篇关于从另一个动作中调用一个动作,而不是返回任何简单结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆