如何将Struts 2标签<s:checkbox>中复选框的选中值复制到操作类中

时间:2016-06-11 20:15:48

标签: java jsp checkbox struts2 ognl

在Struts 2表单中,我正在遍历列表并显示记录和复选框,以执行更新/删除操作。

我的要求是如果选中该复选框(表示如果值为true),则对所选记录执行所需的操作。但每次我将复选框的错误值输入到我的动作类中时,我也没有将更新的文本字段值转换为动作类。

以下是action,form和jsp代码:

    // Initial action class to prepare the list to display in jsp

    public class SetupCategoryMod implements ServletRequestAware, ModelDriven<ManageFundCategoryForm> {
        private HttpServletRequest request;
        private ManageFundCategoryForm theForm =  new ManageFundCategoryForm();
        public String execute() throws Exception {  
            Vector list = doGetFundCatergory(); // This list iam getting fine from DB
            theForm.setWs(list); // Setting to pojo
            return "success";
        }
        @Override
        public void setServletRequest(HttpServletRequest httpServletRequest) {
            this.request = httpServletRequest;
        }
        @Override
        public ManageFundCategoryForm getModel() {
            return theForm;
        }
    }

    // POJO for data binding 

    public class ManageFundCategoryForm  {
        private Vector<FundCategoryData> ws;
        // setter and getter for the property here
    }

    // supporting pojo for form binding 

    public class FundCategoryData implements Serializable {
        private static final long serialVersionUID = 8509788495819453084L;
        private String fname;
        private boolean action;

       //setters and getters for the properties here
    }

jsp

    <s:form action="managefundcategory">
    <s:iterator value="ws" status="stat">               
    <tr>
        <td align="center"><s:checkbox name="ws[%{#stat.index}].action" value="action"/>
        <td align="center"><s:textfield name="ws[%{#stat.index}].fname" size="26"/>
    </s:iterator>
    </s:form>

提交具有更新值的上述表单后的操作。但是更新后的价值并不适合采取行动。旧的价值观再次出现。即使在检查复选框之后,我只能在我的行动类中获得“虚假”的价值

    public class ManageFundCategoryAction implements ServletRequestAware, ModelDriven<ManageFundCategoryForm> {
        private HttpServletRequest request;
        private ManageFundCategoryForm theForm = new ManageFundCategoryForm();
        public String execute() throws Exception {                  
            Iterator itr = theForm.getWs().iterator();
            while(itr.hasNext()){ //Printing the values to check if the updated values are coming or not.
                FundCategoryData fdd = (FundCategoryData)itr.next();
                System.out.println(fdd.isAction()+" , "+fdd.getFname());
            }

            //send the updated list to db
            if(update(theForm.getWs()) != 0){
                return "success";
            }
        }
        @Override
        public ManageFundCategoryForm getModel() {
            return theForm;
        }
        @Override
        public void setServletRequest(HttpServletRequest httpServletRequest) {
            this.request = httpServletRequest;
        }
    }

请建议我如何将textfield和checkbox的更新值添加到提交的操作类中?

提交表单后,我在日志中收到以下异常

