简体   繁体   English

在Struts 2中未显示有关选择下拉菜单的错误消息

[英]Not displaying error message for a select drop down in Struts 2

I have following code 我有以下代码

Action: 行动:

private String yourSearchEngine;
private String selectedUserGroupId;   

//with both getter and setter.

public void validate(){
    if("-1".equals(getYourSearchEngine())){
        addFieldError("yourSearchEngine", getText("select search engine"));
        addressList.add("Select search engine");
    }

    if("-1".equals(getSelectedUserGroupId())){
        addFieldError("selectedUserGroupId", "select user group");
        addressList.add("Select user group");
    }

JSP page: JSP页面:

<s:select   label="Search engine:" 
            headerKey="-1" 
            headerValue="Select Search Engines"
            list="searchEngine" 
            name="yourSearchEngine" 
            id="yourSearchEngine"
            value="defaultSearchEngine" 
            tooltip="Select Search Engines" />

<s:select   label = "User Group:" 
            list = "userGroupList" 
            listKey = "userGroupId"
            listValue = "userGroup"
            name = "selectedUserGroupId"
            id="selectedUserGroupId"
            headerKey="-1" 
            headerValue="Select Group" 
            tooltip="Select Group" ></s:select>

when I submit the form and value is -1 , it is not display error message. 当我提交表单并且值为-1 ,它不显示错误消息。 I can see that addFieldError code run addressList get populated. 我可以看到填充了addFieldError代码的运行addressList

But is not displaying error message. 但是没有显示错误信息。

By default Struts2 uses xhtml theme. 默认情况下,Struts2使用xhtml主题。 This theme is designed to display field errors on input elements bound to the action. 该主题旨在在绑定​​到操作的输入元素上显示字段错误。 If errors aren't displayed then make sure you have used xhtml theme, or you can add explicitely theme attribute to each select tag or to the form tag. 如果未显示错误,请确保您使用了xhtml主题,或者可以将theme属性显式添加到每个select标记或form标记。 You can also configure a theme to be used by default using struts constant. 您还可以使用struts常量将主题配置为默认使用。

<s:select   label="Search engine:" 
            headerKey="-1" 
            headerValue="Select Search Engines"
            list="searchEngine" 
            name="yourSearchEngine" 
            id="yourSearchEngine"
            value="defaultSearchEngine" 
            tooltip="Select Search Engines" 
            theme="xhtml" />

<s:select   label = "User Group:" 
            list = "userGroupList" 
            listKey = "userGroupId"
            listValue = "userGroup"
            name = "selectedUserGroupId"
            id="selectedUserGroupId"
            headerKey="-1" 
            headerValue="Select Group" 
            tooltip="Select Group" 
            theme="xhtml" />

See where you can specify a theme in struts tags. 查看可以在struts标记中指定theme Specifying The Theme Struts 2 Should Use . 指定主题Struts 2应该使用

You can specify the theme on a per Struts 2 tag basis or you can use one of the following methods to specify what theme Struts 2 should use: 您可以基于每个Struts 2标签指定主题,也可以使用以下方法之一指定Struts 2应该使用的主题:

  • The theme attribute on the specific tag 特定标签上的theme属性
  • The theme attribute on a tag's surrounding form tag 标签周围表单标签上的theme属性
  • The page-scoped attribute named "theme" 页面范围的属性名为"theme"
  • The request-scoped attribute named "theme" 请求范围的属性名为"theme"
  • The session-scoped attribute named "theme" 会话范围的属性名为"theme"
  • The application-scoped attribute named "theme" 应用程序范围的属性名为"theme"
  • The struts.ui.theme property in struts.properties (defaults to xhtml ) struts.propertiesstruts.ui.theme属性(默认为xhtml

To make this effective on the page make sure the INPUT result configured to the action with defaultStack of interceptors. 为了使此效果在页面上有效,请确保使用defaultStack拦截器将INPUT结果配置为该操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM