从中获取选定的值

时间:2015-02-15 22:49:43

标签: grails grails-2.0 grails-controller

我试图获得2个链式下拉列表。第一个显示远程服务器文件夹,选择后,第二个显示所选文件夹中的文件。接下来我想将所选文件复制到我的本地磁盘。但我无法将文件名转换为某个变量以便以后使用它。此列表是从控制器呈现的,而不是从gsp呈现的。点击此链接:http://grails.asia/grails-chained-select---load-data-on-one-dropdown-box-depending-on-another。    下拉列表呈现正常,但问题是我需要从控制器中获取从第二个下拉列表(文件名)中选择的值,并在将来使用它。请帮我解决这个问题。 域类:



package filemanager

class SourceServ {
	String sourceServName
	String sourceServFolder
	int sourceServPort
	String sourceServUser
	String sourceServPassword
	String localFolder
	Boolean selected
	static hasMany = [destservs: DestServ]
    static constraints = {
		sourceServName blank: false, nullable: false
		sourceServFolder blank: false, nullable: false
		sourceServPort blank: false, nullable: false
		sourceServUser blank: false, nullable: false
		sourceServPassword blank: false, nullable: false
		localFolder blank: false, nullable: false
		selected nullable: true
    }
	
static mapping = {
        destservs cascade: "all-delete-orphan"
    }
}




控制器:



package filemanager
import com.jcraft.jsch.*
import com.jcraft.jsch.ChannelSftp.*

class ChooseFromController {
	SourceServ sourceServ
	DestServ destServ
	String value
	String fileName
	String name
    def index(Integer max) 		{
        params.max = Math.min(max ?: 10, 100)
		model:[sourceServ: SourceServ]
    }
	//Вывод второго списка с файлами
	def sourceServChanged(long sourceServId) { 
		sourceServ = SourceServ.get(sourceServId)
		FileWork fileWork1 = new FileWork (sourceServ.getSourceServName(), sourceServ.getSourceServUser(), sourceServ.getSourceServPassword(), sourceServ.getSourceServFolder(), "C:/tmp/", sourceServ.getSourceServPort(), "null")
		Session ses
		ses = fileWork1.connect()
		def fileList = fileWork1.getFileList(ses) 
		ses.disconnect()
		render g.select(name:'fileName', 
			   from:fileList, noSelection:[null:'-Choose file to copy-'])
			   //onchange:getFileName(this.value))
		render fileName
	}
	def getFileName(input){
		render "Hello" //input //fileName
	}
	def copyToLocal(String x) {
		render fileName//sourceServ.getSourceServName()
	}
}




GSP:



<%@ page contentType="text/html;charset=UTF-8" import="filemanager.SourceServ" import="filemanager.DestServ"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/>
<meta name="layout" content="main"/>
<title>Выбор исходного сервера</title>
 <g:javascript library='jquery' />
</head>
<body>
  <div class="body">
  <%--Список исходных серверов с составным option value(то, что выводится в списке)--%>
 <b>Выберите исходный сервер и папку</b>
<div>
 <g:select id="id" name="sourceServName" from="${sourceServ.list()}" value="${id}"
 				 optionKey="id"
          		 optionValue="${{it.sourceServName+it.sourceServFolder}}" noSelection="['':'-Choose source server-']" 
          		 onchange="sourceServChanged(this.value);"/> <%--Передача id в sourceServId--%>
</div>
        <div>
            <b>Выберите файл для копирования </b>
            <span id="subContainer"></span>
        </div>
        <script>
            function sourceServChanged(sourceServId) {
                <g:remoteFunction controller="ChooseFrom" action="sourceServChanged"
                	update="subContainer"
                    params="'sourceServId='+sourceServId"/>
            }
        </script>
 </div>   
 <br>    
	<%--<g:submitButton name="copyToLocal" class="button" value="copyToLocal" />--%>   
	<g:form controller="ChooseFrom">
          <g:actionSubmit value="Submit" action="copyToLocal"/>
    </g:form>

</body>
</html>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

这里发生的是从你的javascript中将新的select元素呈现到表单之外的span“subContainer”。尝试将span标记替换为表单元素

<g:form controller="ChooseFrom">
          <span id="subContainer"></span>
          <g:actionSubmit value="Submit" action="copyToLocal"/>
</g:form>

现在,当单击提交按钮时,它会将值提交到ChooseFrom Controller中的copyToLocal操作。因此,从操作内部可以从params.fileName

访问所选值