Why can't access attribute in request? - struts2

I want to access some attributes in request, for example, the base attribute in request,
the following are part of values in request
request ...base=/ecs, stack=com.opensymphony.xwork2.ognl.OgnlValueStack#11c4b31}, __cleanup_recursion_counter=1, .freemarker.RequestParameters=freemarker.ext.servlet.HttpRequestParametersHashModel#1c00cb4 ...
I use <s:debug /><s:property value="%{#request.base}" />
to access base attribute in request, but nothing shown in my jsp. So why?

Not sure why do you need this but only base inside request is inside .freemarker.TemplateModel which is ScopesHashModel. So you need use method get to get things from there.
<s:property value="#request['.freemarker.TemplateModel'].get('base')" />
Try this:
<s:property value="#request['javax.servlet.include.context_path']"/>
Update
If you just need context path then use <s:url> tag for that.
<s:url value="/"/>

If you want to access request parameters following will work
<s:property value="#parameters['base']"/>
If you want to access attributes of the request then following should work
<s:property value="#request.base"/>
or
<s:property value="#request['base']" />

Related

More than one parameters in URL Struts2

I wanted to pass more than one parameter for an action. but when i build the URL using Struts2, it builds with only one parameter. May i know what is went wrong in the below code?
<s:url action="loadValidLevelValueDropDown" id="levelvalueURL" escapeAmp="false">
<s:param name="hierarchyId" value="searchAttribute.hierarchyId.id"></s:param>
<s:param name="valuebycoulmn" value="refcolumnName%{#level.count}"></s:param>
</s:url>
result is,
/appname/loadValidLevelValueDropDown.action?hierarchyId=1
The value attribute of <s:param> tag takes OGNL expression as a value. Your refcolumnName%{#level.count} value is not a valid expression so parameter is not being appended to url.
If your refcolumnName is a collection than you can access it like that:
<s:param name="valuebycoulmn" value="refcolumnName[#level.count]" />
if it supposed to be a string than you need to append it like string:
<s:param name="valuebycoulmn" value="'refcolumnName' + #level.count" />

Struts2 tag <s:text> I need to lower case the string returned

So I have a tag like this
<s:text name="furniture_logs" />
Where s is defined:
<%# taglib prefix="s" uri="/WEB-INF/tld/struts-tags.tld"%>
and "furniture_logs" is a key from the database (returnes a string) . I want to show the lower case of this string, what can I do ? I am new to Struts2 and i didnt find any reference for that.
The <s:text> tag is for rendering a I18n text messages. If you really want to use it then you could try calling getText method and toLowerCase in <s:property> tag like that:
<s:property value="getText('furniture_logs').toLowerCase()"/>
Struts2 uses OGNL that means you can call Java methods inside Struts2 tags. Read about OGNL http://commons.apache.org/ognl/.
Update
If using <s:text> tag is a must to you, then use var attribute of it.
<s:text var="flogs" name="furniture_logs"/>
<s:property value="#flogs.toLowerCase()"/>

access struts2 include param value in another jsp page in struts if tag

I am passing param value in include tag in jsp page like below
<s:include value="../../commonjspf/status.jspf">
<s:param name="mystatus" value="%{status}">
</s:param>
</s:include>
where status variable come from action class .
I want to access that mystatus param in status.jspf page in struts if tag to compare with my default values.
<s:if test ="">
</s:if>
or
<s:set name="" value =""/>
any of above tags.
how can i access ?
please suggest me .
Thanks.
Use the ${param.ParamName} notation to access them, as mentioned in the reference below:
http://struts.apache.org/2.0.14/docs/include.html
A sample code:
Page 1:
<s:include value="testPage.jsp">
<s:param name="mystatus">TestThis</s:param>
</s:include>
Page 2:
<%# taglib prefix="s" uri="/struts-tags"%>
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<c:set var="mystatus" value="${param.mystatus}" scope="page"/>
<s:if test='%{#attr.mystatus == "TestThis"}'>
This is what we want
</s:if>
<s:else>
This is not what we want
</s:else>
Any additional params supplied to the included page are not accessible within the rendered page through the tag since no valuestack will be created.
refer to the Struts2 documentation for details.
Struts2 Include tag
You can, however, access them in a servlet via the HttpServletRequest object or from a JSP page via a scriptlet.something like
${param.ParamName}
I would just like to throw this in as an alternative to using the struts include tag.
You can instead use the jsp:include tag and use the struts s:push tag to push parameters onto the stack and make them available in the included page, it adds an couple of extra lines into the jsp but is much more flexible as you can pass objects rather than just strings into the included JSP.
The nature of the push tag means once your done the parameters are poped from the stack again.
Primary JSP
<s:push value="#{'someStringParam':'some string', 'someObjectParam': someObject}">
<jsp:include page="../includes/user-tabs.jsp" />
</s:push>
Included JSP
<s:property value="someStringParam" />
<s:if test="someObjectParam.someValue > 10">
Result!
</s:else>
Building from James' answer, this helped me on my Page 2:
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%# page isELIgnored="false" %>
<c:set var="mystatus" value="${param.mystatus}" scope="page"/>
<c:if test="${empty mystatus}">
<c:set var="mystatus" value="default status here" />
</c:if>
If you are passing a value from one page to another (as below) be careful about the syntax. You need to put single quotes around the name text you are passing, otherwise it's considered the name of a variable.
<s:include value="other.jsp">
<s:param name="thevar" value="'text i want to see'" />
</s:include>
Then in the "other.jsp" page you will see the "text i want to see" if you do as follows:
${param.thevar}
If, instead you do NOT place the single quotes in the param value attribute, you see nothing in the other.jsp page.
I only mention it as I've seen this a lot of times.

struts 2 text tag with dynamic expression , how?

I need to supply the key dynamically from the action to the text tag
<s:text name="<%=talkToUsInfo.messageKey%>" />
but the name attribute on text tag is declared as false ( not sure why ? )
How do i get something like this working without changing the tld in the jar file ?
Take a look at OGNL
It might look like this
<s:text name="%{talkToUsInfo.messageKey}" />
Struts documentation says:
Instead, you should use the getText() method that you inherit when your Action extends XWork's ActionSupport:
<s:textfield name="lastName" label="getText('person.lastName')" />
So I used e.g.
<s:property value="getText('status' + #someObject.currentStatus)" />
instead of "s:text" and it worked.
I prefer to use OGNL and s:text and do not make a call to getText manually.
You usually define a fixed prefix while add the dynamic part to it, so some thing like this could be used:
<s:text name="%{'student.types.'+currencyType}" />
The only time which I used getText inside the s:property was something like this (which needs some if/else if you wanted to use s:text ):
<s:property value="shouldCancelIt.equals(\"0\") ?
getText('label.yes'):getText('label.no')" />

Creating navigation links in Struts 2

Is there some way to replicate rails' "link-to-unless-current"?
Ie. if i have a list of links in my navigation, and I want to mark the current page with a different css style (or even have it be plain text, not a link)?
Now I just made a method in my action-class (getPage()) which returns a name that I assign for each action, and test for that when building my navigation... Works but not pretty, and I have to manually set the page name (couldn't struts somehow automaticalyy get it from the context).
Using Struts 2, Tiles, JSP + Struts taglibs.
To get the current url you you can use the Url tag without any params, actions, etc:
<s:url var="currenturl" includeParams="get" escapeAmp="false"/>
Then assuming you construct your navigation Url something like:
<s:url var="url" action="someAction" escapeAmp="false">
<s:param name="id" value="id"/>
</s:url>
Then you can then use a normal if tag to test whether the current url matches on that you've constructed.
<s:if test="#url eq #currenturl">
</s:if>
For example, to change the class, you can do a conditional inline to your anchor definition:
class="current"</s:if>>XXX
Use the includeParams="get" if the parameters in your URL are meaningful for navigation otherwise exclude that and use an if like:
<s:if test="#url.startsWith(#currenturl)">

Resources