使用 struts2 dojo 标签填充一个下拉列表以选择其他选项

发布于 2024-12-12 04:16:14 字数 2410 浏览 0 评论 0原文

我有两个下拉菜单。我想在选择第一个下拉列表时填充第二个下拉列表。 我正在使用 struts-dojo-tags 来完成此任务。

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib prefix="sx" uri="/struts-dojo-tags"%>
<html>
<head>
<title>Welcome</title>
<script>
</script>
<sx:head />
</head>
<body>
<h2>Struts 2 + Ajax Example 2!</h2>
<s:form name="f1">
<s:textfield name="proj" label="Project Name" />
<s:url id="url" value="/populateStates.action" />
<s:select id="id1" list="countryList"
    listKey="countryId" listValue="countryName" headerKey="0"
    headerValue="-Select-" label="Country"/>
<s:select id="id2" list="list2" listKey="stateId"
    listValue="stateName" headerKey="0" headerValue="-Select-"
    label="State" />
<sx:bind sources="id1" targets="id2" events="onchange"
    href="%{#url}" />
</s:form>
<br>
</body>
</html>

我的动作类有两个动作方法。

        public String populateCountry() throws Exception{

    list1 = new ArrayList<Country>();
    list1.add(new Country(1, "Country1"));
    list1.add(new Country(2, "Country2"));
    list1.add(new Country(3, "Country3"));

    return SUCCESS;
}

public String populateStates() throws Exception{

    System.out.println("populateStates******************"+id1);

    list2 = new ArrayList<State>();

    if (id1 != null && !id1.equals("")) 
    {   
        if(id1.equals("1"))
        {
            list2.add(new State(1, "UMB1"));
            list2.add(new State(2, "UMB2"));
            list2.add(new State(3, "UMB3"));
        }
        else if(id1.equals("2"))
        {
            list2.add(new State(4, "UMB4"));
            list2.add(new State(5, "UMB5"));
            list2.add(new State(6, "UMB6"));
        }
        else if(id1.equals("3"))
        {
            list2.add(new State(7, "UMB7"));
            list2.add(new State(8, "UMB8"));
            list2.add(new State(9, "UMB9"));
        }       
    }
    return SUCCESS;
}

当第一次加载 jsp 时(使用填充的 Country 下拉列表),将调用第一个操作方法,当我从 Country 下拉列表中选择一个国家/地区时,将调用第二个操作方法。

问题:States 下拉列表未填充。当我调试代码时,我发现当控制到达 populateStates 时,我的 Action 类的所有变量都被重置(id1、id2、list1、list2、proj)。因此我得到一个空的州下拉列表。请帮我解决这个问题。

I have two dropdowns. I want to populate the second dropdown on selection of first dropdown.
I am using struts-dojo-tags for accomplishing this.

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib prefix="sx" uri="/struts-dojo-tags"%>
<html>
<head>
<title>Welcome</title>
<script>
</script>
<sx:head />
</head>
<body>
<h2>Struts 2 + Ajax Example 2!</h2>
<s:form name="f1">
<s:textfield name="proj" label="Project Name" />
<s:url id="url" value="/populateStates.action" />
<s:select id="id1" list="countryList"
    listKey="countryId" listValue="countryName" headerKey="0"
    headerValue="-Select-" label="Country"/>
<s:select id="id2" list="list2" listKey="stateId"
    listValue="stateName" headerKey="0" headerValue="-Select-"
    label="State" />
<sx:bind sources="id1" targets="id2" events="onchange"
    href="%{#url}" />
</s:form>
<br>
</body>
</html>

My action class has two action methods.

        public String populateCountry() throws Exception{

    list1 = new ArrayList<Country>();
    list1.add(new Country(1, "Country1"));
    list1.add(new Country(2, "Country2"));
    list1.add(new Country(3, "Country3"));

    return SUCCESS;
}

public String populateStates() throws Exception{

    System.out.println("populateStates******************"+id1);

    list2 = new ArrayList<State>();

    if (id1 != null && !id1.equals("")) 
    {   
        if(id1.equals("1"))
        {
            list2.add(new State(1, "UMB1"));
            list2.add(new State(2, "UMB2"));
            list2.add(new State(3, "UMB3"));
        }
        else if(id1.equals("2"))
        {
            list2.add(new State(4, "UMB4"));
            list2.add(new State(5, "UMB5"));
            list2.add(new State(6, "UMB6"));
        }
        else if(id1.equals("3"))
        {
            list2.add(new State(7, "UMB7"));
            list2.add(new State(8, "UMB8"));
            list2.add(new State(9, "UMB9"));
        }       
    }
    return SUCCESS;
}

First action method is called when the jsp is loaded the first time (with populated Country drop down) and second action method is called when I select a country from the Country dropdown.

Problem: States drop down is not getting populated. When I debug the code, I see that when control reaches populateStates all the variables of my Action class are reset(id1, id2, list1, list2, proj). Due to which I get an empty States dropdown. Please help me to solve this issue.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

筱果果 2024-12-19 04:16:14

我不认为 会做你认为它会做的事情;目标的 HTML 将被操作返回的内容替换。

您可以尝试仅返回列表(也许这就是您正在做的事情),但我不这样做相信它会起作用。 (如果确实如此,请务必遵循您自己的答案。)

我实际上建议使用 jQuery 插件; Dojo 标签已被弃用一段时间,使用非常过时的 Dojo 版本,并且使用起来通常很痛苦。

另一种选择是仅使用普通的旧式 jQuery(或您选择的库)——它几乎总是比通过标签库实现自定义功能更容易。 Ajax 标签库非常适合非常简单的用例,但一旦有任何自定义内容,我不相信它们会节省很多精力。 YMMV。

I don't think <sx:bind> does what you think it does; the target's HTML will be replaced by what the action returns.

You could try returning just the list of <option>s (and perhaps that's what you're doing), but I don't believe it would work. (If it does, definitely follow up with your own answer.)

I'd actually recommend using the jQuery plugin; the Dojo tags have been deprecated for some time, use a very outdated version of Dojo, and often a pain to work with.

Another option would be to just use plain old jQuery (or library of your choice)--it's almost always easier than implementing custom functionality via a tag library. The Ajax tag libraries are great for really simple usecases, but as soon as there's anything custom, I'm not convinced they save much effort. YMMV.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文