Question

Je suis nouveau sur struts 2. Je suis confronté à un problème de remplissage de la balise Select avec propriété de liste. Les valeurs sont fournies par la classe d'action.Veuillez me fournir un exemple de sode pour ce scénario.

Ma classe d'action

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>();
}

Contenu de la page 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>

Cartographie

<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>

En cela, si j'ajoute un fichier de validation ou si une erreur survient dans la liste déroulante, le chargement ne se fait pas.Veuillez m'aider ..

Était-ce utile?

La solution

balise de sélection Struts2

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

ci-dessus. valeur = sélection par défaut, list = collection (de Map) provenant de votre classe d'action, listKey = clé pour map, listValue = valeur pour map.

Modifier (après avoir examiné le code fourni):

Votre problème est que vous n'avez aucun getter dans la classe d'action qui correspond à lstVehicles (qui est mentionné dans la propriété list de votre balise select)

Ajoutez ceci à votre classe d'action:

public List getLstVehicles ()
{
    return this.lstVehicles;
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top