Struts 2 Tutorial

Struts 2 tutorials

Apache Struts 2, another popular Java Model-View-Contraller (MVC) framework, combine of both successful WebWork and Struts 1.x web frameworks.

Apache Struts 2, is totally different with Struts 1, it’s core features are all implemented with interceptors, “value stack” concept, OGNL expression and Struts 2 tags to work around the application data, and many annotations and conventions to make this framework more simpler to use. In this tutorials, it provides many step by step examples and explanations on using Struts 2 MVC framework.

Happy learning Struts 2. :)

Struts 2 Quick Start

Struts 2 quick start example.

Struts 2 Configurations

Anything about Struts 2 configuration file.

Struts 2 Action & Form

Struts 2 Action and form data management.

Struts 2 Interceptors

Abything about Struts 2 interceptors.

Struts 2 UI Tags

Struts 2 UI tags, to render the HTML form and non-form componnets.

Struts 2 Control Tags

Struts 2 Control tags or Logic tags, help to do conditional processing, iterate, manipulate and display the data.

  • iterator tag example
    Struts 2 Iterator tag is used to iterate over a value, which can be any of java.util.Collection or java.util.Iterator.
  • if, elseIf, else tag example
    Struts 2 If, ElseIf and Else tags are used to perform basic condition checking.
  • append tag example
    Struts 2 append tag is used to combine few Iterators (created by List or Map) into a single Iterator.
  • generator tag example
    Struts 2 generator tag is used to generate an iterator based on the “val” attribute provided in the page.
  • merge tag example
    Struts 2 merge tag is used to merge few Iterators (created by List or Map) into a single Iterator.
  • sort tag example
    Struts 2 sort tag is used to sort a List using a java.util.Comparator.
  • subset tag example
    Struts 2 subset tag is used to output a subset or portion of an iterator elements.

Struts 2 Data Tags

Struts 2 Data tags, help to get the data from the ValueStack, or place the data into the ValusStack.

  • a tag example
    Struts 2 a tag is used to render a HTML “<a>” tag.
  • action tag example
    Struts 2 action tag is used to call action class directly from a JSP page.
  • bean tag example
    Struts 2 bean tag is used to instantiate a class in the JSP page.
  • date tag example
    Struts 2 date tag is used to format Date object in JSP page.
  • debug tag example
    Struts 2 debug tag is a very useful debugging tag to output the content of the “Value Stack” and also the “Stack Context” details in JSP page.
  • include tag example
    Struts 2 include tag is used to include JSP or HTML page directly into the current page.
  • i18n tag example
    Struts 2 i18n tag is used to get the message from any declared resource bundle, not just the resource bundle that associated with the current action.
  • param tag example
    Struts 2 param tag is used to parametrize other tags.
  • property tag example
    Struts 2 property tag is used to get the property value from a class, which will default to the current Action class.
  • push tag example
    Struts 2 push tag is used to push value to the top of stack, so that it can be access or reference easily.
  • set tag example
    Struts 2 set tag is used to assign a value to a variable in a specified scope (application, session, request, page, or action).
  • text tag example
    Struts 2 text tag is used to get the message from the resource bundle that’s bundle with the action class.
  • url tag example
    Struts 2 “url” tag is used to create an URL and output it as a text format.

Struts 2 Resource Bundle & Localization

Struts 2 resource bundle to support the web localization features (multiple language).

Struts 2 Theme

Struts 2 layout is designed by the “xhtml” theme, understand the Struts 2 theme concept is a “MUST”.

Struts 2 Integrate with Other Frameworks

Anything about the Struts 2 integration with others framework – Spring, Hibernate, Quartz, Log4j…

Struts 2 FAQ

Struts 2 FAQ.

Struts 2 Common Errors

Struts 2 common errors.

Struts 2 Reference

About the Author

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

Comments

Leave a Reply

avatar
newest oldest most voted
Cithan Selva
Guest
Cithan Selva

Help me to recover this issue. Actually this is working fine. Unexpectedly it raises error at starting stage itself.

nile
Guest
nile

how to take snap using webcam by opening jsp page
nileshnaik564@gmail.com

Pritam Sanjore
Guest
Pritam Sanjore

Hi,
I am using your theme=”simple” for my struts2 application,but while using theme=”simple” in my jsp client side validation using “ActionName-validation.xml” is not working. Is there any way I can make this to work? If so please tell me how to do that ASAP. while removing theme=”simple” from my jsp my page structuring is loosing.

