params values are always null in controller on clicking <g:actionSubmit> button

空扰寡人 提交于 2019-12-12 04:43:51

问题


In my application, I have search button and based on the search criteria, I have to search data. I have used few hidden variables to store data.

My .gsp page looks like this:

<g:form id="job" name="job" method="POST">
<div>
<input type="hidden" id="country" name="country" value="${country}"/>
<input type="hidden" id="paginationFlag" name="paginationFlag" value="${paginationFlag}"/>
<!-- There are other components like text field, select box etc --> 
<g:actionSubmit id="Search" value="Search" formmethod="post"    action="findJob"/>
</div>
</g:form>

My respective controller method looks like this:

def  findJob(){
def country
def paginationFlag

if(params?.country){
  country = params?.country
}else{
  country = 'USA'
}

if(params?.paginationFlag){
 paginationFlag = params?.paginationFlag
}else{
 paginationFlag = 'false'
}

withFormat{
 html{
  List<Applicant> searchList //get data from database.
  // other business logic
    render(view : "jobList",model:[paginationFlag: paginationFlag, country:country])
}

json{
    // some business logic
    def candidateList // value for this candidateList is acquired from database
    def json = ['jsn': candidateList]
    render json as JSON
  }
}

When I click search button and debug the code, first my control goes to controller-> findJob() method and executes the code inside html part. Secondly, it goes to the gsp page (view page) and sets value.Third, it again goes to the controller and executes the code inside json part.

On the first entry to the controller, the value of paginationFlag and country in param are both null. So it will set the value 'false' and 'USA' respectively. But when control goes to controller again for the second time, again the value of param.paginationFlag and params.country are null. Should not they have the assigned values?

Why is it so? What should I do go get the value of country and paginationFlag in params on the second time? Can any one explain me ? Thank you very much for advance.


回答1:


The problem is you're using a regular HTML input tag rather than a Grails g:hiddenField tag. Try this:

<g:form id="job" name="job" method="POST">
    <div>
    <g:hiddenField name="country" value="${country}"/>
    <g:hiddenField name="paginationFlag" value="${paginationFlag}"/>
    <!-- There are other components like text field, select box etc --> 
    <g:actionSubmit id="Search" value="Search" formmethod="post"    action="findJob"/>
    </div>
</g:form>

Tip

You can also simply the params assignment:

def country = params.country ?: 'USA'
def paginationFlag = params.paginationFlag ?: 'false'

I can't tell where the paginationFlag comes from, but know that a boolean is valid.



来源:https://stackoverflow.com/questions/33403072/params-values-are-always-null-in-controller-on-clicking-gactionsubmit-button

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!