1

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

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" 可以从动作类中获取值,因此,JSTL c 标签可以正常工作。我不明白为什么“第一个”struts2 选择标签工作正常,但“第二个”选择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]

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

4

2 回答 2

0

<s:select: 在使用标签填充下拉列表时,我遇到了类似类型的收集错误。经过研究,我发现在您的情况下“我没有初始化我的实例变量列表”private List<String> allDNlist = new ArrayList<String>();应该可以解决问题。

于 2015-01-24T20:40:32.640 回答
0

编辑:

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

请注意,您没有关闭</head>标签,我也复制了它,它的工作原理相同......应该是

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

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

我用

    allDNlist = new ArrayList<String>();
    allDNlist.add("Valore 1 ");
    allDNlist.add("Valore 2 ");
    allDNlist.add("Valore 3 ");

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

请试试这个,而不是服务电话,让我知道...

于 2012-12-14T09:49:20.790 回答