Struts 2 Annotations ExampleStruts 2 provides you convenient way to create struts application using annotations. So, there is no need to have struts.xml file. As we have said earlier, there are 2 ways to use zero configuration file (no struts.xml file).
Annotations used in struts 2 applicationFor simple annotation example of struts 2, we can use 3 annotations: 1) @Action annotation is used to mark the action class. 2) @Results annotation is used to define multiple results for one action. 3) @Result annotation is used to display single result. Example of struts 2 application using annotationsYou need to create 4 files for struts annotated application:
Let's have a look at the directory structure first. 1) Create index.jsp for inputThis jsp page creates a form using struts UI tags that receives name from the user. index.jsp2) Create the action classThis action class uses the annotations for the action and result. RegisterAction.java3) Create struts.properties file inside src directorystruts.properties4) Create result.jsp to display messageThis jsp page displays the username. result.jspOutput |