从自动填充中获取价值

时间:2015-09-09 06:34:03

标签: autocomplete struts2

下面是代码片段,我想检索所选值,自动完成工作完全正常,

<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ taglib prefix="sx" uri="/struts-dojo-tags" %>

<html>
<head>
<script type="text/javascript">

    function sayPramod()
    {
        alert("hiii");          
    }   

</script>
<sx:head />
</head>

<body>
<h1>Struts 2 autocompleter + JSON example</h1>

<s:form  >

<s:url id="databaseList" action="databaseJSON"  
/>

<sx:autocompleter label="What's your favorite Database Server?" 
href="%{databaseList}" name="yourFavDatabase"       
/>


<s:submit value="submit" name="submit" onclick="sayPramod();" />

</s:form>

</body>
</html>

java代码是:

package com.krcl.rap.common.menu.action;

import java.util.HashMap;
import java.util.Map;
import com.opensymphony.xwork2.Action;

public class DatabaseJSON {

private Map<String, String> databases = new HashMap<String, String>();

public DatabaseJSON(){
    databases.put("Google", "www.google.com");
    databases.put("Oracle", "Oracle");
    databases.put("PostgreSQL", "PostgreSQL");
    databases.put("Microsoft SQL Server", "Microsoft SQL Server");
    databases.put("DB2", "DB2");
}

public String execute() {
    return Action.SUCCESS;
}

public Map<String, String> getDatabases() {
    return databases;
}

public void setDatabases(Map<String, String> databases) {
    this.databases = databases;
}
}

struts.xml中

<package name="json" namespace="/" extends="json-default">
        <action name="databaseJSON" class="com.krcl.rap.common.menu.action.DatabaseJSON">
        <result type="json" >
            <param name="root">databases</param>
        </result>
        </action>
</package>


<package name="default" namespace="/" extends="struts-default">
    <action name="autoCompleterAction" 
    class="com.krcl.rap.common.menu.action.AutoCompleterAction" 
        method="display">
    <result name="none">pages/autocompleter-json.jsp</result>
    </action>

    <action name="resultAction" 
        class="com.krcl.rap.common.menu.action.AutoCompleterAction" >
    <result name="success">pages/result.jsp</result>
    </action>
</package>

1 个答案:

答案 0 :(得分:0)

使用

<sx:autocompleter label="What's your favorite Database Server?" 
href="%{databaseList}" name="yourFavDatabase"       
id="yourFavDatabase"/>

在你的jsp和

var yourFavDb=dojo.widget.byId('yourFavDatabase').getValue();
在脚本中

获取值。如果您想在Action类中获取所选值,则在提交表单后,它将自行出现。