Struts 2选择由列表属性填充的标签

发布于 2024-08-08 19:51:03 字数 5275 浏览 5 评论 0原文

我是 struts 2 的新手。我在用列表属性填充 Select 标签时遇到问题。这些值由操作类提供。请为我提供此场景的示例代码。

我的操作类

public class TripDetailsAdd extends ActionSupport {

    @Override
    public String execute() throws Exception {
        return SUCCESS;
    }

    public String populate() {
        VehicleDAO vehicleDAO = new VehicleDAO();
        this.lstVehicles.addAll(vehicleDAO.getAllVehicles());
        return "populate";
    }   

    private String vehicleId;   
    private Collection lstVehicles = new ArrayList<VehiclesVO>();
}

Jsp 页面内容:

<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib prefix="sform" uri="/struts-dojo-tags"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%@page import="com.vms.business.dao.VehicleDAO"%>
<%@page import="java.util.Collection"%><html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Trip Details</title>
</head>
<body>
<s:form action="tripDetailsAdd" method="POST" >
    <s:hidden name="expenseTypeId"></s:hidden>
    <table width="100%" height="96%" cellpadding="0" cellspacing="0">
        <tr>
            <td valign="top"><!-- Menu Starts  --> <jsp:include
                page="/pages/menu.jsp"></jsp:include> <!-- Menu End  -->
            <table width="95%" align="center">
                <tr>
                    <td>
                    <table width="100%" border="0" cellpadding="0" cellspacing="0">

                        <tr>
                            <td class="highlight">Trip Details Add</td>
                            <td><s:actionerror /><s:actionmessage /></td>
                        </tr>
                        <tr>
                            <td class="header3shadow" colspan="2"><img height="2"
                                border="0" width="100%"></img></td>
                        </tr>
                    </table>
                    <table>
                        <tr>
                            <td>&nbsp;</td>
                        </tr>
                    </table>
                    <div
                        style="overflow: auto; height: expression((document.body.clientHeight -80) +px ');">
                    <table width="60%" cellspacing="0" cellpadding="0" border='0'>
                        <tr>
                            <td class="FieldTitle" valign="top">
                            <table width="100%" cellspacing="4" cellpadding="0" border='0'>
                            <s:select headerKey="0" headerValue="Select One" required="*" label="Vehicle No."
                             labelSeparator=":"   list="lstVehicles" listKey="vehicleId" listValue="regNo"></s:select>

                            </td>
                        </tr>
                        <tr>
                            <td>
                            <table width="100%" cellspacing="4" cellpadding="0" border='0'>
                                <s:textfield labelposition="left" requiredposition="right"
                                    name="totalIncome" label="Total Income" cssStyle="FieldTitle"
                                    labelSeparator=":"></s:textfield>
                            </table>
                            </td>
                        </tr>
                    </table>

                    <table cellpadding="0" width="60%" cellspacing="0" border="0">
                        <tr>
                            <td align="right"><s:submit label="Add" value="Add"></s:submit></td>
                        </tr>
                    </table>
                    </div>

                    <table border="0" cellspacing="0" cellpadding="0">
                        <tr>
                            <td>&nbsp;</td>

                        </tr>
                    </table>
                    </td>
                </tr>
            </table>
            </td>
        </tr>
    </table>
</s:form>
</body>
</html>

映射

<action name="*TripDetailsAdd" method="{1}"
            class="com.vms.trip.presentation.TripDetailsAdd">
            <result name="success" type="redirect">showTripDetailsList
            </result>
            <result name="populate">/pages/tripdetails/TripDetailsAdd.jsp
            </result>
            <result name="error">/pages/tripdetails/TripDetailsAdd.jsp
            </result>
            <result name="input">/pages/tripdetails/TripDetailsAdd.jsp
            </result>
        </action>

如果我添加验证文件或如果页面下拉列表中发生任何错误则不会加载。请帮助我..

I am new to struts 2. I am facing problem in filling Select tag with list property. The values are supplied from action class.Please provide me sample sode for this scenario.

My action class

public class TripDetailsAdd extends ActionSupport {

    @Override
    public String execute() throws Exception {
        return SUCCESS;
    }

    public String populate() {
        VehicleDAO vehicleDAO = new VehicleDAO();
        this.lstVehicles.addAll(vehicleDAO.getAllVehicles());
        return "populate";
    }   

