我正在使用struts2 + spring DI来自动装配bean。我想创建一个处理保存多行的表单。我的想法是在bean中创建一个bean。
CreatePeopleAction.java
public class CreatePeopleAction extends BaseAction {
@Autowired
private PeopleManager PeopleManager;
@Autowired
private PeopleCreateBean peopleCreateBean;
@Autowired
private java.util.ArrayList<PeopleItemsBean> peopleItemsBean;
public String create() throws Exception {
Map<String, Object> result = new HashMap<String, Object>();
result = peopleManager.createPeople(peopleCreateBean);
if (result.get(Constants.ACTION_MESSAGE) != null)
setMessage(result.get(Constants.ACTION_MESSAGE).toString());
return result.get(Constants.ACTION_RESULT).toString();
}
--setter --getter
}
PeopleCreateBean.java
@Component
public class PeopleCreateBean {
private List<PeopleItemsBean> peopleItemsBean;
private ArrayList<MasterCode> nationalityOptions = null;
private ArrayList<MasterCode> genderOptions = null;
private ArrayList<MasterCode> salutationOptions = null;
private ArrayList<MasterCode> designationOptions = null;
private String name;
private Integer designation;
private String passportNo;
private Integer nationality;
private Integer gender;
private Integer salutation;
--setter getter
}
PeopleItemsBean.java
public class PeopleItemsBean extends java.util.ArrayList {
private String name;
private String designation;
private String passportNo;
private String nationality;
private String gender;
private String salutation;
--setter --getter
}
PeopleManagerImpl.java
@Service("peopleManager")
@Transactional(propagation=Propagation.REQUIRED, readOnly=true)
public class PeopleManagerImpl extends GenericManagerImpl<People, Integer>
implements PeopleManager {
PeopleDao peopleDao;
@Autowired
private MasterCodeManager masterCodeManager;
@Autowired
public PeopleManagerImpl(PeopleDao peopleDao) {
super(peopleDao);
this.peopleDao = peopleDao;
}
private People setPeople(PeopleCreateBean peopleCreateBean) throws Exception {
People People = null;
if(peopleCreateBean.getPeopleItemsBean()!=null && !peopleCreateBean.getPeopleItemsBean().isEmpty() && peopleCreateBean.getPeopleItemsBean().size() >0)
{
for(int i=0; i<peopleCreateBean.getPeopleItemsBean().size(); i++){
People = new People();
if (peopleCreateBean.getPeopleItemsBean().get(i).getPrCompaniesId() != null && !peopleCreateBean.getPeopleItemsBean().get(i).getPrCompaniesId().equals(Constants.STRING_EMPTY)) {
People.setPrCompaniesId(peopleCreateBean.getPeopleItemsBean().get(i).getPrCompaniesId());
}
if (peopleCreateBean.getPeopleItemsBean().get(i).getName() != null && !peopleCreateBean.getPeopleItemsBean().get(i).getName().equals(Constants.STRING_EMPTY)) {
People.setName(peopleCreateBean.getPeopleItemsBean().get(i).getName());
}
if (peopleCreateBean.getPeopleItemsBean().get(i).getDesignation() != null && !peopleCreateBean.getPeopleItemsBean().get(i).getDesignation().equals(Constants.STRING_EMPTY)) {
People.setDesignation(Integer.parseInt(peopleCreateBean.getPeopleItemsBean().get(i).getDesignation()));
}
if (peopleCreateBean.getPeopleItemsBean().get(i).getPassportNo() != null && !peopleCreateBean.getPeopleItemsBean().get(i).getPassportNo().equals(Constants.STRING_EMPTY)) {
People.setPassportNo(peopleCreateBean.getPeopleItemsBean().get(i).getPassportNo());
}
if (peopleCreateBean.getPeopleItemsBean().get(i).getNationality() != null && !peopleCreateBean.getPeopleItemsBean().get(i).getNationality().equals(Constants.STRING_EMPTY)) {
People.setNationality(Integer.parseInt(peopleCreateBean.getPeopleItemsBean().get(i).getNationality()));
}
if (peopleCreateBean.getPeopleItemsBean().get(i).getGender()!= null && !peopleCreateBean.getPeopleItemsBean().get(i).getGender().equals(Constants.STRING_EMPTY)) {
People.setGender(Integer.parseInt(peopleCreateBean.getPeopleItemsBean().get(i).getGender()));
}
if (peopleCreateBean.getPeopleItemsBean().get(i).getSalutation() != null && !peopleCreateBean.getPeopleItemsBean().get(i).getSalutation().equals(Constants.STRING_EMPTY)) {
People.setSalutation(Integer.parseInt(peopleCreateBean.getPeopleItemsBean().get(i).getSalutation()));
}
if (peopleCreateBean.getPeopleItemsBean().get(i).getEffectiveDate() != null && !peopleCreateBean.getPeopleItemsBean().get(i).getEffectiveDate().equals(Constants.STRING_EMPTY)) {
People.setEffectiveDate(peopleCreateBean.getPeopleItemsBean().get(i).getEffectiveDate());
}
if (peopleCreateBean.getPeopleItemsBean().get(i).getExpiryDate() != null && !peopleCreateBean.getPeopleItemsBean().get(i).getExpiryDate().equals(Constants.STRING_EMPTY)) {
People.setExpiryDate(peopleCreateBean.getPeopleItemsBean().get(i).getExpiryDate());
}
if (peopleCreateBean.getPeopleItemsBean().get(i).getStatus() != null && !peopleCreateBean.getPeopleItemsBean().get(i).getStatus().equals(Constants.STRING_EMPTY)) {
People.setStatus(Integer.parseInt(peopleCreateBean.getPeopleItemsBean().get(i).getStatus()));
}
}
}
return people;
}
//trying to save..iteration.
@Transactional(propagation=Propagation.REQUIRED, readOnly=false)
@Override
public Map<String, Object> createPeople(PeopleCreateBean PeopleCreateBean) {
Map<String, Object> result = new HashMap<String, Object>();
result.put(Constants.ACTION_RESULT, Action.SUCCESS);
try {
if(PeopleCreateBean.getPeopleItemsBean()!=null && !PeopleCreateBean.getPeopleItemsBean().isEmpty() && PeopleCreateBean.getPeopleItemsBean().size() >0)
{
for(int i=0; i<PeopleCreateBean.getPeopleItemsBean().size(); i++){
People People = queryPrPeople(PeopleCreateBean.getPeopleItemsBean().get(i).getName());
if (prPeople == null ) {
prPeopleDao.insert(setPrPeople(PeopleCreateBean));
result.put(Constants.ACTION_MESSAGE, BLConstant.USERCREATIONSTATUS_SUCCESS);
result.put(Constants.ACTION_RESULT, Action.SUCCESS);
} else {
result.put(Constants.ACTION_MESSAGE, BLConstant.USERCREATIONSTATUS_FAILED_USEREXISTS);
}
}
}
} catch (Exception e) {
result.put(Constants.ACTION_MESSAGE, BLConstant.USERCREATIONSTATUS_FAILED);
log.error(e);
e.printStackTrace();
}
return result;
}
--setter --getter
}
AddPeople.jsp
<s:form id="frmPeopleAdd" action="createPeopleCreateAction" method="post" validate="false" theme="css_xhtml">
<s:include value="People_tab.jsp"/>
<table border="0" cellspacing="0" cellpadding="0" width="100%" align="center">
<s:set name="varMessage" value="PeopleCreateBean.txtMessage"/>
<s:hidden name="PeopleCreateBean.PeopleItemsBean"></s:hidden>
<s:if test="%{#varMessage != null && #varMessage != ''}">
<tr>
<td> </td>
</tr>
<tr>
<td>
<table width="95%" border="0" cellspacing="0" cellpadding="0" align="center">
<tr>
<td class="alertMessage" align="left"><s:property value="PeopleCreateBean.txtMessage"/></td>
</tr>
</table>
</td>
</tr>
<tr>
<td> </td>
</tr>
</s:if>
<tr>
<td><table width="95%" border="0" cellspacing="0" cellpadding="0" align="center">
<tr>
<td class="mainheader">People</td>
</tr>
</table></td>
</tr>
<tr>
<td>
<table border="0" cellspacing="0" cellpadding="0" width="95%" align="center" id="myTable" class="order-list">
<thead>
<tr>
<td class="fieldname">No</td>
<td class="fieldname"> </td>
<td class="fieldname">Nationality <span class="mandatoryMessage">*</span></td>
<td class="fieldname">NRIC/Passport No <span class="mandatoryMessage">*</span></td>
<td class="fieldname">Salutation</td>
<td class="fieldname">Name <span class="mandatoryMessage">*</span></td>
<td class="fieldname">Designation <span class="mandatoryMessage">*</span></td>
<td class="fieldname">Gender <span class="mandatoryMessage">*</span></td>
</tr>
</thead>
<tbody>
<s:if test="PeopleItemsBean != null">
<%int i=0;%>
<s:iterator value="PeopleItemsBean" status="stat" >
<%i++;%>
<tr>
<td width="5%" class="fieldContent"><span><s:property value="%{#stat.count}"/></span></td>
<td width="5%" class="fieldContent"></td>
<td class="fieldContent">
<s:select headerKey="-1" headerValue=" Please Select " list="PeopleCreateBean.nationalityOptions" listKey="id" listValue="masterCodeMembersDesc" name="PeopleItemsBean[%{#stat.index}].nationality" />
</td>
<td width="15%" class="fieldContent"><s:textfield name="PeopleItemsBean[%{#stat.index}].passportNo" size="20" style="width:90%"/></td>
<td class="fieldContent">
<s:select headerKey="-1" headerValue=" Please Select " list="PeopleCreateBean.salutationOptions" listKey="masterCodeMember" listValue="masterCodeMembersDesc" name="PeopleItemsBean[%{#stat.index}].salutation" />
</td>
<td width="20%" class="fieldContent"><s:textfield name="PeopleItemsBean[%{#stat.index}].name" size="20" style="width:90%"/></td>
<td class="fieldContent">
<s:select headerKey="-1" headerValue=" Please Select " list="PeopleCreateBean.designationOptions" listKey="id" listValue="masterCodeMembersDesc" name="PeopleItemsBean[%{#stat.index}].designation" />
</td>
<td class="fieldContent">
<s:select headerKey="-1" headerValue=" Please Select " list="PeopleCreateBean.genderOptions" listKey="id" listValue="masterCodeMembersDesc" name="PeopleItemsBean[%{#stat.index}].gender" />
</td>
</tr>
</s:iterator>
</s:if>
--and the rest.....
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="People" extends="struts-default" namespace="/">
<result-types>
<result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult"/>
</result-types>
<action name="*PeopleCreateAction" method="{1}"
class="com.ox.people.action.PeopleCreateAction">
<interceptor-ref name="params"/>
<interceptor-ref name="prepare"/>
<interceptor-ref name="basicStack"/>
<result name="success" type="tiles">/peopleAdd.tiles</result>
</action>
</package>
</struts>
我收到了错误
java.lang.ClassCastException: com.opensymphony.xwork2.util.XWorkList cannot be cast to com.ox.people.action.model.PeopleItemsBean
at com.ox.people.service.PeopleManagerImpl.createPeople(PeopleManagerImpl.java:68)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:309)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:108)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
at com.sun.proxy.$Proxy22.createPeople(Unknown Source)
at com.ox.people.action.PeopleCreateAction.create(PeopleCreateAction.java:105)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:453)
at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:292)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:255)
at org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:256)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
at com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:176)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
at com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:265)
at org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
at com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:138)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:211)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:211)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
at com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:190)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
at org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
at org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:90)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
at org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:243)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
at com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:100)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
at com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:141)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
at com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:145)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
at com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:171)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
at com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:176)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
at org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
at com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:192)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:187)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
at com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:171)
[18/10/2013 16:26:39.204] ERROR PeopleManagerImpl - java.lang.ClassCastException: com.opensymphony.xwork2.util.XWorkList cannot be cast to com.ox.people.action.model.PeopleItemsBean
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:211)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:54)
at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:511)
at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.__invoke(StandardContextValve.java:123)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.__invoke(StandardHostValve.java:171)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1852)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
at java.lang.Thread.run(Thread.java:680)
如何从jsp获取bean(PeopleItemsBean)并保存到数据库中? 任何想法?
由于
MRizq