How to compare a single character from url request parameter in Struts 2

安稳与你 提交于 2019-12-02 10:14:29

问题


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>

I also tried

 <s:set var="theUrl" value='%{#parameters.theconditionTypeInUrl}' />

  <s:if test='%{#theUrl == "Y"}'>

 </s:if>
 <s:else>

 </s:else>

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>

回答1:


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">

Then parameters will be processed and put to the action attributes, where you could access them via OGNL by implementing getter and setter.

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;
}

More detail explanation you could find here.

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.



来源:https://stackoverflow.com/questions/17001344/how-to-compare-a-single-character-from-url-request-parameter-in-struts-2

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