我有以下来源:
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:标记“选择”,字段“列表”,名称“ product.search.category”:所请求的列表键“类别”无法解析为集合/数组/地图/枚举/迭代器类型。例如:人物或人物。{name}-[未知位置]
我只希望从给定的ArrayList<String>(List<String>)中填充DropDownList ,但它不起作用。如果我直接设置列表内容,则效果很好。
ArrayList<String>
List<String>
<s:select list = "categories" key = "product.search.category" />
您正在列出a,List<String>并尝试通过OGNL .(点表示法)访问不存在的字段。
.
在OGNL中
product.search.category
相当于Java
getProduct().getSearch().getCategory()
由于您正在列出字符串,因此只需省略key属性,因为您的键和值都将是String本身。
看来你是混淆key与name太:key是的关键<option>因素,同时name是Action的属性将接收通过其二传手所选择的值。
key
name
<option>
<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中的每个标签指定完全限定的类名称。