I am trying to make a simple creation of account and when create button is clicked it should perform the action "create" but I get an error saying that
HTTP Status 404 - "/WEB-INF/grails-app/views/users/create.gsp" not found.
Here is my code block for the index.gsp
<!-- CREATEFORM -->
<div id="id02" class="modal">
<g:form class="modal-content animate" controller="users" action="create">
<div class="imgcontainer">
<span onclick="document.getElementById('id02').style.display='none'" class="close" title="Close Modal">×</span>
<div class="container" style="text-align:center">
<b><h style="font-family: Arial, Helvetica, sans-serif; font-size: 30px">CREATE AN ACCOUNT</h></b><br/>
<input type="text" placeholder="Enter Username" name="uname" required/>
<input type="password" placeholder="Enter Password" name="psw" required/>
<input type="text" placeholder="First Name" name="firstName" required/>
<input type="text" placeholder="Last Name" name="lastName" required/>
<input type="text" placeholder="Age" name="age" required/>
<br/>
<input type="date" placeholder="Birth Date" name="birthdate" required/>
<br/>
<input type="text" placeholder="Student Number" name="studno" required/><br/>
<label>
<input type="checkbox" checked="checked" name="remember"> Remember me</input>
</label>
<br/>
<button type="submit" style="width: 100px; margin-right:10px;" >Create</input>
<button type="button" onclick="document.getElementById('id02').style.display='none'" class="cancelbtn">Cancel</button>
</div>
</div>
</g:form>
</div>
and Here is my code block for the usersController.
class usersController {
def index(){}
def create()
{
new Users(userid:params.uname,password:params.psw).save()
new UserInfo(studentno:params.studno,age:params.age,birth_date:params.birthdate,first_name:params.firstName,last_name:params.lastName,user_id:params.uname).save()
}
}
And here's the error
HTTP Status 404 - "/WEB-INF/grails-app/views/users/create.gsp" not found.
Because you are saving data using create action but after saving data there is no redirect option or create.gsp available. So grails will look first is there any redirect option available and try to redirect create.gsp
Check bellow code changes which will save user id to userInfo table and redirect to index page
So change your usersController
class usersController {
def index() {}
def create() {
def user = new Users(userid: params.uname, password: params.psw).save()
def userInfo = new UserInfo(studentno: params.studno, age: params.age, birth_date: params.birthdate, first_name: params.firstName, last_name: params.lastName, user_id: user.id).save()
if (user && userInfo) {
flash.message = "User created successfully"
redirect action: 'index'
} else {
flash.message = "Problem in user creation"
redirect action: 'index'
}
}
}
And add bellow code to your index.gsp
${flash.message}
There are many ways to redirect to pages. You can use render, redirect, chain, forward ..etc
Please check grails documentation for more information
In Grails for action (non gsp) you need to render something to the client,
otherwise it executes everything inside that action, but returns 404 gsp not found response, if there is no render / redirection statement it consider that action as a gsp but it is not gsp actually, therefore it responds 404.
You could render something data like list / JSON or simple string as follows.
def create() {
//your business logic is here
render "operation performed successfully"
}
First of all what grails version do you use? If the latest one (3.3.4) so this link might be helpful.
Speaking shortly: if you don't specify what to render explicitly - grails tries to find the view to display the result of action execution as:
grails-app/views/<controllerName>/<actionName>.gsp
Looks like you don't have one.
UPD
There are several ways to render action's output in grails controller.
Define explicit content as:
def myAction() {
...
render "Hello World!"
}
That will result in "white screen" with "Hello world!" text in top-left corner.
Another option is to create a gsp view in the conventional location as grails-app/views//.gsp - it will automatically be rendered after action execution if no specifying the render method invocation (like in the sample you've given)
The third option is to specify the model map:
def myAction() {
...
[param1key: param1value, param2key: param2value, ...]
}
The model will be taken into account when rendering your gsp view.
Please read the documentation I gave link to. It's incredibly helpful.
Related
I have a controller, edit method paint the user info in the form file. But
when I try to update with errors the command object return userCommand object so I lost all info from user object.
Is there any way to use both in the same form? or what I'm missing here.
UserController class
class UserController {
def edit(User user) {
respond user
}
def update(UserCommand userCommand) {
log.debug "Update a User"
if (userCommand.hasErrors()) {
respond userCommand.errors, view: 'edit'
return
}
}
}
_form.gsp file
<g:form action="update">
<div class="form-group">
<label for="firstName">User First Name</label>
<input class="form-control" name="firstName" value="${user?.firstName}">
</div>
<div class="form-group">
<label for="lastName">User Last Name</label>
<input class="form-control" name="lastName" value="${user?.lastName}">
</div>
</g:form>
You should use UserCommand on both actions parameter and create your User object inside the action from UserCommand by data binding. This is to separate your View Model from the actual Model.
Like other users stated, return the whole UserCommand instead of UserCommand.errors. You can access UserCommand.errors in the view also.
Remember, that after your UserCommand is valid, you still need to create User and validate it and return any errors it has. Both can be run through <g:renderErrors bean="${user}"> for example.
Thank you #tuomas-valtonen. You drive me to find the solution.
def edit(User user) {
UserCommand userCommand = new UserCommand()
bindData(userCommand, user)
respond userCommand
}
I have a problem with $_POST. I am adding data to my mysql database with this code and it is working.;
if (isset($_POST["d_kayit"])){
$denetci=$dbpdo->prepare("INSERT INTO denetciler(name,pass) VALUES(:name, :pass)");
$denetci->bindParam(":name",$_POST["denad"],PDO::PARAM_STR);
$denetci->bindParam(":pass",$_POST["sif"],PDO::PARAM_STR);
$denetci->execute();
}
But in the same form i want to use $_POST["denad"] for another insert. It is giving me "Notice: Undefined index: denad in" error. Sample code that giving error is;
if (isset($_POST["add"]))
{
echo "Person: ".$_POST["denad"];
}
Can you help me please?
If this:
if (isset($_POST["add"]))
{
echo "Person: ".$_POST["denad"];
}
is yielding notice: Undefined index: denad then you probably don't have inputs "add" and "denad" on the same form. Or it is an unchecked checkbox.
Edit based on the code of your HTML form. You'll need something like this, you can't end your form with </form> like you did until you included all needed input fields:
<form action="" method="post">
<!-- content here -->
<input name="denad" id="denad" type="text" style="margin-top:2px; width:200px; height:30px;"></input>
<input type="submit" class="get_file" id="K_ekle" name="add" onclick="test()" value="Kişiye Ekle" style="float:left;"></input>
<!-- more content here -->
</form>
I'm trying to get the results from a g:uploadForm action I have in my GSP file. I need to upload the file, then after it's successfully uploaded I need to tell if the upload was successful, then display another area in the GSP file.
<g:uploadForm action="save" method="post">
<h1>
<g:message code="upload"/>
</h1>
<h5>
<g:message code="upload.message"/>
</h5>
<br/>
<input type="file" id="Upload" name="Upload" class="input"/>
<br/>
<g:formButton type="submit" buttonClass="btn-primary btn-lg" buttonTextCode="upload.button" />
</g:uploadForm>
I just need something to say if it was successful or not.
Is this something I need to handle in the controller and just post to the GSP after that? I'm new to grails and groovy.
It's pretty common to use flash scoped variables for these types of messages. In fact, if you look at the Grails documentation about uploading files you will see it does just that.
def upload() {
def f = request.getFile('myFile')
if (f.empty) {
flash.message = 'file cannot be empty'
render(view: 'uploadForm')
return
}
f.transferTo(new File('/some/local/dir/myfile.txt'))
redirect(render: 'uploadForm')
}
Using the above example you could then include the following in your uploadForm GSP page.
${flash.message} to display this.
Here is my Grails (2.3.6) controller:
class WidgetController {
def index() {
render(
view: "createNew",
model:[
]
)
}
def execute() {
println "Executing form submission!"
redirect(action: "listAll")
}
def listAll() {
// Does some stuff
}
}
The index URL is, say, http://localhost:8080/myapp/widget. The idea is that when someone goes to this URL, they are presented with an HTML form. When they fill out the form, they are sent (on the server side) to the execute() method, which does some heavy duty stuff and then redirects them to the listAll() method which does some final stuff and renders a web page for them to see.
Here is the HTML form on the createNew.gsp (rendered from the index() method:
<g:form name="create-new-form" url="[action:'execute',controller:'widget']">
<table class="pure-table pure-table-bordered">
<tr>
<td class="row-header">Fizz:</td>
<td><g:textField id="app-fizz" name="fizz" /></td>
</tr>
<tr>
<td class="row-header">Buzz:</td>
<td><g:textField id="app-buzz" name="buzz" /></td>
</tr>
</table>
<g:actionSubmit value="Create" />
</g:form>
When I go to this URL and submit the form (clicking the Create button) I get redirected to http://localhost/myapp/widget/execute which displays one of my customized error pages (basically a "Sorry this page is unavailable"-type error.
Additionally, in the log outputs, my println stating "Executing form submission!" is not firing. This tells me that I don't have something wired correctly: Grails is trying to redirect to an /execute URL but somehow isn't linking that URL with my controller's execute() method. Ideas?
Try with:
<g:actionSubmit action="execute" value="Create" />
If you specify only value for g:actionSubmit it creates button with this label and also redirect to action based on this value. If action name is different than button label you should specify action and value attributes. Take a look at documentation.
Note that if you use g:actionSubmit then action attribute of g:form will be ignored (which you specified btw.). You'll find more info where it may be useful in docs linked above.
use plain <input type="submit" value="go"/>. thus the form is submitted to the URI defined in <g:form> tag
g.actionSubmit or g.submitButton are needed, if you want to submit your form somewhere ELSE.
I have created a very simple form for signing up a user
<g:form name="signupForm" url="[controller:'users', action:'signup']">
<g:textField name="username" placeholder="Username" />
<g:passwordField name="password" placeholder="Password" />
<g:textField name="email" placeholder="Email" />
<g:actionSubmit class="right" value="Signup" action="update" />
</g:form>
When I click the submit button I get a 404 error The requested resource is not available. However, if I navigate to the exact same URL manually (or even just select the address bar on the 404 error page and press enter) then it works!
My controller looks like this, it's very simple.
class UsersController {
def signup() {
render "Hello World"
}
}
Sorry if this is a noob question, but I've looked all over the Grails docs and can't figure out why this is happening. Any help much appreciated. Thanks.
The g:actionSubmit has the parameter action="update" which will push it to the UsersController def update which isn't there so it will throw a 404.
You can remove the action="update" or add that action to the controller.
http://grails.org/doc/latest/ref/Tags/actionSubmit.html
There is also a g:submitButton you can use instead.
http://grails.org/doc/latest/ref/Tags/submitButton.html