我的应用中,有个产品由哪个单位供应,所以在录入产品的时候供应商(厂家)希望是下来选择:
使用<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 {
@Autowired
private ProductService productService;
@Autowired
private 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 {
@Autowired
private 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"
class="model.activion.ProductAction">
<result name="input">/jsp/product.jsp</result>
</action>
<action name="addProduct" class="model.activion.ProductAction"
method="saveProduct">
<result name="success">/jsp/success.jsp</result>
<result name="error">/jsp/error.jsp</result>
<result name="input">/jsp/product.jsp</result>
</action>
</package>
</struts>
JSP界面:product.jsp
<%@ 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>
分享到:
相关推荐
本书是广受赞誉的Struts 2优秀教程,它全面而深入地阐述了Struts 2的各个特性,并指导开发人员如何根据遇到的问题对症下药,选择使用最合适的特性。作者处处从实战出发,在丰富的示例中直观地探讨了许多实用的技术,...
下载放到Myeclips里就可以运行、里面包含struts2环境搭建(点击按钮)、struts2+dwr整合(在文本框中输入值后会访问后台,给select动态增加option赋值、点击按钮又会显示会select被选中的option的value和text值)、...
此代码适合所有下拉列表取值;一个项目所有的下拉列表只需要这一个公用方法,接下来为大家详细介绍下具体实现步骤,感兴趣的朋友可以参考下,希望可以帮助到你
struts2+dwr整合实例,在文本框中输入值。会到后台查询,给select标签赋值,要是想实现级联下拉框,我想你看完应该能做出来。
书中介绍了如何利用Struts 2 来解决Web 应用开发中的常见问题,同时还深入浅出地探讨了许多能帮助程序员编写Struts 2 应用程序的技巧,如管理页面导航活动、输入验证、国际化和本地化、对Ajax 的支持,等等。...
\contentsline {chapter}{Contents}{2}{section*.1} {1}Java基础}{17}{chapter.1} {1.1}基本语法}{17}{section.1.1} {1.2}数字表达方式}{17}{section.1.2} {1.3}补码}{19}{section.1.3} {1.3.1}总结}{23}{...
深田之星Database2Sharp,是一个NHibernate、Castle-ActiveRecord、Enterprise Library和PetShop架构的C#代码和Java代码生成工具,提供了对MS Sql2000、MS Sql2005、Oracle、Mysql、Access的支持;可以生成各种架构...
而且消耗较低的CPU4、易于整合:Beetl能很容易的与各种web框架整合,如Spring MVC,JFinal,Struts,Nutz,Jodd,Servlet等。5、支持模板单独开发和测试,即在MVC架构中,即使没有M和C部分,也能开发和测试模板。6、...