读书人

struts2 lt;s:selectgt;赋值步骤

发布时间: 2012-10-25 10:58:57 作者: rapoo

struts2 <s:select>赋值方法
我的应用中,有个产品由哪个单位供应,所以在录入产品的时候供应商(厂家)希望是下来选择:
使用<s:select>一直报错:
tag 'select', field 'list', name 'supplierId': The requested list key '#product.getComPanys' could not be resolved as a collection/array/map/enumeration/iterator type. Example: people or people.{name} - [unknown location]

我分别采用了4中方法:上面报错只是其中一种,但4中方法报错都一致。。。我google、baidu了 看了N多贴依然是解决不了超级郁闷,还请给位多指教!!!

下面贴出我的类以及处理方法:

ProductAction

package model.activion;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import model.service.CompanyService;import model.service.ProductService;import org.springframework.beans.factory.annotation.Autowired;import util.UtilRandow;import util.UtilString;import com.opensymphony.xwork2.ActionSupport;import entity.Company;import entity.Product;@SuppressWarnings("serial")public class ProductAction extends ActionSupport {@Autowiredprivate ProductService productService;@Autowiredprivate CompanyService companyService;private String productNo;private String productName;private String supplierId;         private List<Company> comPanys;   [color=red]//第一种与第二种都采用List赋值[/color]private HashMap  mapCom;          [color=red]//第三种 才偶那个Map赋值[/color]    //保存产品    public String   saveProduct(){    Product product = new Product();    product.setProductNo("CP"+UtilRandow.getDateRandow());    product.setProductName(productName);        product.setSupplierId(supplierId);    boolean falge = productService.addProduct(product);        if (falge==true) return SUCCESS;    else return ERROR;    }  public String getProductNo() {return productNo;}public void setProductNo(String productNo) {this.productNo = productNo;}public String getProductName() {return productName;}public void setProductName(String productName) {this.productName = productName;}       public void setSupplierId(String supplierId) {this.supplierId = supplierId;}public String getSupplierId() {return supplierId;}       [color=red]//Map 取值方法获取数据库种所有的company [/color]public HashMap getMapCom() {List<Company> ls = this.companyService.queryCompany(null, null);       for(Company c :ls){    mapCom.put("companyId",c.getCompanyId());    mapCom.put("companyName",  c.getCompanyName());       }return mapCom;}public void setMapCom(HashMap mapCom) {this.mapCom = mapCom;}       [color=red]//List 取值方法获取数据库种所有的company [/color]       public List<Company> getComPanys() {List<Company> comPanys = new ArrayList<Company>();comPanys = this.companyService.queryCompany(null, null);return comPanys;}public void setComPanys(List comPanys) {this.comPanys = comPanys;}}


UtilBean:只是其中一个方法有使用 //第四种采用<s:bean>的方式赋值
package model.bean;import java.util.ArrayList;import java.util.List;import model.service.CompanyService;import org.springframework.beans.factory.annotation.Autowired;import util.UtilString;import entity.Company;public class UtilBean {@Autowiredprivate CompanyService companyService;private List<Company> lc  = new ArrayList<Company>();public List<Company> getLc() {List<Company> listCom  = this.companyService.queryCompany(null, null);if (UtilString.isEmpty(listCom)) lc = null;else lc = listCom;return lc;}public void setLc(List<Company> lc) {this.lc = lc;}}

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> <package name="model.activion" extends="struts-default">                <!-- 输入校验--> <action name="validateProduct" name="code"><%@ page language="java" contentType="text/html; charset=UTF-8"%><%@ taglib prefix="s" uri="/struts-tags"%><html><head><title>产品信息</title>                <s:head theme="ajax"/></head><body><center><s:tabbedPanel id="productList" theme="ajax" doLayout="false" closeButton="pane"labelposition="top" selectedTab="product" ><s:div id="product" theme="ajax" label="基本信息"><s:form action="validateProduct" validate="true"><s:textfield name="productNo" label="产品编号" readonly="true" /><s:textfield name="productName" label="产品名称" required="true" /><!-- 第1种  List--><s:select list="companys" listKey="companyId"listValue="companyName" name="supplierId" label="厂家名称"headerKey="" headerValue="" /><!-- 第2种 List--><s:action name="product" id="getComPanys" /><s:select list="#product.getComPanys" listKey="companyId"listValue="companyName" name="supplierId" label="厂家名称"headerKey="" headerValue="" /><!-- 第3种 Map--><s:select list="mapCom" key="companyId"value="companyName" name="supplierId" label="厂家名称"headerKey="" headerValue="" /><!-- 第4种 List--><s:bean id="ub" name="model.action.UtilBean" /><s:select list="#ub.lc" listKey="companyId"listValue="companyName" name="supplierId" label="厂家名称"headerKey="" headerValue="" /><s:submit value="确定" action="addProduct" /><s:reset value="重置" /></s:form></s:div><!-- 图片上传未处理  --><s:div id="image" theme="ajax" label="图片上传"><s:form><s:textfield name="productName" label="产品名称"/><s:file name="file" accept="jpeg/gif/*" label="图片一"/><s:file name="file" accept="jpeg/gif/*" label="图片二"/><s:file name="file" accept="jpeg/gif/*" label="图片三"/><s:submit value="上传" /><s:reset value="重置" /></s:form></s:div></s:tabbedPanel></center></body></html>
package entity;import javax.persistence.Column;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;import javax.persistence.Table;import javax.persistence.Temporal;import javax.persistence.TemporalType;import org.hibernate.annotations.GenericGenerator;@Entity@Table(name = "bsc_company", catalog = "jxc_db")public class Company implements java.io.Serializable {// Fieldsprivate String companyId;private String companyNo;private String companyName; @GenericGenerator(name = "generator", strategy = "uuid.hex")@Id@GeneratedValue(generator = "generator")@Column(name = "company_id", unique = true, nullable = false, length = 50)public String getCompanyId() {return this.companyId;}public void setCompanyId(String companyId) {this.companyId = companyId;}@Column(name = "company_no", length = 50)public String getCompanyNo() {return this.companyNo;}public void setCompanyNo(String companyNo) {this.companyNo = companyNo;}@Column(name = "company_name", length = 50)public String getCompanyName() {return this.companyName;}public void setCompanyName(String companyName) {this.companyName = companyName;}} List<Company> listCom = this.companyService.queryCompany(null, null);
返回一定有值!!! 3 楼 laojiang 2009-02-01 总结的好! 4 楼 daoyongyu 2009-04-10 在第一种list=companys改成list=%{companys}试试。 5 楼 xiaofengtoo 2009-04-12 谢谢 楼上兄弟 我试试看 !!!

读书人网 >软件架构设计

热点推荐