开发者

Scalatra - how do we do an internal redirect / forward of request

开发者 https://www.devze.com 2023-03-22 07:59 出处:网络
I want to call another internal url from my scalatra \'controller\'. I can\'t do a simple redirect, as there\'s some security s开发者_如何学运维ettings that mean a user has access only to the first ur

I want to call another internal url from my scalatra 'controller'. I can't do a simple redirect, as there's some security s开发者_如何学运维ettings that mean a user has access only to the first url.

Is there a way to do this?


get("/foo") {
  servletContext.getRequestDispatcher("/bar").forward(request, response)
} 


The get() method is defined as (similar to POST, et al):

def get(transformers : org.scalatra.RouteTransformer*)(action : => scala.Any) : org.scalatra.Route

Depends on what you mean by internal redirect, I presume you just want to execute another route's action. You have a few options of what you can do. This seems to be working for me:

val canonicalEndpoint = get("/first/route") {
  //do things in here    
}

Then you could subsequently do:

get("/second/route")( canonicalEndpoint.action )

And I think you would get your desired response.

I like saving the whole Route response of the get() as you may also want to use that with scalatra's url() function in routing.

0

精彩评论

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

关注公众号