Struts2 - 无法解析请求的列表键 'Products'

标签 struts2

我是 Struts 2 的新手,想看看我是否可以用值列表填充下拉框,但我仍然无法让它工作。

以下是我的 Action 类 (Portal.java):

public class Portal extends ActionSupport {

    private Map Products;
    private String product;

    public Portal(){
       Products = new HashMap();
       Products.put("1", "Java");
       Products.put("2", "C++");
    }

    public Map getProducts() {  
       return Products;
    }


    public void setProducts(Map Products) {
       this.Products = Products;
    }

    public String getProduct() {
    return product;
    }

    public void setProduct(String product) {
    this.product = product;
    }  

    public String execute() {
       return SUCCESS;
    }
}

以下是我的 Portal.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Portal</title>
</head>
<body>
<h2>Portal</h2>
<s:form action="result" namespace="/">
    <h4>
        <s:select label="Select Product"
        name="product"
        headerKey="-1"
        headerValue="Select Product"            
        list="Products"                                                     
        />
    </h4>
    <h4>
        <s:submit label="Submit" name="submitButton"  align="center" />
    </h4>
</s:form>   

以下是我的result.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org 
/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Results Page</title>
</head>
<body>
<h1>Portal</h1> 
<h4>
   Selected Product : <s:property value="product"/>
</h4>
</body>
</html>

以下是我的struts.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
   <constant name="struts.enable.DynamicMethodInvocation" value="false" />
   <constant name="struts.devMode" value="true" />
   <constant name="struts.ognl.allowStaticMethodAccess" value="false" />
   <package name="default" extends="struts-default" namespace="/">          
      <action name="result" class="com.abc.xyz.Portal">
         <result name="error">Portal.jsp</result>
         <result name="success">Result.jsp</result>                     
      </action>     
   </package>
</struts>

以下是我的 web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee
/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">

<display-name>Portal</display-name>
<filter>
  <filter-name>struts2</filter-name>
  <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
  <filter-name>struts2</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
   <welcome-file>Portal.jsp</welcome-file>
</welcome-file-list>
</web-app>

当我在 Tomcat 6 服务器上执行此命令时,出现以下错误:

SEVERE: Servlet.service() for servlet jsp threw exception
tag 'select', field 'list', name 'product': The requested list key 'Products' could   
not be resolved as a collection/array/map/enumeration/iterator type. Example: 
people or people.{name} - [unknown location]

但是当我用 list="#{'1':'Java', '2':'C++'}"替换 list="Products"时,它工作得很好..我不知道是什么我错过了,所以列表没有显示。

请帮忙。

最佳答案

没有为 map 产品分配内存。

就在为产品设置值之前写 - Products = new Map();

然后将值设置到 map 。这将有助于在 View 中填充产品的值。

关于Struts2 - 无法解析请求的列表键 'Products',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9811039/

相关文章:

java - 如何在struts2 启动时将一些数据库值设置为应用程序范围?

javascript - 如何在struts2 param标签中使用javascript变量

java - 您能解释一下 Struts2 Web 应用程序会发生什么吗?

javascript - 将 React 组件集成/注入(inject)到静态(已加载)HTML 中

java - 在 JBoss 7.1.1 中使用 EJB 3.1 调用 Struts 2 Action 时获得 Struts 报告

javascript - JavaScript 中的 Struts 标签

hibernate - 解释 Struts 2、Spring 和 Hibernate 集成的教程

java - 如何使用 Struts2 jQuery 从 external.js 获取 session 值

java - 从 jsp 设置 Struts2 Action 成员

java - 更新 Map 中自定义对象的值,例如包含对象 B 列表的对象 A