1

我将 Strust2 用于表示层。我有带有下拉列表的 struts 表单,它与 java 对象(应用程序)列表绑定。下拉显示应用程序对象列表,用户可以选择一个应用程序并提交。当在Action类中检索用户输入值时,接收值类型为“String”,我们不能直接从struts下拉中检索对象吗,以我为例“应用程序”对象

   private List<Application> applicaionList = new ArrayList<Application>();
   @Autowired
   private ApplicationService applicationService;
   private Application application;

   public void loadTheForm(){
       applicationList = applicationService.findAll();
   }

   public void submitForm(){
       Document doc = new Document();
       doc.setApplication(application);

   }
   //Getter Setters...

}

应用程序.jsp

<s:form action ="submitForm">
    <s:select list ="applicationList" headerValue="---Select---" headerKey="-1" name="application"/>
</s:form>

struts.xml

   <action name="submitForm" class="com.ActionSupport" method="submitForm">
            <result name="success" type="tiles">/newAdminDocumentRequired.tiles</result>
   </action>

当用户从下拉列表中选择值并提交时,提交的值是字符串,我们不能直接在Struts中取对象吗,如果我们不能获取选择值的对象,我们怎么能得到呢?

谢谢你,乌德西卡

4

2 回答 2

2

我从你的问题中得到的是

 1. You have to show list of applications as drop-down.
 2. User selects one application and submit the form.
 3. You have to retrieve the selected application and perform some action with it.

我假设您的 Application 类具有所有应用程序独有的属性“id”。还有一个应用程序名称,您必须向用户显示它。所以现在,我会按如下方式解决这个问题

  <s:form action ="submitForm">
        <s:select list ="applicationList" headerValue="---Select---" headerKey="-1" key="application" listKey="id" listValue="applicationName"/>
    </s:form>

现在,这个标签将创建一个下拉菜单,如下所示

<select name="application">
    <option value="-1" selected="selected">---Select---</option>
    <option value="1">Demo 1 App</option>
    <option value="2">Demo 2 App</option>
    <option value="3">Demo 3 App</option>
    <option value="4">Demo 4 App</option> 
   </select>

请注意,OPTION 元素中的值 (1,2,3,4) 是 application.id,titles(Demo 1 App, Demo 2 App,etc) 是 application.applicationName。

现在,用户将选择并提交。所选应用程序的 id 将发送到参数“application”中的 struts 操作。在行动中,您可以这样做

public MyClass extends ActionSupport{

   private List<Application> applicaionList = new ArrayList<Application>();
   @Autowired
   private ApplicationService applicationService;
   private **String** application;

   public void loadTheForm(){
       applicationList = applicationService.findAll();
   }

   public void submitForm(){
       Application varApp = applicationService.findApplicationById(getApplication());
       Document doc = new Document();
       doc.setApplication(varApp);

   }
   //Getter Setters...

}

请注意,我已将应用程序类型更改为字符串。是的,我认为您不能直接从下拉列表中传递对象。

希望能帮助到你。

于 2012-11-12T06:08:16.200 回答
0
Dropdown with object attributes 
Here Example is shown for store object

//存储Bean类

calss Store{
private int storeId;
private String storeName;

//getter setter

}

//动作类 //设置struts2动作类中的列表(模型驱动)

List<Store> storeList=new ArrayList<>();

//JSP页面

<s:select id="store" name="store" headerKey="-1"
                                headerValue="Select Store " list="storeList"
                                listKey="storeId" listValue="storeId"
                                value="%{IteratorList[#status.index].itemBase.{storeId}}"
                            /></td>
于 2017-07-16T06:06:36.057 回答