Grails giving me 404 on controller when submitting form - forms

I have a basic controller, just like this:
class BookingController {
def periodCheck(){
}
def periodInput(){
}
Both booking/periodCheck.gsp and booking/periodInput.gsp have been created, and they work if I access typing the URL directly.
However, I have a form in periodInput.gsp that is supposed to send some data to periodCheck, and every time, when I send the form, I get a 404 error, saying that The requested resource (/hoteledison/booking/periodCheck) is not available. The form is defined like this:
<g:form action="periodCheck">
<!-- here would go the fields -->
<g:actionSubmit class="btn" value="Comprobar" />
</g:form>
What am I doing wrong? I've also tried defining allowedMethods in the controller, but that does not help.

Ok, according to this blog post, there's a difference between using actionSubmit and submitButton. For forms like mine, with just one action, I should have used the later.
Now it's working.

Related

HTTP Status 404 rest url mapping error pagination next

I have a list.gsp that displays list of items which is restful. It displays well but It gives me error when I click next or page no. My mapping is:
"/request/list/$sort?/$order?/$max?/$offset?"(controller:"request"){
action = [GET:"list"]
}
My view pagination is:
<div class="paginationlayer">
<span >
<g:paginate next="Next" prev="Back"
total="${ total }" /></span>
</div>
I tried using name url mapping like this:
name requestURL: "/list/$sort?/$order?/$max?/$offset?"{
controller = 'request'
action = 'list'
}
and out some mapping in the view like this mapping="requestURL", I even added params in the pagination, or hardcoded params like offset, max etc but still the same.
but still it gives me HTTP Status 404 when I click "next" it seems that the url loses its map and becomes something like this : http://localhost:8081/client/request/%5BGET%3Alist%5D?offset=10&max=10&order=desc
as basic as it may sound, the solution was to put action="list" to the pagination. Didn't occur to me coz all my paginations work with out it.
<span class="gadgetNumber">
<g:paginate next="Next" prev="Back"
maxsteps="0" action="list"
total="${ printRequestInstanceTotal }" /></span>
There is no need to list all possible parameters in UrlMappings.groovy: whatever you may need will still be available via the params object or via action method attributes.
Please try rewriting the url mapping as
"/request/list"(controller:"request"){
action = [GET:"list"]
}
This will probably resolve your issue and save us the (possibly considerable) effort of determining why exactly your URL mapping isn't being accepted.

grails access form attributes in controller

In grails i have a form with g:field tags like:
<g:field name="test" from="0..20"/>
I am trying to find a way how I can access the "from" attribute in my controller.
I can get the "value" attribute by using:
print params.test
I have tried:
print params.test.from
I'm sure there must be a way to do this but I can not seem to find it.
What I am wanting to achieve by this is perform validation so that the value does not go outside the the from range.
I know that this can be added in the domain, but in my situation I need to allow the user to overwrite the range constraints.
Any ideas?
By the time that code hits the browser, it is just HTML. from doesn't exist anymore. If that is being rendered into some sort of client side validation, that's not going to get submitted back to the server in a form submit.
If you explain what you are really needing to do in your question, I can provide a better answer.
You can pass the "from" values as hidden fields.
<g:hiddenField name="min" value="0" />
<g:hiddenField name="max" value="20" />
Something like that.

Struts 2.3 form with multiple submit tags with action attribute

This is something quite simple which worked perfectly with Struts 2.1.x. But we recently upgraded to 2.3.15.2 and it broke. Basically we have a form (actually, many forms) with multiple submits:
<s:form>
<s:submit action="save" />
<s:submit action="resetPassword" />
</s:form>
If I stick the action in the tag all is well. But if instead it is in the tag, I get a 404 error. And it's the same action!
I've been debugging and discovered that when you use the "action" attribute in the tag, the generated html is:
<input type="submit" name="action:save">
<input type="submit" name="action:resetPassword">
Supposedly Struts should take this "action" prefix and say "A-ha! This is an action!" and execute it. And it more or less does this. Or at least tries to. What I've discovered is that at a very low level, the DefaultActionMapper.handleSpecialParameters() method goes through all the parameters and tries to create a ParameterAction for each one, and if it's not null, it is executed. Most of the parameters produce a "null" ParameterAction, but not "action:".
In the docs I found this about ParameterAction:
Defines a parameter action prefix. This is executed when the configured prefix key is
matched in a parameter name, allowing the implementation to manipulate the action mapping
accordingly. For example, if the "action:foo" parameter name was found, and a
ParameterAction implementation was registered to handle the "action" prefix, the execute
method would be called, allowing the implementation to set the "method" value on the
ActionMapping
So what it does is set the mapping's result to a new ServletDispatcherResult with the name of the Action:
mapping.setResult(new ServletDispatcherResult(actionName));
On the other hand, when the action is specified in the s:form tag, the mapping's result is null.
So that when finally we get to Dispatcher.serviceAction():
if (mapping.getResult() != null) {
Result result = mapping.getResult();
result.execute(proxy.getInvocation());
} else {
proxy.execute();
}
So when the action is specified in the tag, proxy.execute() is called, which just calls the Action/method itself. Which is what should happen! But when the action is specified in the tag, since the mapping has a result, the proxy's invocation is passed to result.execute(), which calls ServletDispatcherResult ... and in the end, I get a 404.
This seems like a whole lot of work just to get multiple submit buttons with action attributes working. Is this a known issue for Struts 2.3? Do I need to implement a ParameterAction for the "action" prefix as stated in the docs?
EDIT
Ok, known bug, opened just a few days ago. In the meantime I can either downgrade to 2.3.15.1 or use the "method" attribute rather than the "action" attribute.
Hopefully it will be fixed soon ...
this is b/c in struts2.3.16.
Disables support for action: prefix
by default struts.mapper.action.prefix.enabled = false
set
<constant name="struts.mapper.action.prefix.enabled" value="true"/>
in struts.xml
Internal Changes of struts2-core 2.3.16
The action: and method: prefixes are be by default excluded and changed order to first check excludeParams and then acceptedParams in ParametersInterceptor
This is supposed to be in the process of being fixed for 2.3.15.3.
The specific jira is:
https://issues.apache.org/jira/browse/WW-4204

How to get the caller url in struts2?

I have a struts2 page which uses a shared action using <s:action> with executeResult="true" to add header contents to the page. However, there's some processing need to be done in the header action, which needs to retrieve the url of the actual page(aka the caller page). But if I use <s:url> within the header action's jsp, it only retrieve the url of the header action. So I would like to ask the experts here to enlighten me on how to achieve the result I want.
Thanks in advance.
I'm not sure I follow... so feel free to correct me if I'm not at all answering what you want.
But you should be able to use the <s:set> tag with the appropriate scope (probably request) before you call <s:action> to make the values you need available to the next action. See: http://struts.apache.org/2.2.1.1/docs/set.html
I would consider using the request object for this:
String referrer = request.getHeader("referer"); //referer spelling intentional
Do whatever you need with it via string manipulation.
So below is what I did to do it.
On the main page, use s:url and s:set to obtain the current url as well as parameters and save it in request scope
<s:set name="pageurl" scope="request">
<s:url includeParams="none" encode="true"/>
</s:set>
<s:set name="pageparams" scope="request" value="#parameters"/>
On the header page, you can retrieve them using below
<form action="<s:property value='#attr.pageurl'/>" >
<s:iterator value="#attr.pageparams" var="param">
<s:hidden name="%{#param.key}" value="%{#param.value}" id="_header_%{#param.key}"/>
</s:iterator>
</form>

question about CodeIgniter urls

I am using an application (a blog) written using the CodeIgniter framework and would like to search my blog from my browsers location bar by adding a string to the end of my blogs url like this:
http://mysite.com/blog/index.php/search...
As you can see in the example above I am not really sure how to format the rest of the url after the search part so I am hoping someone here might be able to point me in the right direction.
This is what the form looks like for the search box if that helps at all.
form class="searchform" action="http://mysite.com/blog/index.php/search" method="post">
<input id="searchtext" class="search_input" type="text" value="" name="searchtext">
<input type="submit" value="Search" name="Search">
</form>
Thx,
Mark
Since your form is posting to http://mysite.com/blog/index.php/search, I'm assuming this 'search' controller's default function is the one your are attempting to submit your data to. I think that the easiest way to do this would be to just grab the post data inside of the controller method you're posting to. Example:
function search()
{
$search_params = $this->input->post('search_text');
}
Then you would have whatever the user input stored as $search_params, and you can take actions from there. Am I misunderstanding what you're asking?
It seems like you're kind of discussing two different approaches. If you wanted to make a request to
mysite.com/blog/index.php/search&q=what_I_am_looking_for
This is going to call the search controllers default method (which is index by default). If you wanted to use the URL to pass parameters like that you would go to your function in the search controller and do:
print_r($this->input->get('q'));
This will print out "what_am_I_looking_for".
An easier approach in my opinion would be to:
1. Create a view called "search_view" with the HTML content you pasted above, and have the form "action" http://www.mysite.com/blog/index.php/test/search
Create a controller called "Test" that looks like the following:
class Test extends CI_Controller {
function search()
{
$search = $this->input->post('searchtext');
print_r($search);
}
public function display_search()
{
$this->load->view('search_view');
}
}
Visit http://www.mysite.com/blog/index.php/test/display_search in your browser. This should present you with the form you placed in search_view.php. Once the form is submitted, you should be sent to the search function and print out the variable $search, which will have whatever text you submitted on that form.
If this isn't what you were looking for then I am afraid I do not understand your question.