我是Struts 2的新手,試圖查看是否可以使用值列表填充下拉框,但仍然無法使其工作。Struts2 - 無法解析請求的列表項'Products'
以下是我的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」時,它的工作原理非常好..我不知道我錯過了什麼以便列表不顯示。
請幫忙。
沒有,他將產品定義爲參考變量。 – 2012-03-22 04:38:56
引用變量?我沒有仔細閱讀這個問題,但仔細觀察它......行動類中的變量產品應該是「產品」,應該有一個吸氣劑,但沒有。 – Quaternion 2012-03-22 07:18:12