如何在Struts 2中比较URL请求参数中的单个字符 [英] How to compare a single character from url request parameter in Struts 2
问题描述
我正在读取具有单个字符的url参数.它将是Y
或N
.我必须写一个条件来检查它是Y
还是N
并相应地做一些事情.这是我写的,但似乎没有用(总是转到其他地方).网址是
I am reading the url parameter which has a single character. It will either be Y
or N
. I have to write a condition to check if it Y
or N
and do something accordingly. This is what I wrote but does not seem to work (always goes to else). The url is
http://wwww.helloworld.com/showInfo?thecountry=us& theconditionTypeInUrl = Y
<s:set var="theUrl" value="%{#parameters.theconditionTypeInUrl}" />
<s:if test="%{#theUrl == 'Y'}">
</s:if>
<s:else>
</s:else>
我也尝试过
<s:set var="theUrl" value='%{#parameters.theconditionTypeInUrl}' />
<s:if test='%{#theUrl == "Y"}'>
</s:if>
<s:else>
</s:else>
struts.xml包含
the struts.xml contains
<action name="showInfo" class="showInfoAction">
<result name="success" type="tiles">dis.Info</result>
<result name="no_results" type="tiles">dis.noInfo</result>
</action>
推荐答案
通常,请求参数由params
拦截器处理.动作配置应在堆栈中包含此拦截器.如果未明确引用此拦截器,则使用已经包含params
拦截器的defaultStack
.您需要的是将操作扩展为"struts-default"
的程序包.
Usually request parameters are processed by the params
interceptor. The action configuration should have this interceptor included in the stack. If you not referencing this interceptor explicitly the defaultStack
is used that already contains the params
interceptor. What you need is the package for the action to be extended the "struts-default"
.
<package name="default" namespace="/" extends="struts-default">
然后将处理参数并将其放入action属性,您可以在其中通过实现getter和setter通过OGNL访问它们.
Then parameters will be processed and put to the action attributes, where you could access them via OGNL by implementing getter and setter.
另一种方法是实现 ParameterAware
通过您的操作.注入参数后,可以从valueStack
顶部的操作中使用该参数,并且可以通过
Another way is to implement ParameterAware
by your action. After injecting parameters it could be available from the action that is on top of the valueStack
and you could get parameters via
<s:if test="parameters['theconditionTypeInUrl'] == 'Y'">
在为参数属性添加吸气剂之后
after adding getter for parameters attribute
Map getParameters(){
return this.parameters;
}
您可以找到使用#parameters
键通过OGNL尝试从操作上下文映射访问请求参数的方式.在if
标记中访问参数可能会遇到麻烦,请参见此处.
The way you are trying access request parameters from the action context map via OGNL using #parameters
key. There could be troubles accessing parameters in the if
tag, see here.
这篇关于如何在Struts 2中比较URL请求参数中的单个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!