Is it possible to have dynamic values in tiles.xml in Struts2 - struts2

Is it possible to pass dynamic values in tiles.xml as we do in struts.xml in Struts2? I have used ${parameter} to get dynamic values in config file but it doesnt seem to work. Any ideas?

You can pass wildcards to tiles from your struts actions, I've used this to do similar things for dynamic projects where each client might have a different CSS file for instance.
In your struts action you would have a tiles result type and you can pass the value such as:
<action name="{eventURL}/update" class="org.groundworkgroup.struts.actions.admin.UpdateEventSettings">
<result name="login" type="tiles">/login.tiles</result>
<result name="input" type="tiles">/admin.${#session.bean.pageID}.${#session.bean.fileID}.tiles</result>
<result name="success" type="tiles">/admin.${#session.bean.pageID}.${#session.bean.fileID}.tiles</result>
</action>
And then in your tiles.xml you would "plug in" the wildcards:
<definition name="/admin.*.*.tiles" extends="adminLayout">
<put-attribute name="title" value="Welcome" />
<put-attribute name="jsfile" value="{1}/js/{2}.js" />
<put-attribute name="cssfile" value="{1}/css/{2}.css" />
<put-attribute name="body" value="/WEB-INF/content/sites/admin/main.jsp" />
<put-attribute name="menu" value="/WEB-INF/content/sites/admin/menu.jsp" />
</definition>
In this particular example the struts action pageID is the project directory where the files are located and in the tiles.xml it is placed as wildcard {1}. The fileID is the filename associated with this particular action or user represented in the tiles.xml by {2}. You can use this set up to pass dynamic values to your tiles in order to control for example page states or JSP's to render or like in this example, custom css and js files.

Related

Get request from two different name space and to call same action

I have below struts configuration file with me,I need add another "namespace"(/mservice) to same action,In that config file I need to remove <param name="excludeNullProperties">false</param>
<struts>
<package name="com.web.mobile" namespace="/service" extends="mobile-default">
<action name="ticketSupport!*" class="com.web.mobile.TicketSupport"
method="{1}">
<result type="json">
<!-- This parameter should remove to the "/mservice" namesapce -->
<param name="excludeNullProperties">false</param>
</result>
<result name="input" type="json"/>
</action>
</package>
</struts>
I have two mobile apps, need to call same action with different parameters,I am thinking to maintain 2 struts configuration files, How can I modify existing config file to work with both apps?

How to prevent the wildcard namespace in struts?

I am facing a problem that I couldn't find a key word for googleing the following situation:
<package name="Main" namespace="/" extends="struts-default">
<action name="administrator" class="com.xyz.AdminAction">
<result name="success">/WEB-INF/jsp/admin.jsp</result>
</action>
</package>
The above url should be http://xyz.com/administrator and it works fine. However, if I change the url to http://xyz.com/asdasd/asdasdasd/administrator and it still works, but I can't accept this! So any setting to tell struts only http://xyz.com/administrator is accepted? Thanks!
Set the alwaysSelectFullNamespace property to true.
From struts-default.properties:
### Whether to always select the namespace to be everything before the last slash or not
struts.mapper.alwaysSelectFullNamespace=true
XML configuration is preferred, so in your struts.xml:
<struts>
<constant name="struts.mapper.alwaysSelectFullNamespace" value="true" />
...

url links in struts2 tiles gets namespace added on second time after click. How do I stop this?

I am using struts 2, tiles, and namespaces and am having a problem that after I click a link the menu appends the namespace a second time. I am using the struts tag url with an action and a namespace. When the page is first displayed, the links in the menu are correct land have the namespace before the action. After I click on one of the links, the page redisplays and although the source looks okay, when I hover over it firefox shows the url with the namespace in there twice. I.E.
localhost/ar/customermaint becomes localhost/ar/ar/customermaint. If I have clicked on a link that has a different namespace such as "ss" first then it becomes localhost/ss/ar/customermaint.
any idea what I'm doing wrong? Or is it a problem with tiles and namespaces.
I had faced this issue before. Each time when the action is called, the namespace gets appended in the URL and it looked ugly
Example: http://localhost:8080/RmAirlines/user/user/user/user/user/login for calling the action login from a namespace user each time
Setting the namespace to "/" isn't an ideal solution either.
Solution: Suppose the namespace of the package is "/user" and there's an action named "login" inside it (in your struts.xml). Then where ever you call the action, refer it as action="/user/login" (or action="/user/login.action") - i.e. putting a "/" before the namespace will save your day!
Example:
struts.xml
<package name="userManagement" namespace="/user" extends="struts-default">
<action name="login" class="com.rm.airlines.action.Login">
<result name="success">/user.jsp</result>
<result name="login">/login.jsp</result>
</action>
</package>
login.jsp
<s:form action="/user/login">
<s:textfield label="Email" key="email" />
<s:password label="Password" key="password" />
<s:submit />
</s:form>
To avoid this, always follow a simple principle.
For all namespace declaration, use a slash "/" followed by the namespace name.
In this way, it will not append the earlier namespace in new url.
What struts assume, if you don't specify "/" in new namespace, it tries to find it in currently used namespace. If you specify "/" before namespace name, it takes it as a root namespace.
Example:-
<package name="default" namespace="/nsp" extends="struts-default">
<action name="validate" class="login.LoginAction">
<result name="success">/home.jsp</result>
<result name="login">/login.jsp</result>
</action>
</package>
<package name="cricket" namespace="/cricket" extends="struts-default">
<action name="findcaptain" class="cricket.CaptainAction">
<result>/showCaptain.jsp</result>
</action>
</package>
Look in each of my package declaration, I am using namespace name prefixed by a slash. i.e. /nsp and /cricket). And that's how we could avoid this namespace appending issue.

Does anyone have any experience that interceptor(i18n) will cause no value during postback?

Assume I have this struts2 form
<s:form action="login" method="post">
<s:textfield key="login_name" name="login_name"/>
<s:submit></s:submit>
</s:form>
And also with this struts.xml setting
<constant name="struts.custom.i18n.resources" value="messageResource" />
<constant name="struts.devMode" value="true" />
<package name="login" namespace="/" extends="struts-default">
<action name="login" class="actions.index.index">
<interceptor-ref name="i18n"/>
<result name="LOGIN_SUCCESS">/Main.jsp</result>
<result name="LOGIN">/Login.jsp</result>
</action>
</package>
If I have added <interceptor-ref name="i18n"/> to the setting, login_name will have no value after form submitted; otherwise I can retrieve the value successfully.
If you want me to provide further detail, please just let me know. Thanks in advance!
You are adding only one interceptor for your action by doing so all other interceptors are not included. The default stack already includes i18n interceptor so no point to add it by yourself.

accept attribute is not working in <s:file> tag of struts2

I am trying to validate the content types of uploading files using accept attribute but it seem to be not working.
Here is my code.
<s:file theme="simple" name="fileUpload" accept="image/jpeg"/>
i also tried
<s:file theme="simple" name="fileUpload" accept="image/*"/>
Both are not working what could be the problem?
The HTML accept attribute is not supported in IE and Safari. You can define allowed mime types in struts.xml for you file upload action like that:
<action name="..." class="...">
<interceptor-ref name="defaultStack">
<param name="fileUpload.allowedTypes">image/jpeg</param>
</interceptor-ref>
<result>...</result>
</action>
See others parameters you can configure in fileUpload interceptor.

Resources