我有以下來源:「選擇」 從列表中<String>支柱不填充
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="true" />
<constant name="struts.devMode" value="true" />
<constant name="struts.custom.i18n.resources" value="ApplicationResources" />
<package name="vislabWebShop" extends="struts-default">
<action name="UserForward">
<result>/pages/Login.jsp</result>
</action>
<action name="UserLogin" class="vislabWebShop.controller.LoginAction">
<result name="success">/pages/Welcome.jsp</result>
<result name="input">/pages/Login.jsp</result>
</action>
<action name="UserRegister" class="vislabWebShop.controller.RegisterAction">
<result name="success">/pages/RegisterSuccess.jsp</result>
<result name="input">/pages/Register.jsp</result>
</action>
<action name="UserRegisterNew">
<result>/pages/Register.jsp</result>
</action>
<action name="UserRegisterSuccess">
<result>/pages/Login.jsp</result>
</action>
<action name="ProductSearchForward">
<result>/pages/SearchProduct.jsp</result>
</action>
<action name="ProductSearch" class="vislabWebShop.controller.ProductSearchAction">
<result name="success">/pages/Login.jsp</result>
</action>
</package>
</struts>
ProductSearchAction.java:
package vislabWebShop.controller;
import java.util.ArrayList;
import java.util.List;
import com.opensymphony.xwork2.ActionSupport;
public class ProductSearchAction extends ActionSupport
{
private List<String> categories;
private String chosenCategory;
public ProductSearchAction()
{
categories = new ArrayList<String>();
categories.add("Eins");
categories.add("Zwei");
categories.add("Drei");
}
@Override
public String execute() throws Exception
{
return SUCCESS;
}
public List<String> getCategories()
{
return categories;
}
public void setCategories(List<String> categories)
{
this.categories = categories;
}
public String getChosenCategory()
{
return chosenCategory;
}
public void setChosenCategory(String chosenCategory)
{
this.chosenCategory = chosenCategory;
}
}
SearchProduct.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title><s:text name="welcome.title" /></title>
</head>
<body bgcolor="white">
<font color="red"> <s:actionmessage />
</font>
<p>
<b><s:text name="product.search.title" /></b>
</p>
<s:form action="ProductSearch" focusElement="description">
<s:textfield name="description" key="prompt.description" size="20" />
<s:textfield name="minprice" key="prompt.price.min" size="20" />
<s:textfield name="maxprice" key="prompt.price.max" size="20" />
<s:select key="product.search.category" headerKey="-1"
headerValue="Bitte wählen Sie eine Kategorie"
list="categories" />
<s:submit value="Produkt suchen" align="right" />
</s:form>
<font color="red"> <s:actionerror label="label" />
</font>
</body>
</html>
現在我有問題,我總是碰到下面的錯誤,如果我來自行動ProductSearchForward到JSP網站SearchProduct.jsp:
org.apache.jasper.JasperException: tag 'select', field 'list', name 'product.search.category': The requested list key 'categories' could not be resolved as a collection/array/map/enumeration/iterator type. Example: people or people.{name} - [unknown location]
我只是想從給定的ArrayList<String>
(List<String>
)填充的DropDownList ,但它不起作用。如果我直接設置列表內容,它工作正常。
好吧,我像你提到的那樣。但只是出現以下錯誤:標記'select',字段'list',名稱'chosenCategory':請求的列表鍵'categories'不能被解析爲一個collection/array/map/enumeration/iterator類型。示例:人員或人員。{名稱} - [未知地點] 在哪裏設置列表? –
如果在設置時指的是人口,我建議實現'Preparable'接口並在'prepare()'方法中加載「靜態」數據。避免使用Action的構造函數...否則,將其填充到'execute()'方法中,但在INPUT結果的情況下它將不可用於頁面。同時刪除List的setter。 –
順便說一句,錯誤信息是誤導性的:只要按照我所說的關鍵字和名稱屬性進行操作即可。 –