20:13:06,846 ERROR [com.opensymphony.xwork2.util.InstantiatingNullHandler] (http-localhost/127.0.0.1:8080-11) Could not create and/or set value back on to object: ognl.OgnlException: ws [java.lang.IllegalArgumentException: java.lang.ClassCastException@1f17f83]
    at ognl.ObjectPropertyAccessor.setPossibleProperty(ObjectPropertyAccessor.java:83) [ognl-2.6.11.jar:]
    at ognl.ObjectPropertyAccessor.setProperty(ObjectPropertyAccessor.java:131) [ognl-2.6.11.jar:]
    at com.opensymphony.xwork2.util.OgnlValueStack$ObjectAccessor.setProperty(OgnlValueStack.java:68) [xwork-2.0.4.jar:]
    at ognl.OgnlRuntime.setProperty(OgnlRuntime.java:1656) [ognl-2.6.11.jar:]
    at ognl.ASTProperty.setValueBody(ASTProperty.java:101) [ognl-2.6.11.jar:]
    at ognl.SimpleNode.evaluateSetValueBody(SimpleNode.java:177) [ognl-2.6.11.jar:]
    at ognl.SimpleNode.setValue(SimpleNode.java:246) [ognl-2.6.11.jar:]
    at ognl.Ognl.setValue(Ognl.java:476) [ognl-2.6.11.jar:]
    at ognl.Ognl.setValue(Ognl.java:494) [ognl-2.6.11.jar:]
    at com.opensymphony.xwork2.util.InstantiatingNullHandler.nullPropertyValue(InstantiatingNullHandler.java:106) [xwork-2.0.4.jar:]
    at ognl.ASTProperty.getValueBody(ASTProperty.java:94) [ognl-2.6.11.jar:]
    at ognl.SimpleNode.evaluateGetValueBody(SimpleNode.java:170) [ognl-2.6.11.jar:]
    at ognl.SimpleNode.getValue(SimpleNode.java:210) [ognl-2.6.11.jar:]
    at ognl.ASTChain.setValueBody(ASTChain.java:168) [ognl-2.6.11.jar:]
    at ognl.SimpleNode.evaluateSetValueBody(SimpleNode.java:177) [ognl-2.6.11.jar:]
    at ognl.SimpleNode.setValue(SimpleNode.java:246) [ognl-2.6.11.jar:]
    at ognl.Ognl.setValue(Ognl.java:476) [ognl-2.6.11.jar:]
    at com.opensymphony.xwork2.util.OgnlUtil.setValue(OgnlUtil.java:186) [xwork-2.0.4.jar:]
    at com.opensymphony.xwork2.util.OgnlValueStack.setValue(OgnlValueStack.java:158) [xwork-2.0.4.jar:]
    at com.opensymphony.xwork2.util.OgnlValueStack.setValue(OgnlValueStack.java:146) [xwork-2.0.4.jar:]
    at com.opensymphony.xwork2.interceptor.ParametersInterceptor.setParameters(ParametersInterceptor.java:193) [xwork-2.0.4.jar:]
    at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:159) [xwork-2.0.4.jar:]
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:86) [xwork-2.0.4.jar:]
    at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224) [xwork-2.0.4.jar:]
    at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223) [xwork-2.0.4.jar:]
    at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455) [xwork-2.0.4.jar:]
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221) [xwork-2.0.4.jar:]
    at com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:105) [xwork-2.0.4.jar:]
    at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224) [xwork-2.0.4.jar:]
    at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223) [xwork-2.0.4.jar:]
    at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455) [xwork-2.0.4.jar:]
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221) [xwork-2.0.4.jar:]
    at org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:83) [struts2-core-2.0.11.jar:]
    at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224) [xwork-2.0.4.jar:]
    at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223) [xwork-2.0.4.jar:]
    at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455) [xwork-2.0.4.jar:]
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221) [xwork-2.0.4.jar:]
    at org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:207) [struts2-core-2.0.11.jar:]
    at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224) [xwork-2.0.4.jar:]
    at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223) [xwork-2.0.4.jar:]
    at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455) [xwork-2.0.4.jar:]
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221) [xwork-2.0.4.jar:]
    at com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:74) [xwork-2.0.4.jar:]
    at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224) [xwork-2.0.4.jar:]
    at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223) [xwork-2.0.4.jar:]
    at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455) [xwork-2.0.4.jar:]
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221) [xwork-2.0.4.jar:]
    at com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:127) [xwork-2.0.4.jar:]
    at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224) [xwork-2.0.4.jar:]
    at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223) [xwork-2.0.4.jar:]
    at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455) [xwork-2.0.4.jar:]
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221) [xwork-2.0.4.jar:]
    at org.apache.struts2.interceptor.ProfilingActivationInterceptor.intercept(ProfilingActivationInterceptor.java:107) [struts2-core-2.0.11.jar:]
    at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224) [xwork-2.0.4.jar:]
    at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223) [xwork-2.0.4.jar:]
    at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455) [xwork-2.0.4.jar:]
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221) [xwork-2.0.4.jar:]
    at org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:206) [struts2-core-2.0.11.jar:]
    at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224) [xwork-2.0.4.jar:]
    at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223) [xwork-2.0.4.jar:]
    at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455) [xwork-2.0.4.jar:]
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221) [xwork-2.0.4.jar:]
    at com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:115) [xwork-2.0.4.jar:]
    at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224) [xwork-2.0.4.jar:]
    at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223) [xwork-2.0.4.jar:]
    at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455) [xwork-2.0.4.jar:]
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221) [xwork-2.0.4.jar:]
    at com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:143) [xwork-2.0.4.jar:]
    at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224) [xwork-2.0.4.jar:]
    at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223) [xwork-2.0.4.jar:]
    at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455) [xwork-2.0.4.jar:]
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221) [xwork-2.0.4.jar:]
    at com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:121) [xwork-2.0.4.jar:]
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:86) [xwork-2.0.4.jar:]
    at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224) [xwork-2.0.4.jar:]
    at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223) [xwork-2.0.4.jar:]
    at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455) [xwork-2.0.4.jar:]
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221) [xwork-2.0.4.jar:]
    at org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:170) [struts2-core-2.0.11.jar:]
    at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224) [xwork-2.0.4.jar:]
    at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223) [xwork-2.0.4.jar:]
    at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455) [xwork-2.0.4.jar:]
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221) [xwork-2.0.4.jar:]
    at com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:123) [xwork-2.0.4.jar:]
    at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224) [xwork-2.0.4.jar:]
    at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223) [xwork-2.0.4.jar:]
    at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455) [xwork-2.0.4.jar:]
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221) [xwork-2.0.4.jar:]
    at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:176) [xwork-2.0.4.jar:]
    at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224) [xwork-2.0.4.jar:]
    at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223) [xwork-2.0.4.jar:]
    at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455) [xwork-2.0.4.jar:]
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221) [xwork-2.0.4.jar:]
    at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:50) [struts2-core-2.0.11.jar:]
    at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:504) [struts2-core-2.0.11.jar:]
    at org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:419) [struts2-core-2.0.11.jar:]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:246) [jbossweb-7.4.8.Final-redhat-4.jar:7.4.8.Final-redhat-4]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214) [jbossweb-7.4.8.Final-redhat-4.jar:7.4.8.Final-redhat-4]
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:231) [jbossweb-7.4.8.Final-redhat-4.jar:7.4.8.Final-redhat-4]
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:149) [jbossweb-7.4.8.Final-redhat-4.jar:7.4.8.Final-redhat-4]
    at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:169) [jboss-as-web-7.4.0.Final-redhat-19.jar:7.4.0.Final-redhat-19]
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:145) [jbossweb-7.4.8.Final-redhat-4.jar:7.4.8.Final-redhat-4]
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:97) [jbossweb-7.4.8.Final-redhat-4.jar:7.4.8.Final-redhat-4]
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:102) [jbossweb-7.4.8.Final-redhat-4.jar:7.4.8.Final-redhat-4]
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:344) [jbossweb-7.4.8.Final-redhat-4.jar:7.4.8.Final-redhat-4]
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:856) [jbossweb-7.4.8.Final-redhat-4.jar:7.4.8.Final-redhat-4]
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:653) [jbossweb-7.4.8.Final-redhat-4.jar:7.4.8.Final-redhat-4]
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:926) [jbossweb-7.4.8.Final-redhat-4.jar:7.4.8.Final-redhat-4]
    at java.lang.Thread.run(Thread.java:745) [rt.jar:1.7.0_101]

1 个答案:

答案 0 :(得分:1)

解决方案很明显。例外情况发生在OGNL级别。因为您没有在模型中初始化ws属性,并且createIfNull配置设置为true(这是默认值),OGNL将尝试为您实例化一个属性,但它失败,因为它太旧或者不知道如何实例化该属性,因为它具有未知类型。

尝试将类型更改为List

private List<FundCategoryData> ws;