2013-12-11 23 views
3

我有以下來源:「選擇」 從列表中<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 ,但它不起作用。如果我直接設置列表內容,它工作正常。

回答

3
<s:select list = "categories" 
      key = "product.search.category" /> 

你列出一個List<String>和試圖訪問,通過OGNL .(點表示),不存在的領域。

在OGNL

product.search.category 

就是Java

getProduct().getSearch().getCategory() 

因爲你是上市字符串的等價物,只是省略了關鍵屬性,因爲無論你的鍵和值將是字符串本身。

看來你是在混淆keyname太:key<option>元素的關鍵,而name是行動的屬性將接收通過其二傳手所選擇的值。

<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 */ 
} 

而且你必須爲支柱每個變量指定完全限定類名.xml ...

+0

好吧,我像你提到的那樣。但只是出現以下錯誤:標記'select',字段'list',名稱'chosenCategory':請求的列表鍵'categories'不能被解析爲一個collection/array/map/enumeration/iterator類型。示例:人員或人員。{名稱} - [未知地點] 在哪裏設置列表? –

+0

如果在設置時指的是人口,我建議實現'Preparable'接口並在'prepare()'方法中加載「靜態」數據。避免使用Action的構造函數...否則,將其填充到'execute()'方法中,但在INPUT結果的情況下它將不可用於頁面。同時刪除List的setter。 –

+0

順便說一句,錯誤信息是誤導性的:只要按照我所說的關鍵字和名稱屬性進行操作即可。 –