I am new to Struts 2, but so far, I have made decent progress using the API.
However, I am stuck at something which I need to get out from. I am using Struts 2 with Spring integration. I am writing my Action
classes with annotations as like many of you, I love annotations.
My requirement is that URLs will be in the following nature:
http://<DOMAIN>/program/program1.jspx
http://<DOMAIN>/program/program2.jspx
http://<DOMAIN>/program/program3.jspx
As you can see there is a certain pattern in the URL with program1
, program2
, and program3
varying, and the rest are all static. I have very easily handled similar situation with Spring MVC (I don't have the option of using Spring MVC for the current project) like "/program/{program_name}.jspx"
in my other project.
But when I use the same in Struts 2 I get the error.
My Action
class is as follows:
@Result(name="program", location="program", type="tiles")
public class ProgramAction extends ActionSupport {
@Action("program/{programName}")
public String getProgramPage() {
// few more lines of code
return "program";
}
}
The error is:
2013-02-28 15:46:35.591 WARN [http-bio-8080-exec-3] CommonsLogger.java:60
Could not find action or result
com.opensymphony.xwork2.config.ConfigurationException: There is no Action mapped for namespace / and action name program1.
at com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:189) ~[xwork-core-2.2.1.jar:2.2.1]
at org.apache.struts2.impl.StrutsActionProxy.prepare(StrutsActionProxy.java:61) ~[struts2-core-2.2.1.jar:2.2.1]
at org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:39) ~[struts2-core-2.2.1.jar:2.2.1]
at com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:58) ~[xwork-core-2.2.1.jar:2.2.1]
at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:475) ~[struts2-core-2.2.1.jar:2.2.1]
....
....
My struts.xml
file is as follows:
<struts>
<constant name="struts.convention.default.parent.package" value="default"/>
<constant name="struts.action.extension" value="jspx" />
<constant name="struts.mapper.alwaysSelectFullNamespace" value="false" />
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<package name="default" extends="struts-default, json-default, rest-default" namespace="/">
<result-types>
<result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult"/>
<result-type name="json" class="org.apache.struts2.json.JSONResult"/>
</result-types>
</package>
</struts>
You have a slash in your action name. struts by default does not like this to correct that add:
<constant name="struts.enable.SlashesInActionNames" value="true"/>