gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 01:17:18 26 4
gpt4 key购买 nike

我是 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/

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