【问题标题】:how to get multiple checkbox value in struts 2 using checkbox list如何使用复选框列表在struts 2中获取多个复选框值
【发布时间】:2013-09-11 11:53:07
【问题描述】:

嗨,我是 struts 2 的新手, 有人可以告诉我如何在 struts 2 中获取多个复选框值。 到目前为止,我已经开发了如下代码。 我收到错误

tag 'checkboxlist', field 'list', name 'subscribe': The requested list key 'subscription' could not be resolved as a collection/array/map/enumeration/iterator type. Example: people or people.{name} - [unknown location]
    at org.apache.struts2.components.Component.fieldError(Component.java:240)
    at org.apache.struts2.components.Component.findValue(Component.java:333)
    at org.apache.struts2.components.ListUIBean.evaluateExtraParams(ListUIBean.java:80)
    at org.apache.struts2.components.UIBean.evaluateParams(UIBean.java:875)
    at org.apache.struts2.components.UIBean.end(UIBean.java:523)
    at org.apache.struts2.views.jsp.ComponentTagSupport.doEndTag(ComponentTagSupport.java:42)
    at jsp_servlet.__step1._jsp__tag6(__step1.java:371)
    at jsp_servlet.__step1._jsp__tag0(__step1.java:148)
    at jsp_servlet.__step1._jspService(__step1.java:86)
    at weblogic.servlet.jsp.JspBase.service(JspBase.java:34)
    at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:227)

我获取值的jsp页面是

 <link href="style.css" rel="stylesheet" type="text/css"/>
 <%@ taglib uri="/struts-tags" prefix="s" %>

 <table class="profiletable" align="center">
<tr>
    <td align="center"><h2>Upload User Profile</h2></td>
</tr>
<tr>
    <td align="center"><s:form action="upload" method="post">
<s:textfield name="fname" label="First Name"></s:textfield>
<s:textfield name="lname" label="Last Name"></s:textfield>
<s:radio name="mstatus" label="Martial Status" list="{'Single','Married'}"></s:radio>
<s:radio name="gender" label="Gender" list="{'male','female'}"></s:radio>
<s:select list="{'India','USA','UK','Germany','France','Australia'}" label="Country" name="country"></s:select>
 <s:checkboxlist label="Letter you want to subscribe" name="subscribe" list="subscription" />
  <s:submit value="upload profile" align="center"></s:submit>
</s:form></td>
</tr>
 </table>

我的jsp页面显示值是

<%@ taglib uri="/struts-tags" prefix="p" %>
<h2>Profile uploaded sucessfully</h2>
 First Name:<p:property value="fname"/><br/>
Last Name:<p:property value="lname"/><br/>
Martial status:<p:property value="mstatus"/><br/>
Gender:<p:property value="gender"/><br/>
Country:<p:property value="country"/><br/>
Letters Subscribed:<p:property value="subscribe"/>/>

我的动作文件是

package com.javapoint;

import java.util.ArrayList;
 import java.util.List;

import com.opensymphony.xwork2.ActionSupport;



 public class UploadProfile extends ActionSupport {

/**
 * 
 */
private static final long serialVersionUID = 1L;
private String fname;
private String lname;
private String mstatus;
private String gender;
private String country;
private String subscribe;
private List<String> subscription;

public String getFname() {
    System.out.println("inside getfname");
    return fname;
}
public void setFname(String fname) {
    System.out.println("inside getfname");
    System.out.println("the firstname set is "+fname);
    this.fname = fname;
}
public String getLname() {

    return lname;
}
public void setLname(String lname) {
    this.lname = lname;
    System.out.println("the lastname set is "+ lname);
}
public String getMstatus() {
    return mstatus;
}
public void setMstatus(String mstatus) {
    this.mstatus = mstatus;
}
public String getGender() {
    return gender;
}
public void setGender(String gender) {
    this.gender = gender;
}
public String getCountry() {
    return country;
}
public void setCountry(String country) {
    this.country = country;
}

public String getSubscribe() {
    return subscribe;
}
public void setSubscribe(String subscribe) {
    this.subscribe = subscribe;
}
public List<String> getSubscription() {
    return subscription;
}
public void setSubscription(List<String> subscription) {
    this.subscription = subscription;
}
public UploadProfile(){
    subscription=new ArrayList<String>();
    subscription.add("Politics");
    subscription.add("Sports");
    subscription.add("Editorial");
    subscription.add("Gadgets");
    subscription.add("Overdrive");
}
public String execute()throws Exception{
    System.out.println("inside execute");
    if(fname!=null){
    return "profileuploaded";
    }else{
        return "error";

    }
}

public String display(){

    return NONE;
}


}

我的 struts.xml 文件是

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts
Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
 <package name="default" extends="struts-default">

<action name="product" class="com.javapoint.Product">
<result name="success">welcome.jsp</result>
</action>
<action name="upload" class="com.javapoint.UploadProfile">
<result name="profileuploaded">ProfileUploaded.jsp</result>
 <result name="error">index.jsp</result>
</action>
</package>
  </struts>    

我的 web.xml 文件是

<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
  </filter-class>
 </filter>
  <filter-mapping>
   <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <welcome-file-list>
  <welcome-file>step1.jsp</welcome-file>
   </welcome-file-list>

