Using certain parts of URL as params in a Grails Controller - grails

I've created a static URL in Grails:
class UrlMappings {
static mappings = {
...
"/remittance/bank"{
controller={"BankRemittance"}
action=[GET:"index"]
}
"/remittance/bank/$bankId/spd/$spdId"{
controller={"SPD"}
action=[GET:"show"]
}
...
}
}
Now what I want is to retrieve bankId and spdId values of the URL on my SPDController:
class SPDController {
def myService
def show() {
String bankId = ? // code to get bankId from the URL
String spdId = ? // code to get spdId from the URL
render(
view: "...",
model: myService.transact(bankId, spdId)
)
}
}
But how can I do that? It seems using params.bankId and params.spdId is not for this scenario.

Your action should accept the URL parameters as arguments.
def show(String bankId...)

Related

Url.RouteUrl not gettting an API route in MVC5

I created a MVC 5 site using its template and added the following API controller:
namespace MvcSite.Controllers {
public class TestController : ApiController {
[Route("test/send"), HttpGet]
public String Send() {
return "SENT";
}
} // TestController
} // MvcSite.Controllers
When I access "/test/send" I get the string "SENT" back as expected ...
In a Razor view or in a Controller I need to get the URL of the send action so I tried:
var url = Url.RouteUrl(new { controller = "Test", action = "Send", HttpRoute = true });
But url is null ... I have no idea why ...
The API route is working fine so it is in the Route Table ...
What am I missing?
Thank You,
Miguel
Configure a named route and use the HttpRouteUrl method to get the URL.
[Route("test/send", Name = "Send"), HttpGet]
public String Send() {
return "SENT";
}
Get the URL like this:
var url = Url.HttpRouteUrl("Send", new {});

Grails , How to accessing current ?lang in domain class

http://localhost:8080/app/?lang=EN
-----------------------------------------------
Domain
class Company {
String nameJp
String nameEn
static constraints = {
}
String toString(){
if(lang=='EN')
return nameEn ? nameEn:nameJp
else
return nameJp
}
}
How can i check current languages in domain class
You can try LocaleContextHolder or pass the current Locale as param:
String toString() {
return toString(LocaleContextHolder.getLocale())
}
//get the possibility of passing a Locale (better also for tests)
String toString(Locale locale) {
if(locale.language == 'en') {
return nameEn ? nameEn:nameJp
} else {
return nameJp
}
}
But this is more a presentation need than a Domain Class need, so you can create a TagLib and handle this there.
class CompanyTagLib {
static namespace = "comp"
def name = { attrs ->
Locale locale = attrs.locale ? attrs.remove('locale') : ResquestContextUtil.getLocale(request)
Company company = attrs.remove('company')
...
}
}
Not sure that you can access it in domain class, but in controller you can get it from session
session[SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME]

How to get params in a model?

How to get params in a model (in Grails) ? params. does not exist in a model.
If by "Model" you mean a Domain Model class, they don't have params, they're just POJOs. Params only apply to Controllers, as far as I know.
IF your parameter names are propertly formated and match the domain attributes, you can do this:
Assuming
params = [name:"some name", age: "194", civilState: "MARRIED"]
and
class Person{
String name
Integer age
CivilStatus civilState //some enum
}
You can leverage groovy's property binding constructor like this in a controller:
class Controller {
def saveAction = {
new Person(params)
}
}
As I understand you have a special method for filling domain from request. At this case you could pass all params (it's a Map) to such method, like:
def MyDomain {
static MyDomain buildFromParams(def params) {
return new MyDomain(
field1: params.field_1, //assuming that your params have different naming scheme so you can't use standard way suggested by Raphael
field2: params.field_2
)
}
}
class MyController {
def myAction() {
MyDomain foo = MyDomain.buildFromParams(params)
}
}

Grails 2.2.0 URLMappings: Any way to use same URL with Different Verb

I have the following:
"/api/users"(controller: "user") {
action = [GET:"list"]
}
Doing a call to http://localhost:8080/platform/users I get a list of users back. Then I added this:
"/api/users"(controller: "user") {
action = [POST:"save"]
}
And now I get a 404 and it is not hitting either method in UserController. I'd like to be able to use the same URL with the verb controlling which action. Am I doing this wrong or does Grails not support this?
From the Grails docs: URL Mappings
static mappings = {
"/product/$id"(controller:"product") {
action = [GET:"show", PUT:"update", DELETE:"delete", POST:"save"]
}
}
For your case:
"/api/users"(controller: "user") {
action = [GET:"list",POST:"save"]
}
Check your userController to see if there is allowedMethods defined accordingly like this:
class UserController {
static allowedMethods = [save: "POST", list: "GET"]
def list() {
.....
}
def save() {
.....
}
}

UrlMapping causing method not to render different view than default

class SearchController {
def list = {
List<Product> productsList = productRepository.findProductBySearchPhrase(params.searchPhrase)
render(view: "/product/list", model: [products: productsList])
}
}
class UrlMappings {
"/$controller/$action?/$id?" {
constraints {}
}
"/search" {
controller = "search"
view = "list"
constraints {}
}
}
1) This URL works properly, rendering GSP from /views/product/list directory.
myapp.com/search/list?searchPhrase=underware
2) This URL doesn't do the work, rendering /views/search/list.
myapp.com/search?searchPhrase=underware
Any ideas?
May be you want to replace 'view' with 'action' in the search URL Mapping.

Resources