Struts2 - Setting a property from a static list String property using OGNL not working

青春壹個敷衍的年華 提交于 2019-12-20 02:39:23

问题


I'm trying to set a hidden input value using the struts2 framework. The input value is an unchanging value, so I'd like to set it via a static reference rather than worrying about setting it possibly in multiple places in the Controller.

Here is the list definition:

     package com.packagename.models;
     public class UsernameModel implements Serializable, Comparable<UsernameModel> {

          ...
          /**
          * <P>A static container of the predefined username types.
          */
          public static final class UsernameTypes {
           public static final String ALIAS = "Alias";
           public static final String ASSIGN_NUM = "Assignment Number";
          }
          ...
      }

I realize that this may be out of the scope of the question, but when I try to reference the property inside the hidden input like so it throws an exception:

<s:hidden name="username_type" value="<s:property value="@com.packagename.models.UsernameModel.UsernameTypes@ALIAS" />"></s:hidden>

org.apache.jasper.JasperException: /WEB-INF/content/user/profile.jsp (line: 185, column: 64) Unterminated <s:hidden tag

If I remove the property tag from the s:hidden input the exception goes away but no text appears where the "Alias" string should be.

-- Thanks in advance


回答1:


You cannot nest tags like that. And to reference inner class you need to use $ sign.

<s:hidden name="username_type"
          value="%{@com.packagename.models.UsernameModel$UsernameTypes@ALIAS}" />

IMO it is better to use that kind of static variables directly in class rather than send them from JSP.



来源:https://stackoverflow.com/questions/17733505/struts2-setting-a-property-from-a-static-list-string-property-using-ognl-not-w

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!