`
zhangfeilo
  • 浏览: 391320 次
  • 性别: Icon_minigender_1
  • 来自: 昆明
社区版块
存档分类
最新评论

Struts2 将两个<sx:autocompleter/>关联起来

阅读更多

本人最近两天想使用sx:autocpmpleter来实现地区的异步动态更新。《Struts2.1权威指南》中找到示例,结果按照示例的方式去研究怎么弄都无法现实动态更新。最后发现示例代码不全,作者很马虎导致,代码更本不可运行。在网上搜索也没有相关的完整示例代码。花了漫长的2天时间终于搞定了!个人意见仅供参考。

1.jsp代码:

 

<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ taglib prefix="sx" uri="/struts-dojo-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>自动完成</title>
 <sx:head/>
</head>
<body>
<h3>将两个sx:autocompleter关联起来</h3>
<form id="selectForm" >
请选择您喜欢的作者:<br />
<sx:autocompleter 
 name="author" 
 list="{'1','Rod Johnson' , 'David Flanagan'}" 

<--list="#request['books']" listKey="id" listValue="name"-->

<--list="#{'1':'2','2':'3'}" listKey="%{0}" listValue="%{1}"

 list="#request['books']" listKey="id"<--传递的值--> listValue="name"<--显示的值--> required="id"<--指定传递的参数属性-->

-->

 value="1"
 notifyTopics="/books"
 forceValidOption="true"/>

</form>
请选择您喜欢的图书:<br />
<sx:autocompleter 
 name="book"
 href="getBooks.action" 
 cssStyle="width: 240px;"
 autoComplete="false"
 formId="selectForm"
 listenTopics="/books"
 forceValidOption="true"/>
</body>
</html>

 2.Action代码

 

package lee;

import java.util.ArrayList;
import java.util.List;
import com.opensymphony.xwork2.ActionSupport;


public class GetBooksAction extends ActionSupport {

    private String author;
    private List<String> books = new ArrayList<String>();
    private int num;

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }

    //author属性的setter和getter方法
    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = HTMLDecoder.decode(author);
    }
    //books属性的getter方法

    public List<String> getBooks() {
        return books;
    }
    //处理用户请求的execute方法

    public String execute() throws Exception {
        if (author.equals("1")) {
            books.clear();
            books.add("疯狂Java讲义");
            books.add("轻量级Java EE企业应用实战");
            books.add("疯狂Ajax讲义");
            books.add("疯狂XML讲义");
            num = 3;
        } else if (author.equals("Rod Johnson")) {
            books.clear();
            books.add("Expert One-on-One J2EE"
                    + " Design and Development");
            books.add("Expert One-on-One J2EE"
                    + " Design and Development");
            num = 1;
        } else if (author.equals("David Flanagan")) {
            books.clear();
            books.add("JavaScript权威指南");
            books.add("Expert One-on-One J2EE"
                    + " Design and Development");
            num = 1;
        }
        System.out.println(books.get(0));
        return SUCCESS;
    }
}

 

3.异步页面代码

 

<%@ page contentType="text/html;charset=GBK" language="java" %>

 

 

<%@ taglib prefix="s" uri="/struts-tags" %>
{
<s:iterator value="books" var="b" status="bs">
 '<s:property/>':'<s:property/>'
    <s:if test="%{#bs.index<num}">
        ,
    </s:if>
</s:iterator>
}

 

0
0
分享到:
评论
1 楼 littleshy 2012-10-17  
感谢楼主,李刚太坑爹了,差那一点都不弄好

相关推荐

Global site tag (gtag.js) - Google Analytics