我有以下资料来源:
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>
现在我遇到了问题,如果我从Action ProductSearchForward到JSP网站SearchProduct.jsp,我总是会收到以下错误:
org.apache.jasper.JasperException:标记'select',字段'list',名称'product.search.category':请求的列表键'categories'无法解析为collection / array / map / enumeration / iterator类型。例如:人或人。{name} - [unknown location]
我只想从给定的ArrayList<String>
(List<String>
)填充DropDownList,但它不起作用。如果我直接设置列表内容,它可以正常工作。
答案 0 :(得分:3)
<s:select list = "categories"
key = "product.search.category" />
您正在列出List<String>
并尝试通过OGNL .
(点表示法)访问不存在的字段。
在OGNL
product.search.category
相当于Java
getProduct().getSearch().getCategory()
由于您列出了字符串,只需省略键属性,因为您的键和值都将是字符串本身。
您似乎也在key
与name
混淆:key
是<option>
元素的关键,而name
是Action的属性将通过其Setter收到所选的值。
<s:select list = "categories"
name = "chosenCategory" />
编辑:为了获得成功,请实施Preparable Interface并在那里加载“静态”数据:
public class ProductSearchAction extends ActionSupport implements Preparable {
private List<String> categories;
private String chosenCategory;
@override
public void prepare() throws Exception {
categories = new ArrayList<String>();
categories.add("Eins");
categories.add("Zwei");
categories.add("Drei");
}
@Override
public String execute() throws Exception {
return SUCCESS;
}
/* GETTERS AND SETTERS */
}
您必须为struts.xml中的每个标记指定完全限定的类名...