【问题讨论】:

    标签: java jsp checkbox struts2 struts-config


    【解决方案1】:
    1. struts.xml:

    <action name="subscription_listing" method="UploadProfile" class="com.javapoint.UploadProfile" > <result name="none">/your_jsppage_where_you_have_checkboxlist</result> </action>

    2.在Uploadprofile Action类中修改Uploadprofile方法:

    public String UploadProfile(){
    subscription=new ArrayList<String>();
    subscription.add("Politics");
    subscription.add("Sports");
    subscription.add("Editorial");
    subscription.add("Gadgets");
    subscription.add("Overdrive");
    return NONE;
    

    }

    [3]。现在直接调用subscription_listing action。

    你明白了吗?

    【讨论】:

    • 嗨,杰伊,如何执行第 3 步
    • 在 url:whatever_your_path/subscription_listing.action ".action" 后缀是可选的。
    • @user251287 :意味着在 url 而不是您的 jsp 页面中调用此操作。你对我的回答满意吗?
    • 嗨,杰伊,直接调用它会正确显示启动页面(step1.jsp),但是在提交值时它仍然没有捕获复选框值,你能告诉我什么是 return none
    • 有两种方法: 1. 你调用 action 而不是 jsp 页面 2. 在你的 jsp 页面中对“subscription_listing”动作进行 ajax 调用。如果你选择way-2,那么你可以调用jsp页面(不需要调用listing action)。
    【解决方案2】:

    由于您是 Struts2 的新手,希望这个 example 可以帮助您在 struts2 中使用多个复选框

    【讨论】:

    • 嗨,开发者,我之前使用了相同的示例,并使我的 prog 完全相同,但仍然出现错误。
    • @user251287,可能你得到的是空列表,因此异常。
    • @user251287 我在演示应用程序中使用了您的代码。我刚刚从方法执行返回“profileuploaded”,页面显示良好,带有多个复选框。
    • 另外我认为动作名称“产品”的类应该是“com.javapoint.UploadProfile”
    • 您好,开发人员,操作名称产品适用于我项目中的另一个页面。它与此上传配置文件没有链接。仍然出现同样的错误。
    【解决方案3】:

    在浏览并实施上述建议之后,我已经得出了在 chekbox 的情况下直接调用动作文件的解决方案,虽然可以有替代方案,但目前我还不知道。

    问题出在我的 struts.xml 上,应该是

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts
     Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
     <struts>
     <package name="default" extends="struts-default">
    
     <action name="upload" class="com.javapoint.UploadProfile">
     <result name="profileuploaded">ProfileUploaded.jsp</result>
      <result name="error">index.jsp</result>
    
    
     </action>
    <action name="UploadProfile" class="com.javapoint.UploadProfile" method="display">
        <result name="none">/step1.jsp</result>
       </action>
    
    </package>
    </struts>    
    

    最后的动作文件是

    包com.javapoint;

    导入 java.util.ArrayList; 导入 java.util.List;

    导入 com.opensymphony.xwork2.ActionSupport;

    公共类 UploadProfile 扩展 ActionSupport {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private String fname;
    private String lname;
    private String mstatus;
    private String gender;
    private String country;
    private String[] defaultsubscription;
    private String[] yoursubscription;
    private List<String> subscription;
    
    public String getFname() {
        System.out.println("inside getfname");
        return fname;
    }
    public void setFname(String fname) {
        System.out.println("inside getfname");
        System.out.println("the firstname set is "+fname);
        this.fname = fname;
    }
    public String getLname() {
    
        return lname;
    }
    public void setLname(String lname) {
        this.lname = lname;
        System.out.println("the lastname set is "+ lname);
    }
    public String getMstatus() {
        return mstatus;
    }
    public void setMstatus(String mstatus) {
        this.mstatus = mstatus;
    }
    public String getGender() {
        return gender;
    }
    public void setGender(String gender) {
        this.gender = gender;
    }
    public String getCountry() {
        return country;
    }
    public void setCountry(String country) {
        this.country = country;
    System.out.println(country);
    }
    
    public String[] getYoursubscription() {
        return yoursubscription;
    }
    public void setYoursubscription(String[] yoursubscription) {
        this.yoursubscription = yoursubscription;
    }
    
    
    public UploadProfile(){
        System.out.println("inside constructor");
        subscription=new ArrayList<String>();
        subscription.add("Politics");
        subscription.add("Sports");
        subscription.add("Editorial");
        subscription.add("Gadgets");
        subscription.add("Overdrive");
        }
    
    public String[] getDefaultSubscription(){
        return new String [] {"Politics", "Sports"};
    }
    public List<String> getSubscription() {
        return subscription;
    }
    public void setSubscription(List<String> subscription) {
        this.subscription = subscription;
    }
    
    
    public String execute()throws Exception{
        System.out.println("inside execute");
        System.out.println(yoursubscription.toString()+"   "+subscription+"    "+defaultsubscription);
        if(fname!=null){
        return "profileuploaded";
        }else{
            return "error";
    
        }
    }
    public String display() {
        return NONE;
    }
    
    
    
    }
    

    【讨论】:

      猜你喜欢
      • 2017-03-09
      • 2019-08-16
      • 1970-01-01
      • 2016-05-02
      • 1970-01-01
      • 2015-05-20
      • 2016-07-19
      • 2015-02-11
      • 1970-01-01
      相关资源
      最近更新 更多