How to remove the action suffix extension in Struts 2

In Struts 2, all action class has a default suffix .action extension. For example,


<struts>
  <package name="default" namespace="/" extends="struts-default">
	<action name="SayStruts2">
		<result>pages/printStruts2.jsp</result>
	</action>
  </package>
</struts>

To access the “SayStruts2” action class, use the following URL :


Action URL : http://localhost:8080/Struts2Example/SayStruts2.action

Configure the action extension

Struts 2 is allow to configure the action extension easily, to change it, just declare a constant “struts.action.extension” value :

1. html extension

Change the action class to .html extension.


<struts>
	 
  <constant name="struts.action.extension" value="html"/> 
	
  <package name="default" namespace="/" extends="struts-default">
	<action name="SayStruts2">
		<result>pages/printStruts2.jsp</result>
	</action>
  </package>
	
</struts>

Now you can access the “SayStruts2” action class via


Action URL : http://localhost:8080/Struts2Example/SayStruts2.html

2. No extension

Change the action class to empty extension.


<struts>
	 
  <constant name="struts.action.extension" value=""/> 
	
  <package name="default" namespace="/" extends="struts-default">
	<action name="SayStruts2">
		<result>pages/printStruts2.jsp</result>
	</action>
  </package>
	
</struts>

Now you can access the “SayStruts2′ action class via


Action URL : http://localhost:8080/Struts2Example/SayStruts2

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
18 Comment threads
6 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
19 Comment authors
pkaJustin JamesPrakhar Mishrastruts2do Recent comment authors
newest oldest most voted
Durgadas
Guest
Durgadas

Having extension value = “” will cause issues while serving files like js, css etc
Use extension as “,”

Using value=”,” will tell Struts that the action extension is empty. Also it will not attempt to match any requests that do have an extension such as *.css, *.js etc.

struts2do
Guest
struts2do

Really good comment!!! thanks!!!! save me a lot of time!!!!!!!

pk
Guest
pk

sir how we change the action extension when we use annotation

Justin James
Guest
Justin James

This is what i exactly looking for.. Thank you..

Prakhar Mishra
Guest
Prakhar Mishra

Have anybody tried struts2-archetype-starter (maven archetype)?

vivek
Guest
vivek

when i use , my application fails to load css and javascript files.

How to overcome it.

prashanth
Guest
prashanth

do we have similar to this in xwork,webwork?

Piyush
Guest
Piyush

If you not need extension Use

if you use
your index.jsp,index.html, and all .js, .css, all images give an errors

Piyush
Guest
Piyush

i can’ see my java source code

Piyush
Guest
Piyush

For No extension Use

if you use your index.html,index.jsp and all .js, .css, all images give an error

trackback
Working with Struts 2 actions|SimpleCodeStuffs

[…] : To change the extension to any value that suits your need, view – Struts2 Coustom extension example. […]

Adrian
Guest
Adrian

Are you sure? I completed your tutorial called Struts 2 Hello World example and there you don’t have extension set in struts.xml and actions are accessible without “.action” suffix. I also modified it to use jQuery post and suffix wasn’t also needed. Regards

vik
Guest
vik

Me too tried but its not working… it shows some errors when using this line in struts.xml

Durgadas
Guest
Durgadas

Yup, its working fine even without specifying .action in the url.

ayoub
Guest
ayoub

how to change the extension using the annotations, thank you

hamen
Guest
hamen

Is it possible to remove .action just for one specific package?

a
Guest
a

did you get to know the answer

Jorge Sereno
Guest
Jorge Sereno

Hello. I have the exemple above on a sample application where me index.hml, wish is my welcome file defined in web.xml, as the code:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="refresh"
	content="0;URL='startPage'">
</head>
</html>

where startPage is an action on struts.
Whith the “struts.action.extension” set to “html” it does not work, whithout works fine. I can’t find the problem.
Anyone can help me?

Thankx

sunil
Guest
sunil

When i remove the extension then css files are served as text/html MIME type

Alex
Guest
Alex

How to add the action suffix extension in Struts 2 with tiles ?

I tested in struts 2 but if i am using tiles that i can’t

lwpro2
Guest
lwpro2

the web.xml, filter url-pattern needs to be updated accordingly. Thanks.

yejie
Guest
yejie

I’m facing the same problem. Any Ideas?

Dan
Guest
Dan

I’m using struts 2.1.6 and the html extension works but the empty extension doesn’t it gives an error that the namespace / can’t find the corresponding action, any idea why?

trackback
Working with Struts 2 actions

[…] The default .action is configurable, just change the “struts.action.extension” value to suit your […]