使用Struts 2填充组合框时出现问题

时间:2013-07-04 15:25:32

标签: java jsp struts2 combobox ognl

当我在文件中使用以下代码时,我在Struts2中设置combobox标记的字段时遇到了问题

SUCCESS.JSP

<s:form>
  <s:combobox label="Select and extract a field:" name="fieldname" 
            headerValue="--- Please Select ---" headerKey="1"
            list="fields" listValue="myField" />
</s:form>

另一方面,我可以使用 more 直接方式填充combobox,但这对我来说只对确保项目结构正确无误是有用的:

<s:form>
   <s:combobox label="Select and extract a field: " name="fieldsNames"
        headerValue="--- Please Select ---"
        headerKey="1" list="{'1','2','3','4','5','6'}"  />
</s:form>

我对任何建议持开放态度,因为我已经尝试了很多东西,而且很明显我被卡住了。

其余的类和XML文件是:

COMBOBOX.JAVA

    public class comboboxTag extends ActionSupport{

    private List<String> fields;
    private String myField;
    private String fieldName;

    public String getFieldName() 
    {
    return fieldName;
    }

    public void setFieldName(String fieldName) 
   {
      this.fieldName = fieldName;
   }

   public String getField() 
  {
  return myField;
  }

  public void setField(String field) 
  {
        this.myField = field;
  }

 public String execute()throws Exception
 {

  fields = new ArrayList<String>();

  fields.add("1");
  fields.add("2");
  fields.add("3");
  fields.add("4");
  return SUCCESS;

  }

  public List<String> getFields()
  {
  return fields;      
  }

 }

STRUTS.XML

<struts>

<constant name="struts.multipart.maxSize" value="19000000" />

<package name="fileUploadPackage" extends="struts-default">

    <interceptors>
        <interceptor-stack name="fileUploadStack">
            <interceptor-ref name="exception" />
            <interceptor-ref name="alias" />
            <interceptor-ref name="servletConfig" />
            <interceptor-ref name="prepare" />
            <interceptor-ref name="i18n" />
            <interceptor-ref name="chain" />
            <interceptor-ref name="debugging" />
            <interceptor-ref name="profiling" />
            <interceptor-ref name="scopedModelDriven" />
            <interceptor-ref name="modelDriven" />
            <interceptor-ref name="fileUpload">
                <param name="maximumSize"> 18900000 </param>
                <param name="allowedTypes"> application/pdf,text/plain</param>
            </interceptor-ref>
            <interceptor-ref name="checkbox" />
            <interceptor-ref name="staticParams" />
            <interceptor-ref name="actionMappingParams" />
            <interceptor-ref name="params">
                <param name="excludeParams"> dojo\..*,^struts\..*</param>
            </interceptor-ref>
            <interceptor-ref name="conversionError" />
            <interceptor-ref name="validation">
                <param name="excludeMethods"> input,back,cancel,browse</param>
            </interceptor-ref>
            <interceptor-ref name="workflow">
                <param name="excludeMethods"> input,back,cancel,browse</param>
            </interceptor-ref>
        </interceptor-stack>
    </interceptors>

    <!-- FILE UPLOAD ACTION -->
    <action name="fileUpload" class="org.apache.fileUploadStruts.FileUploadAction">
        <interceptor-ref name="fileUploadStack" />
        <result name="input">/index.jsp</result>
        <result name="success">/success.jsp</result>
    </action>

            <action name="comboboxTag" class="org.apache.fileUploadStruts.comboboxTag">
         <result>/success.jsp</result>
    </action>  

</package>

INDEX.JSP

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  <title>Upload a File!!</title>
  <s:head />
</head>
<body>
  <s:form action="fileUpload" method="post" enctype="multipart/form-data" >
      <s:file name="file" label="User File: " />
      <s:submit />
     </s:form>   
</body>
</html>

2 个答案:

答案 0 :(得分:2)

您不需要对组合框进行单独操作。只需创建适当的getter,即可返回FileUploadAction

中的列表
public List<String> getFields() {
  List<String> fields = new ArrayList<String>();
  fields.add("1");
  fields.add("2");
  fields.add("3");
  fields.add("4");
  return fields;
}

不要使用listValue,因为列表中有简单的字符串。

<s:form>
  <s:combobox label="Select and extract a field:" name="fieldname" 
        headerValue="--- Please Select ---" headerKey="1"
        list="fields" />
</s:form>

答案 1 :(得分:1)

您不应将listValue="myField" listKey="id"List<String>一起使用,请删除此属性。