MANISH KUMAR
Guest
MANISH KUMAR

Hi MKyong,

How to return the previous page and as a control come same privios action name using struts2

I am using another action name and control back to previous page but i return previous page but i am not get value of previous page ???

and i am using back button to return previous page and in previous page jsp page include 5 jsp page ….

My doubt is i am not able to return previus action name or previous vale ………?????

please help me !!!!!

DINESH M K
Guest
DINESH M K

MKYoung

I developing an web application in Struts 2 with Struts2 jQuery plugins. I need to include the jQuery.ValidationEngine for the form validation which has Strtus2jQuery datepicker.

But when I did it. The Struts2jQuery is not working and shows the “‘initdatepicker’ exception “.

Can you help me with it.

Thanks,
DINESH M K.

Alias70
Guest
Alias70

HI, I have a problem with struts2 convention + sitemesh decorator.

I have an action that can be or not be decorated.
For Example i can call it as follow
/web/MyAction.action
/web/nodecorate/MyAction.action

So, in my decorator.xml, i have:

/*

/nodecorate/*

The action is mapped as:

@Namespaces({@Namespace(“/), @Namespace(“/nodecorate”)})
public class MyAction

How can I redirect both action calling to the same JSP?

Kisa
Guest
Kisa

Hi Mkyong,

We encountering initialization issues with our application when migrated from WAS 7 to WAS 8.5. Just want to ask if websphere8.5 has compatibility requirements for struts(like upgrade to higher version). Currently we are using 2.0.x version of struts. I can’t seem to find any articale that will tell that Websphere 8 works with specific struts version.
initialization issue is actually resolved when upgraded to 2.3.16 version of struts but lots of defects within the application are coming out due to deprecated classes from 2.0.

fiky yuvita
Guest
fiky yuvita

i want to know abaout combobox in struts2 that filled from database. i got an error and i can’t fixed it. please help me…

Prakash
Guest
Prakash

Hi,I developed application in struts.I have memory leaks in my appliction.How to fix the memory leaks.Please help me….

Htoo Kyaw Naing
Guest
Htoo Kyaw Naing

Buy memory and replace it.

Felix Thomas
Guest
Felix Thomas

Require one urgent help. If i implement tiles with struts2 in the following order
–header.jsp
–my body content goes here(For eg. addCustomer.jsp)
–footer.jsp

when i click on any menu the page will be getting loaded as mentioned above. However, if i do the server side validation on the page(body) when the input is returned by struts(ie., not a valid input value) my page layout of struts is not working. It is directly showing only the body(addCustomer.jsp) with no header and no footer. Any help would be appreciated.

Dhruv
Guest
Dhruv

How to remove the request parameters from the url header when I am using Struts2 tiles..??

Prasad
Guest
Prasad

Hello,
Can we use struts 2 project(project1) as a plug-in in struts-plugin.xml and after adding it has a plug-in in xml file can we use project1 in another struts2 project(project2).
if yes can u please put down that code with an example am in need of it.
thanks in advance.

sarath thota
Guest
sarath thota
hi, i upgrade my application from struts2.0.11 to struts2.3.15.1 jars. Now i am getting problem in validation.xml like below: [ERROR] 2013-09-10 12:24:16,852 [AnnotationActionValidatorManager] (CommonsLogger.java:error:38) Caught exception while loading file com/sutisoft/esign/struts/action/LoginAction-validation.xml http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd – Class: sun.net.www.protocol.http.HttpURLConnection File: HttpURLConnection.java Method: getInputStream Line: 1239 – sun/net/www/protocol/http/HttpURLConnection.java:1239:-1 at com.opensymphony.xwork2.util.DomHelper.parse(DomHelper.java:119) at com.opensymphony.xwork2.validator.DefaultValidatorFileParser.parseActionValidatorConfigs(DefaultValidatorFileParser.java:82) at com.opensymphony.xwork2.validator.AnnotationActionValidatorManager.loadFile(AnnotationActionValidatorManager.java:394) at com.opensymphony.xwork2.validator.AnnotationActionValidatorManager.buildClassValidatorConfigs(AnnotationActionValidatorManager.java:279) at com.opensymphony.xwork2.validator.AnnotationActionValidatorManager.buildValidatorConfigs(AnnotationActionValidatorManager.java:371) at com.opensymphony.xwork2.validator.AnnotationActionValidatorManager.getValidators(AnnotationActionValidatorManager.java:102) at org.apache.struts2.components.Form.getValidators(Form.java:281) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at freemarker.ext.beans.BeansWrapper.invokeMethod(BeansWrapper.java:866) at freemarker.ext.beans.SimpleMethodModel.exec(SimpleMethodModel.java:106) at freemarker.core.MethodCall._getAsTemplateModel(MethodCall.java:93) at freemarker.core.Expression.getAsTemplateModel(Expression.java:89) at freemarker.core.IteratorBlock.accept(IteratorBlock.java:94) at freemarker.core.Environment.visit(Environment.java:221) at freemarker.core.IteratorBlock$Context.runLoop(IteratorBlock.java:167) at freemarker.core.Environment.visit(Environment.java:428) at freemarker.core.IteratorBlock.accept(IteratorBlock.java:102) at freemarker.core.Environment.visit(Environment.java:221) at freemarker.core.MixedContent.accept(MixedContent.java:92) at freemarker.core.Environment.visit(Environment.java:221) at freemarker.core.ConditionalBlock.accept(ConditionalBlock.java:79) at freemarker.core.Environment.visit(Environment.java:221) at freemarker.core.MixedContent.accept(MixedContent.java:92) at freemarker.core.Environment.visit(Environment.java:221) at freemarker.core.Environment.include(Environment.java:1508) at… Read more »
sushil kumar bhaskar
Guest
sushil kumar bhaskar

^[0-9]{10}$

Invalid contact Number(10 digit)

Rakesh
Guest
Rakesh

This site is very effective for me and i want to dummy project on struts 2.0 which is totaly used the struts2.0 and database mysql . Please anyone Help me

Thanks & regards

Rakesh

Jeni
Guest
Jeni

This site was amazing !!!

Ask
Guest
Ask

Hi mkyong,
Struts 1 supported the following actions.
http://www.javabeat.net/2007/07/introduction-to-struts-actions/

what are the different kinds of actions do the struts2 supported?

Any help will be greatly appreciated.
Thanks in advance.

Frank
Guest
Frank

Can you post us an example of Rest Implementation on Struts 2,
No good example or any example is available on the net.

Immigration Solicitors in Brentwood
Guest
Immigration Solicitors in Brentwood

Thank you for sharing your thoughts. I truly appreciate
your efforts and I am waiting for your next write
ups thanks once again.

Ask
Guest
Ask

Hi,
Struts 1 supported the following actions.
http://www.javabeat.net/2007/07/introduction-to-struts-actions/

what are the different kinds of actions do the struts2 supported?

Any help will be greatly appreciated.
Thanks in advance.

Nevil Parekh
Guest
Nevil Parekh
Sir please help me out……… SEVERE: Dispatcher initialization failed Unable to load configuration. – bean – jar:file:/C:/Users/Nevil/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/Struts_Tutorial1/WEB-INF/lib/struts2-gxp-plugin-2.3.4.1.jar!/struts-plugin.xml:8:162 at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:69) at org.apache.struts2.dispatcher.Dispatcher.init_PreloadConfiguration(Dispatcher.java:390) at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:437) at org.apache.struts2.dispatcher.ng.InitOperations.initDispatcher(InitOperations.java:74) at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.init(StrutsPrepareAndExecuteFilter.java:51) at org.apache.catalina.core.ApplicationFilterConfig.initFilter(ApplicationFilterConfig.java:277) at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:258) at org.apache.catalina.core.ApplicationFilterConfig.setFilterDef(ApplicationFilterConfig.java:382) at org.apache.catalina.core.ApplicationFilterConfig.(ApplicationFilterConfig.java:103) at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4638) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5294) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1566) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1556) at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source) at java.util.concurrent.FutureTask.run(Unknown Source) at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at java.lang.Thread.run(Unknown Source) Caused by: Unable to load bean: type:org.apache.struts2.views.gxp.inject.InjectedObjectContainer class:org.apache.struts2.views.gxp.inject.InjectedObjectContainer – bean – jar:file:/C:/Users/Nevil/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/Struts_Tutorial1/WEB-INF/lib/struts2-gxp-plugin-2.3.4.1.jar!/struts-plugin.xml:8:162 at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.register(XmlConfigurationProvider.java:243) at org.apache.struts2.config.StrutsXmlConfigurationProvider.register(StrutsXmlConfigurationProvider.java:102) at com.opensymphony.xwork2.config.impl.DefaultConfiguration.reloadContainer(DefaultConfiguration.java:210) at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:66) … 18 more Caused by: java.lang.ClassNotFoundException: org.apache.struts2.views.gxp.inject.InjectedObjectContainer at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1711) at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1556) at com.opensymphony.xwork2.util.ClassLoaderUtil.loadClass(ClassLoaderUtil.java:152) at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.register(XmlConfigurationProvider.java:214) … 21 more Apr 11,… Read more »
Me
Guest
Me

Looks like the STRUTS2 Core files where NOT added to the deploy.

Hope this helps

rakesh
Guest
rakesh

please provide some tutorials abou javafx.

devi prasad ojha
Guest
devi prasad ojha

Hi MKYong,

Your struts tutorial was really very helpfull tom me.
Just have a case where i will be getting a request and i want to instantly send “OK” response and after sending i want to perform some further operations with that request.

Can you give me some idea how to do it.

Thanks again.

Rob
Guest
Rob

Will there any example of using cookies with Struts2?

Soheil
Guest
Soheil

Hi,

I have an issue in Struts2 and i can not get it resolved. I have a dynamically generated user table. I have two buttons (edit and delete) in front of each row. Each button type goes to different methods in userAdminAction. I know that my values are not correct, but i don’t get anything back in java for my userIds. Can you please help me….

JSP:

User ID
First Name
Last Name
Email
Status
Type

JAVA:
private List users = new ArrayList();
private String userId;
…..
public String edit() throws Exception {
System.out.println(userId);
return “success”;
}
public String Delete() throws Exception {
System.out.println(userId);
return “success”;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}

Firoz
Guest
Firoz

Hi Mkyong,
Usually I dont post any comment. But I must say this one of the best site I have ever gone through. I have started recommending this site to my friends/colleague .
Keep doing the good work mkyong . All the best.

Many Thanks
Firoz

mike
Guest
mike

I just start to learn struts 2. I have an issue about deploy several downloaded struts2 project or war files in Tomcat 7. I received requested resources not available error. Tomcat sample pages are working fine. I do find that there are no compiled jsp files under the work directories. The OS is window 8.
It appears that Tomcat cannot find the jsp files.
Any help will be greatly appreciated.
Thanks in advance.

Nevil Parekh
Guest
Nevil Parekh
Please help me out…… Error::: Feb 27, 2013 3:15:34 PM org.apache.catalina.core.AprLifecycleListener init INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files (x86)\Java\jre7\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:/Program Files (x86)/Java/jre7/bin/client;C:/Program Files (x86)/Java/jre7/bin;C:/Program Files (x86)/Java/jre7/lib/i386;C:\oraclexe\app\oracle\product\10.2.0\server\bin;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Intel\OpenCL SDK\2.0\bin\x86;C:\Program Files (x86)\Intel\OpenCL SDK\2.0\bin\x64;C:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn\;C:\Program Files\Microsoft SQL Server\100\Tools\Binn\;C:\Program Files\Microsoft SQL Server\100\DTS\Binn\;C:\Program Files (x86)\Microsoft SQL Server\90\Tools\binn\;D:\Applications\eclipse;;. Feb 27, 2013 3:15:34 PM org.apache.tomcat.util.digester.SetPropertiesRule begin WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property ‘source’ to ‘org.eclipse.jst.jee.server:Struts_starter’ did not find a matching property. Feb 27, 2013 3:15:35 PM org.apache.coyote.AbstractProtocol init INFO: Initializing ProtocolHandler [“http-bio-8080”] Feb 27, 2013 3:15:35… Read more »
IBM
Guest
IBM

Dear All,

Could you please share the src code, how to upload .csv file into database using core Java?
I would appreciated if some one can help me in this regards.

Regards,
Prateek

Overseas qualified nurses visa
Guest
Overseas qualified nurses visa

I_m not that much of a internet reader to be honest but your sites really nice, keep it
up! I’ll go ahead and bookmark your site to come back in the future. All the best

for beginners
Guest
for beginners

Oh my goodness! Amazing article dude! Many thanks, However I am having
troubles with your RSS. I don’t understand why I can’t join it.

Is there anyone else having identical RSS issues? Anyone who knows
the solution can you kindly respond? Thanks!!