    private String vehicleId;   
    private Collection lstVehicles = new ArrayList<VehiclesVO>();
}

Jsp page content:

<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib prefix="sform" uri="/struts-dojo-tags"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%@page import="com.vms.business.dao.VehicleDAO"%>
<%@page import="java.util.Collection"%><html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Trip Details</title>
</head>
<body>
<s:form action="tripDetailsAdd" method="POST" >
    <s:hidden name="expenseTypeId"></s:hidden>
    <table width="100%" height="96%" cellpadding="0" cellspacing="0">
        <tr>
            <td valign="top"><!-- Menu Starts  --> <jsp:include
                page="/pages/menu.jsp"></jsp:include> <!-- Menu End  -->
            <table width="95%" align="center">
                <tr>
                    <td>
                    <table width="100%" border="0" cellpadding="0" cellspacing="0">

                        <tr>
                            <td class="highlight">Trip Details Add</td>
                            <td><s:actionerror /><s:actionmessage /></td>
                        </tr>
                        <tr>
                            <td class="header3shadow" colspan="2"><img height="2"
                                border="0" width="100%"></img></td>
                        </tr>
                    </table>
                    <table>
                        <tr>
                            <td> </td>
                        </tr>
                    </table>
                    <div
                        style="overflow: auto; height: expression((document.body.clientHeight -80) +px ');">
                    <table width="60%" cellspacing="0" cellpadding="0" border='0'>
                        <tr>
                            <td class="FieldTitle" valign="top">
                            <table width="100%" cellspacing="4" cellpadding="0" border='0'>
                            <s:select headerKey="0" headerValue="Select One" required="*" label="Vehicle No."
                             labelSeparator=":"   list="lstVehicles" listKey="vehicleId" listValue="regNo"></s:select>

                            </td>
                        </tr>
                        <tr>
                            <td>
                            <table width="100%" cellspacing="4" cellpadding="0" border='0'>
                                <s:textfield labelposition="left" requiredposition="right"
                                    name="totalIncome" label="Total Income" cssStyle="FieldTitle"
                                    labelSeparator=":"></s:textfield>
                            </table>
                            </td>
                        </tr>
                    </table>

                    <table cellpadding="0" width="60%" cellspacing="0" border="0">
                        <tr>
                            <td align="right"><s:submit label="Add" value="Add"></s:submit></td>
                        </tr>
                    </table>
                    </div>

                    <table border="0" cellspacing="0" cellpadding="0">
                        <tr>
                            <td> </td>

                        </tr>
                    </table>
                    </td>
                </tr>
            </table>
            </td>
        </tr>
    </table>
</s:form>
</body>
</html>

Mapping

<action name="*TripDetailsAdd" method="{1}"
            class="com.vms.trip.presentation.TripDetailsAdd">
            <result name="success" type="redirect">showTripDetailsList
            </result>
            <result name="populate">/pages/tripdetails/TripDetailsAdd.jsp
            </result>
            <result name="error">/pages/tripdetails/TripDetailsAdd.jsp
            </result>
            <result name="input">/pages/tripdetails/TripDetailsAdd.jsp
            </result>
        </action>

In this if i add Validation file or If any error occurs in page drop down is not loading.Please help me..

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

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

发布评论

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

评论(1

再可℃爱ぅ一点好了 2024-08-15 19:51:03

Struts2 select 标签

<s:select label="Pets"
       name="petIds"
       list="petDao.pets"
       listKey="id"
       listValue="name"
       value="%{petDao.pets.{id}}"
/>

在上面。 value = 默认选择,list = 来自操作类的(地图)集合,listKey = 地图的键,listValue = 地图的值。

编辑(查看提供的代码后):

您的问题是您的操作类中没有任何与 lstVehicles 对应的 getter (在您的选择标记的列表属性中提到)

将其添加到您的操作中班级:

public List getLstVehicles ()
{
    return this.lstVehicles;
}

Struts2 select tag

<s:select label="Pets"
       name="petIds"
       list="petDao.pets"
       listKey="id"
       listValue="name"
       value="%{petDao.pets.{id}}"
/>

In the above. value = default selection, list = collection (of Map) coming from your action class, listKey = Key for map, listValue = value for map.

Edit (after looking at provided code):

your problem is that you do not have any getter in action class that corresponds with lstVehicles (which is mentioned in list property of your select tag)

Add this to your action class:

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