如何在java中根据下拉菜单或单选按钮中的选择添加textArea



我修改了dspace代码的反馈形式,添加了一个类别,询问用户的"用户类型"。

    Select category = form.addItem().addSelect("category");
    category.setLabel("Please select your category");
    category.addOption("UG","Undergraduate/BS");
    category.addOption("MS","MS Student");
    category.addOption("PHD","PhD Student");
    category.addOption("FAC","Faculty");
    category.addOption("RES","Researcher");
    category.addOption("TRA","Trainee");
    category.addOption("BUS","Businessman/Private");
    category.addOption("FF","Fish farmer");
    category.addOption("OT","Other");
    String itemSelected = parameters.getParameter("category","");
    if (StringUtils.equals(itemSelected,"OT"))
    {
        TextArea other = form.addItem().addTextArea("other");
        other.setHelp("Write here if you selected Other");
        other.setValue(parameters.getParameter("other",""));
    }

我的目标是只有当用户选择"其他"时才显示文本区域。此外,如果用户选择其他字段,则文本区域应为必填字段。我怎样才能做到这一点?我还想尝试使用单选按钮而不是选择。

[EDIT]对于那些不熟悉DSpace的人,以下是我从DSpace github修改的原始代码:反馈表单

您可以使用与选择相同的单选按钮:

Radio category = form.addItem().addRadio("category");
category.setLabel("Please select your category");
category.addOption("UG", "Undergraduate/BS");
...

正如terrywb所说,java代码呈现页面服务器端。您可以制作一个两步流程,其中第一步只提交类别。但是,默认情况下包括一个隐藏的TextArea,然后使用javascript显示它更方便用户。

Item item = form.addItem("item-name","hidden");
TextArea other = item.addTextArea("other");

在PageMeta:中添加对javascript文件的引用

public void addPageMeta(PageMeta pageMeta) throws SAXException,
            WingException, UIException, SQLException, IOException,
            AuthorizeException
    {       
        pageMeta.addMetadata("javascript", "static", null, true).addContent("static/js/feedbackform.js");
        // leave the rest
        ...
    }

将javascript放在/dspace/modules/xmlui/src/main/webapp/static/js/feedbackform.js中,这样就可以为其提供服务。

此外,parameters.getParameter("category","");不会开箱即用。parameters实例变量包含/aspects/ArtifactBrowser/sitemap.xmap:中茧给它的参数

<map:match pattern="feedback">
        <map:act type="SendFeedbackAction">
                <map:transform type="FeedbackForm">
                        <map:parameter name="comments" value="{comments}"/>
                        <map:parameter name="email" value="{email}"/>
                        <map:parameter name="page" value="{page}"/>
                </map:transform>
                <map:serialize type="xml"/>
        </map:act>
        <map:transform type="FeedbackSent"/>
        <map:serialize type="xml"/>
</map:match>

SendFeedbackAction从请求中获取数据:

Request request = ObjectModelHelper.getRequest(objectModel);
String page = request.getParameter("page");

并通过将{page}添加到返回的映射中来提供该值:

    // Check all data is there
    if ((address == null) || address.equals("")
        || (comments == null) || comments.equals(""))
    {
            // Either the user did not fill out the form or this is the
            // first time they are visiting the page.
            Map<String,String> map = new HashMap<String,String>();
            map.put("page",page);
            ...
            return map;

commentemail和您希望看到的所有其他字段也是如此。强制执行一个必需的字段也应该在那里完成。

如果我是你,我会使用JTextArea而不是TextArea。。。

public void valueChanged(ListSelectionEvent e) {
    if ( e.getSource() == item) { /*Item is whatever the selected choice you want is*/ ) {
        other.setVisible(true);
    } else {
        other.setVisible(false);
    }
}

请确保您的类扩展了ActionListener!而另一个JTextArea是公共的!)

最新更新