OGNL代表对象图导航语言。它是用于获取和设置Java对象属性的表达式语言。
我的 JSP 页面中有一个下拉菜单,它是用 标签实现的,如下所示 我的 JSP 页面中有一个下拉菜单,它是使用 <s:select> 标签实现的,如下所示 <s:select name="priorToApplyingInfo.userProfile.phoneNumbers[0].type" listKey="key" listValue="value" list="phoneTypes" headerKey="0" headerValue=""/> 现在下拉菜单中的值来自列表 phonetypes,该列表在 HashMap 文件中作为 .java 实现。 phoneTypes = new LinkedHashMap<Integer, String>(); phoneTypes.put(new Integer(1), getText("HOME")); // Phone ContactBook category for the business phone phoneTypes.put(new Integer(DAOHelperFactory.OWNER_PROFILE_PHONE_CATEGORY), getText("WORK")); phoneTypes.put(new Integer(3), getText("MOBILE")); phoneTypes.put(new Integer(DAOHelperFactory.OWNER_PROFILE_FAX_CATEGORY), getText("FAX")); phoneTypes.put(new Integer(5), getText("OTHER")); preferredContact = new ArrayList<String>(); preferredContact.add(getText("HOME")); preferredContact.add(getText("WORK")); preferredContact.add(getText("MOBILE")); preferredContact.add(getText("FAX")); preferredContact.add(getText("EMAIL")); preferredContact.add(getText("OTHER")); bestContactTime = new ArrayList<String>(); bestContactTime.add(getText("AFTERNOON")); bestContactTime.add(getText("EVENING")); bestContactTime.add(getText("MORNING")); 诸如 home=home、work=work 等键位于 .properties 文件中 我正在努力国际化此页面,但我无法找到获取翻译的方法 下拉菜单中的值。 要更改 Struts2 应用程序中的区域设置,您需要将 requst_locale 参数包含到某些链接或表单中。 <s:url var="urlen" includeParams="all" value=""> <s:param name="request_locale">en</s:param> </s:url> <s:a href="%{#urlen}">English</s:a> 如果您想从操作类更改区域设置,请使用 ActionContext 进行设置并将其放入 HTTP 会话中。 ActionContext.getContext().setLocale(locale); session.put(I18nInterceptor.DEFAULT_SESSION_ATTRIBUTE, locale); 您也可以在 JSP 中的 getText 标签的 listValue 属性中调用 <s:select> 方法。 <s:select name="priorToApplyingInfo.userProfile.phoneNumbers[0].type" list="phoneTypes" headerKey="0" headerValue="" listKey="key" listValue="%{getText(value)}"/> 对我来说工作(就像 Aleksandr M 在上一段中写的那样): <s:select listValue="%{getText(value)}" listKey="key" list="phoneTypes></s:select> 只有我创造 phoneTypes = new HashMap<String, String>() phoneTypes.put("HOME", "HOME") phoneTypes.put("WORK", "WORK") etc.. 在这种情况下,键被省略(您可以在此处编写任何内容)并在页面上显示(翻译)值。 此解决方案不适用于列表,仅适用于地图。 在从资源中检索消息之前,您没有切换 Struts2 中的区域设置。 getText() 是本地化方法,如果它使用默认文本提供程序作为默认行为,那么它会搜索区域设置特定的键。您可以从操作上下文或直接从 ActionSupport 操作获取 Struts2 使用的当前语言环境(还没有看到您有一个操作并且它扩展了它)。 通常切换区域设置是通过 i18n 拦截器完成的,您可以在其中将参数放入请求中 request_locale。但是您可以通过更改操作上下文中的区域设置来切换它(确保您正在运行与当前相同的线程)。 ActionContext.getContext().setLocale(new Locale("es")); 您应该在执行任何 getText() 之前运行此代码以获取本地化消息。
我想知道,是否可以动态地将值设置为静态参数 动态价值在这里 我想知道,是否可以动态地将值设置为静态参数 <action name="TestApp_*" class="test.TestApp" method="{1}"> <param name="app_Id">Dynamic value here</param> <result name="input">WEB-INF/jsp/test/testView.jsp</result> </action> 我试过了 <action name="TestApp_*" class="test.TestApp" method="{1}"> <param name="app_Id">${app_Id}</param> <result name="input">WEB-INF/jsp/test/testView.jsp</result> </action> 不工作。它将 ${app_Id} 显示为常规文本。 请注意,它显示 ${app_Id} 作为常规文本到底是什么意思。Struts2 提供了这种灵活性,您可以动态设置 param 值。 您只需在操作类中设置这些值,即可将它们用作占位符。 public class MyAction extends ActionSupport { private int app_Id; public String execute() { // you execute logic this.app_Id= 123; return SUCCESS; } // getter and setter for app_Id } 您可以在结果配置中使用此app_Id <action name="TestApp_*" class="test.TestApp" method="{1}"> <param name="app_Id">${app_Id}</param> <result name="input">WEB-INF/jsp/test/testView.jsp</result> </action> 您可以获得更多详细信息配置结果中的参数 您可能已经(错误地)手动编写了 getter,而不是让 IDE 为您完成。 变量app_Id;必须有一个吸气剂getApp_Id(),而你可以有类似getapp_Id()或get_app_Id()之类的东西......但是如果你不发布你的操作代码,我们怎么知道它?! 此外,考虑更改变量(从现在开始)以尊重约定,即希望它们采用驼峰式命名,而不是下划线分隔...然后 appId 表示变量,getAppId() 表示 getter。 尝试这样做.. <result type="redirect"> <param name="location">WEB-INF/jsp/test/testView.jsp</param> <param name="inputName">app_Id</param> </result> 在jsp页面中你可以直接使用这个值就像 <s:properties value="app_Id"> 试试这个。 看看staticParams拦截器。 此拦截器使用操作配置中定义的静态参数填充操作。如果操作实现了Parameterizable,静态参数的映射也将直接传递给操作。静态参数将添加到请求参数映射中,除非 merge 设置为 false。 参数通常使用 <param> 中的 struts.xml 元素定义。 它展示了如何将此拦截器配置到您的操作配置中。 例如: <action name="someAction" class="com.examples.SomeAction"> <interceptor-ref name="defaultStack"> <param name="staticParams.parse">true</param> <param name="staticParams.overwrite">false</param> </interceptor-ref> <param name="num">${numValue}</param> <result name="success">good_result.ftl</result> </action> 这使得拦截器能够从操作配置中解析 OGNL 表达式的参数值。
使用 OGNL 在 Thymeleaf 中静态方法 Math#random 调用获取 OgnlException: getProperty(null, "lang") 的源为 null
我正在尝试 Thymeleaf 的非常基本的静态 Java 方法调用: 但总是遇到异常 OgnlException: source is null for 我正在尝试 Thymeleaf 进行非常基本的静态 Java 方法调用: <div th:text="${T(java.lang.Math).random()}"/> 但总是遇到例外OgnlException: source is null for getProperty(null, "lang")。 完整错误消息: 2023-08-11 16:17:16,221 ERROR [etp1561005241-55] o.t.TemplateEngine [THYMELEAF][etp1561005241-55] Exception processing template "test.html": Exception evaluating OGNL expression: "T(java.lang.Math).random( )" (template: "test.html" - line 89, col 14) org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating OGNL expression: "T(java.lang.Math).random()" (template: "diag-ui.html" - line 89, col 14) at org.thymeleaf.standard.expression.OGNLVariableExpressionEvaluator.evaluate(OGNLVariableExpressionEvaluator.java:199) at org.thymeleaf.standard.expression.OGNLVariableExpressionEvaluator.evaluate(OGNLVariableExpressionEvaluator.java:104) at org.thymeleaf.standard.expression.VariableExpression.executeVariableExpression(VariableExpression.java:166) at org.thymeleaf.standard.expression.SimpleExpression.executeSimple(SimpleExpression.java:66) at org.thymeleaf.standard.expression.Expression.execute(Expression.java:109) at org.thymeleaf.standard.expression.Expression.execute(Expression.java:138) at org.thymeleaf.standard.processor.AbstractStandardExpressionAttributeTagProcessor.doProcess(AbstractStandardExpressionAttributeTagProcessor.java:144) at org.thymeleaf.processor.element.AbstractAttributeTagProcessor.doProcess(AbstractAttributeTagProcessor.java:74) at org.thymeleaf.processor.element.AbstractElementTagProcessor.process(AbstractElementTagProcessor.java:95) at org.thymeleaf.util.ProcessorConfigurationUtils$ElementTagProcessorWrapper.process(ProcessorConfigurationUtils.java:633) at org.thymeleaf.engine.ProcessorTemplateHandler.handleStandaloneElement(ProcessorTemplateHandler.java:918) at org.thymeleaf.engine.StandaloneElementTag.beHandled(StandaloneElementTag.java:228) at org.thymeleaf.engine.TemplateModel.process(TemplateModel.java:136) at org.thymeleaf.engine.TemplateManager.parseAndProcess(TemplateManager.java:661) at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1103) at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1064) at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1053) at hobbit.mordor.diagui.DiaguiEndpoint.handleSearch(DiaguiEndpoint.java:106) at hobbit.mordor.diagui.DiaguiEndpoint.handle(DiaguiEndpoint.java:149) at hobbit.mordor.server.JettyRequestHandler.dispatchRequest(JettyRequestHandler.java:273) at hobbit.mordor.server.JettyRequestHandler.handle(JettyRequestHandler.java:313) at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:127) at org.eclipse.jetty.server.Server.handle(Server.java:516) at org.eclipse.jetty.server.HttpChannel.lambda$handle$1(HttpChannel.java:487) at org.eclipse.jetty.server.HttpChannel.dispatch(HttpChannel.java:732) at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:479) at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:277) at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:311) at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:105) at org.eclipse.jetty.io.ChannelEndPoint$1.run(ChannelEndPoint.java:104) at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136) at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635) at java.base/java.lang.Thread.run(Thread.java:833) Caused by: ognl.OgnlException: source is null for getProperty(null, "lang") 感谢您的回答。 ${@java.lang.Math@random()}为我工作
如何使用struts2 s:set variable inside s:select tag for list values?
我想在 s:select 中使用 s:set 变量: 纽约 我想在里面使用一个s:set变量s:select: <s:set var="cityNY">NewYork</s:set> <s:select name="cities" list="#{'%{#cityNY}':'%{#cityNY}'}" required="true" /> 上面只是在我的页面中打印相同的值 - %{#cityNY} 想要在 s:set 标签的选项中显示 if s:select 变量的值 您需要将上下文变量直接放入 OGNL 表达式而无需%{}。它将实例化一个地图。 <s:select name="cities" list="#{#cityNY:#cityNY}" required="true" /> list 属性值可以包含 OGNL 表达式。它默认用于解析 OGNL 的值而无需显式%{}。可以在 OGNL 表达式中直接引用子表达式。 #{exp1:exp2} 是一个 OGNL 表达式来实例化一个 Map。它里面有子表达式。每个子表达式应该返回一个不是集合的值。因为他们曾经为地图创建一个键/值对。 如果您使用相同的子表达式,那么最好使用以下代码实例化一个List <s:select name="cities" list="{#cityNY}" required="true" /> 它将生成一个 HTML <select> 标签,其中有一个 <option> 具有相同的值和文本。 如果您需要更多选项,那么您应该使用逗号向 OGNL 表达式添加值。 您可以在我对 OGNL/Struts2 JSP assigning bean to an object. 的回答中找到参考文档
嗨,这似乎是一个奇怪的问题,因为当我通过s:text标签打印出变量时,它可以工作,但当我把它放在id标签中时,它却不能工作。在div中的id可以按字面意思打印出%{param},但是......。
当索引是变量时,我在访问列表的特定元素时遇到一些麻烦。当索引只是一个数字时,我完全不会显示所需的内容。
在Ognl 3.2.10中找不到DefaultMemberAccess
我在最新的struts 2.5.20和OGNL 3.2.10上有一个应用程序。应用程序启动时,出现异常java.lang.ClassNotFoundException:org.apache.catalina上的ognl.DefaultMemberAccess。...
test expression evaluation for boolean value doesn't work as expected
我想使用Struts2标签检查变量bool_val的值 但它不起作用。实际价值:
对于OGNL代码的PingFederate有什么好的参考,例如:#AssertionType.getConditions()。addNewAudienceRestriction()[关闭]
我在网上看到了各种帖子,其中有关于PingFederate的OGNL代码,例如:#AssertionType.getConditions()。addNewAudienceRestriction()。addAudience(“whatever:eh”)#AssertionType.getConditions()....
RuntimeException:必须提供MemberAccess实现
我正在使用一个访问Customer对象的公共字段的模板,如下所示: &...
我正在使用struts验证器进行jsp验证,我想知道如何根据jsp的另一个字段的值定义字段验证。我有一个像这样的元素列表:...
引起:org.apache.ibatis.ognl.NoSuchPropertyException:
我的项目使用mybatis插件,偶尔会偶尔发生以下错误!我使用的mybatis插件tk.mybatis.mapper版本3.3.0调用Mapper接口tk.mybatis.mapper ....
我有一个父文件,我的JSP静态包含在其中。在包含的文件中,我想使用Struts2标记访问父JSP的变量。请告诉我 ...
我想知道如果我们填充动作类属性,我们是否可以直接在结果JSP中访问它们而不将它们分配给动作类中的表单?
我使用以下语法以适当的数字格式显示值,例如1,250.00。 但是,它不起作用。 ......
我有名为allAlbums和allPhotos的对象列表。现在我想显示每张专辑中的所有照片,所以我使用了以下方法。我的代码是
我有一个带有10,000个复选框的表单,每个复选框都有一个数字值,如8757857,8755854。当我提交表单时,我可以看到整个数据正确地传递到...的后端