【问题标题】:Java Objects with Struts Drop down带有 Struts 的 Java 对象 Drop down
【发布时间】:2012-11-12 05:17:12
【问题描述】:

我将 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中不能直接取对象吗,如果不能取到选中值的对象怎么办?

谢谢你, 乌德西卡

【问题讨论】:

  • 你能重新表述一下这个问题吗?我无法理解你想要什么。
  • 我现在改了,想知道用struts2不能直接获取对象吗?
  • 好的,我已经添加了struts.xml的部分,这里我没有使用execute()方法
  • 我可能很累,但我仍然没有抓住重点。请在一段中解释你想要什么。特别是当您说“...直接在 Struts 中获取对象...”时,“对象”是什么。猜测一下,您正在从下拉菜单中向操作发送一个值,那么您希望该值采用什么类型而不是字符串?
  • 您有一个名为 com.ActionSupport 的用户定义类?

标签: struts2


【解决方案1】:

我从你的问题中得到的是

 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 将发送到参数“应用程序”中的 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...

}

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

希望对你有帮助。

【讨论】:

  • 非常感谢,我想知道,所以我们不能直接从下拉列表中传递对象,再次感谢。
【解决方案2】:
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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-01
    • 1970-01-01
    • 2017-12-01
    • 2023-03-17
    • 2015-12-31
    • 2011-02-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多