gpt4 book ai didi

java - Struts 2选择标签错误

转载 作者:行者123 更新时间:2023-12-01 04:57:28 26 4
gpt4 key购买 nike

我是 Struts2 的新手。我想比较一下JSTL的c标签和Struts2的s标签哪个好用...我的代码如下

ListDepartmentNameAction.java

package actions;

import java.util.List;
import org.apache.log4j.Logger;
import org.hibernate.mapping.Array;
import com.opensymphony.xwork2.ActionSupport;
import service.ListDepNameService;

public class ListDepartmentNameAction extends ActionSupport{

private static Logger log = Logger.getLogger(ListDepartmentNameAction.class);
ListDepNameService listDepNameService;
private List<String> allDNlist ;

public String execute() {

allDNlist = listDepNameService.ListAllDepName();
for (String ss : allDNlist) {
System.out.println(ss);
}
log.info(allDNlist);
return "success";

}

public ListDepNameService getListDepNameService() {
return listDepNameService;
}

public void setListDepNameService(ListDepNameService listDepNameService) {
this.listDepNameService = listDepNameService;
}

public List<String> getAllDNlist() {
return allDNlist;
}

public void setAllDNlist(List<String> allDNlist) {
this.allDNlist = allDNlist;
}
}

查询.jsp

<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8"      pageEncoding="UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<s:head />
<h1 align="center" id="h1"></h1>
<body>

<s:form action="listDepName" id="form" method="post">
<input name="Button" type="submit" id="listsubmit" value="List all Department Name"
onclick="javascirpt:abc(this)"/>
</s:form>

<select>
<c:forEach items="${allDNlist}" var="item">
<option value="abc" >${item}</option>
</c:forEach>
</select>

<s:if test="%{allDNlist==null}">456</s:if>
<s:else><s:select name="xxx" list="allDNlist" /></s:else> <!-- 1st -->

<s:select name="xyz" list="allDNlist" /> <!-- 2nd -->

</body>
</html>

“allDNlist”可以从action类中获取值,因此,JSTL c标签可以正常工作。我不明白为什么“1st”struts2 select 标签工作正常,但“2nd”select s 标签不起作用,并收到这样的消息

 HTTP Status 500 - tag 'select', field 'list', name 'xyz': The requested list key 'allDNlist' could not be resolved as a collection/array/map/enumeration/iterator type. Example: people or people.{name} - [unknown location]

即使我 comment() “2nd” select s 标签,我仍然收到与上面相同的错误消息,只是将其删除。

最佳答案

编辑:

我复制了您的整个代码,它运行良好。

请注意,您不要关闭 </head>标签,我也复制了它,它的作用是一样的......应该是

<head>
<s:head/>
</head>

您应该声明您的 ListDepNameService listDepNameService;也作为私有(private)(您已经拥有访问器),并检查返回的列表类型。

我用

测试了代码
    allDNlist = new ArrayList<String>();
allDNlist.add("Valore 1 ");
allDNlist.add("Valore 2 ");
allDNlist.add("Valore 3 ");

在execute()方法中,这是唯一的区别。

请尝试此操作而不是服务调用,并让我知道...

关于java - Struts 2选择标签错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13872239/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com