我正在使用一个 Java Servlet 和一个包含主题和类别的 JSP 文件。当我使用浏览器访问 JSP 页面时,使用了 doGET 方法,在该方法中我声明了我需要填写多选表单的所有主题和类别列表。
我想要做的是,使用 jQuery,每次我的用户在适当的多选表单中更改所选类别时,主题多选的内容会自动更改,而无需刷新网页。
类别和主题之间的关系被发送到 HashMap 中的 JSP 文件(类别 -> 主题),但我不知道如何使用它。
一位朋友告诉我,为了动态更新多选中的主题,我必须使用 jQuery 的 doPost 方法并将更新后的列表发送回 JSP 页面(参见下面我的 doPOST 方法,目前,我' m 试图动态显示所选类别的列表。
但是这段代码不起作用:我的 POST 是根据 FireBug 发送的,但是多选没有更新。我忘记了什么吗?
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
SubjectDAO subjectDao;
RelationDAO relationDao;
TripletDAO tripletDao;
CollectionDAO collectionDao;
try
{
//Récupération de la session courante
HttpSession session = request.getSession();
int idCollection = 1; //Integer.parseInt((String) session.getAttribute("idCollection"));
//TODO: Remplacer le 1 par le commentaire quand les collections marcheront bien
//Initialisation des DAO nécessaires
tripletDao = daoFactory.getTripletDAO();
collectionDao = daoFactory.getCollectionDAO(); //DAOFactory.getDAOFactory(DAOFactory.MYSQL).getCollectionDAO();
subjectDao = daoFactory.getSubjectDAO();
relationDao = daoFactory.getRelationDAO();
//Formation des listes nécessaires
List<Subject> listSubject = subjectDao.selectAll(false, collectionDao.selectId(idCollection)); //Tous les sujets de la collec' courante
List<Relation> listRelation = relationDao.selectAll(false, collectionDao.selectId(idCollection)); //Toutes les relations de la collec' courante
List<Triplet> listTriplet = tripletDao.selectAll(false, collectionDao.selectId(idCollection)); //Tous les triplets de la collec' courante
//TODO: Voir ce qu'on peut faire pour éviter l'utilisation des listes ci-dessous!
ArrayList<String> entitledOfSubjects = new ArrayList<String>(listOfSubjectsEntitled(session, listSubject));
ArrayList<String> entitledOfRelations = new ArrayList<String>(listOfRelationsEntitled(session, listRelation));
//Récupération de la liste des catégories (de la collection courante)
Subject sCategory = subjectDao.selectSystem("CATEGORY");
Relation rIs = relationDao.selectSystem("ISA");
Relation rIn = relationDao.selectSystem("IN CATEGORY");
//La liste listCategory2 contient tous les triplets qui contiennent "ISA CATEGORY". Elle s'appelle listCategory2 parce que listCategory c'est celle de la sidebar!
List<Triplet> listCategory2 = tripletDao.selectAll(-1, rIs.getId(), sCategory.getId(), TripletType.SUBJET, TripletType.SUBJET, false, true, collectionDao.selectId(idCollection));
//Liste des différents compléments des triplets de la collec' courante
ArrayList<Subject> listComplement = new ArrayList<Subject>();
for (int i=0; i<listTriplet.size(); i++)
{
Subject s = listTriplet.get(i).getSComplement();
if (!subjectIsInList(session, s, listComplement) && s!=null && subjectIsInList(session, listTriplet.get(i).getSSubject(), listSubject)
&& relationIsInList(session, listTriplet.get(i).getRelation(), listRelation))
{
listComplement.add(s);
}
}
//Liste des triplets de la collection courante qui ont pour relation "IN CATEGORY"
List<Triplet> listTripletSysteme = tripletDao.selectAllByRelation(rIn.getId(), collectionDao.selectId(idCollection)); //(true, collectionDao.selectId(idCollection));
//Liste des différentes catégories des compléments relevés précédemment
ArrayList<Subject> listComplementCategory = new ArrayList<Subject>();
for (int i=0; i<listTripletSysteme.size(); i++)
{
Subject s = listTripletSysteme.get(i).getSComplement();
if (!subjectIsInList(session, s, listComplementCategory) && s!=null && subjectIsInList(session, listTripletSysteme.get(i).getSSubject(), listComplement))
{
listComplementCategory.add(s);
}
}
//Variables à envoyer à la page JSP
request.setAttribute("listSubject", listSubject);
request.setAttribute("listRelation", listRelation);
request.setAttribute("listCategory2", listCategory2); //session au lieu de request pour la sidebar
request.setAttribute("listComplementCategory", listComplementCategory);
request.setAttribute("listComplement", listComplement);
//TODO: Use of these deprecated by Joe
request.setAttribute("entitledOfSubjects", entitledOfSubjects);
request.setAttribute("entitledOfRelations", entitledOfRelations);
//HashMap (Titre de Catégorie -> liste de titres de sujets lui appartenant)
HashMap<String, ArrayList<String>> categorySubjectsHashMap = new HashMap<String, ArrayList<String>>();
for (int i=0; i<listCategory2.size(); i++)
{
Subject categorie = listCategory2.get(i).getSSubject();
String entitled = categorie.getEntitled((String) session.getAttribute("lang"));
ArrayList<String> listeSujetsCategorie = new ArrayList<String>();
for (int j=0; j<listTripletSysteme.size(); j++)
{
Triplet tripletSysteme = listTripletSysteme.get(j);
if (tripletSysteme.getSComplement().getEntitled((String) session.getAttribute("lang")).equals(entitled) &&
!listeSujetsCategorie.contains(tripletSysteme.getSSubject().getEntitled((String) session.getAttribute("lang"))))
{
listeSujetsCategorie.add(tripletSysteme.getSSubject().getEntitled((String) session.getAttribute("lang")));
}
}
categorySubjectsHashMap.put(entitled, listeSujetsCategorie);
}
request.setAttribute("categorySubjectsHashMap", categorySubjectsHashMap);
//request.setAttribute("listTriplet", listTriplet);
//Subject sCategory = subjectDao.selectSystem("CATEGORY");
//Relation rIs = relationDao.selectSystem("ISA");
/*CollectionDAO collectionDao = DAOFactory.getDAOFactory(DAOFactory.MYSQL).getCollectionDAO();
//int idCollection = Integer.parseInt((String) session.getAttribute("idCollection"));
//List<Triplet> listCategory = tripletDao.selectAll(-1, rIs.getId(), sCategory.getId(), TripletType.SUBJET, TripletType.SUBJET, false, true, collectionDao.selectId(idCollection));
//request.setAttribute("listCategory", listCategory);
//Sert à "overrider" idCollection*/
//int id = add(request);
}
catch (DAOException e)
{
log.error("An error occured", e);
request.setAttribute("error", e.getMessage());
request.getRequestDispatcher("/WEB-PAGES/Other/error.jsp").forward(request, response);
return;
}
// Set page title
request.setAttribute("title", "Creation");
request.getRequestDispatcher("/WEB-PAGES/Creation/creation.jsp").forward(request, response);
//HttpSession session = request.getSession();
//String root = (String) session.getAttribute("root");
//response.sendRedirect(root + "creation");
//TODO: fix root = null
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
//ArrayList<String> al = new ArrayList<String>();
String[] selectedSubjectCategories = request.getParameter("selectedSubjectCategories").split(",");
ArrayList<String> entitledOfSubjects = new ArrayList<String>();
for (int i=0; i<selectedSubjectCategories.length; i++)
{
entitledOfSubjects.add(selectedSubjectCategories[i]);
}
request.setAttribute("entitledOfSubjects", entitledOfSubjects);
}
此外,这里是 JSP 文件的片段,其中包含两个多选,第一个是我选择我的类别的那个,第二个是我想要动态显示属于所选类别的主题的那个:
<div id="subjectCategoriesDiv">
<select name="subjectCategories" multiple="multiple" id="subjectCategories">
<option value="None">-- NONE --</option>
<c:forEach items="${listCategory2}" var="triplet">
<option value="<c:out value="${triplet.SSubject.getEntitled(lang)}" />"><c:out value="${triplet.SSubject.getEntitled(lang)}" /></option>
</c:forEach>
</select>
<script>
$(document).ready(function ()
{
$('#subjectCategories').change(function()
{
var selectedOptions = $('#subjectCategories option:selected');
var selectedValues = $.map(selectedOptions ,function(option)
{
return option.value;
}).join(',');
$.ajax({
type: "POST",
url: "creation",
data: 'selectedSubjectCategories='+selectedValues
});
}
);
}).change();
</script>
</div>
<div class="subjectsListDiv">
<select name="subjectslist" multiple="multiple" id="subjectslist">
<c:forEach items="${entitledOfSubjects}" var="subject">
<option value="<c:out value="${subject}" />"><c:out value="${subject}" /></option>
</c:forEach>
</select>
<script>
$(document).ready(function ()
{
});
</script>
</div>
提前致谢。