Hello world program in struts using netbeans
After the Action is executed, a result selects a resource to render the response. Assuming that you already have built your development environment. Now, let us proceed for building our first Hello World Struts2 project. The aim of this project is to build a web application that collects the user's name and displays "Hello World" followed by the user name. Create an action class which will contain complete business logic and control the interaction between the user, the model, and the view.
Create configuration files to couple the Action, View and Controllers. These files are struts. Let us now start with creating Dynamic Web Project. Select all the default options in the next screens and finally check Generate Web. This will create a dynamic web project for you in Eclipse. Action class is the key to Struts 2 application and we implement most of the business logic in action class.
So let us create a java file HelloWorldAction. The Action class responds to a user action when user clicks a URL. One or more of the Action class's methods are executed and a String result is returned. Based on the value of the result, a specific JSP page is rendered. We will now create the user interface. In this demo example we will use the JSP technology for rendering the user interface. Struts 2 supports other view technologies such as freemarker templates and velocity templates.
Note the use of custom tags in the JSP. These custom tags are provided by Struts 2. The Struts 2 custom tags use the OGNL expressions to refer to the data exposed by the controller class. This is achieved by the Struts 2 framework by automatically exposing the member variables of the controller class to the tag libraries.
In the above example, Struts 2 property tag refers to the value message. The framework automatically translates this to the following call on the controller action class by convention.
Since we have already prepared the Message object in the HelloWorld controller, we get the "Hello World! Finally we need to inform Struts 2 framework about our controllers, action classes and view files.
This can be configured in struts. This file needs to be located at the root of the source tree it needs to be in the classpath.
We have specified an action named HelloWorld and indicated which class file needs to be invoked for the action request. Also we have configured a result which points to our view component message. Struts 2 framework will forward the request to message. Run the project from NetBeans. To see the application in action, enter the following URL in the browser,. Following are some of the common errors encountered by beginners when working with the above sample program.
This is due to the use of a wrong filter in the web. Some of the obsolete Struts2 documentation uses org. StrutsExecuteFilter as the filter class in web. This is no longer a valid filter class, change this to org. This error indicates that you don't have the commons io library in the Web application library folder. Add commons-io This error indicates that common lang dependent library is missing.
Be Careful When You Type. You have successfully compiled your program! Errors are reported in the Output window as hyperlinked text. You double-click such a hyperlink to navigate to the source of an error. You can then fix the error and once again choose Run Build Project. When you build the project, the bytecode file HelloWorldApp. The program prints "Hello World! The next few pages of the tutorial will explain the code in this simple application. After that, the lessons go deeper into core language features and provide many more examples.
Although the rest of the tutorial does not give specific instructions about using the NetBeans IDE, you can easily use the IDE to write and run the sample code. Once you have created a project in the IDE, you can add files to the project using the New File wizard. You might want to create separate IDE projects for sample applications that include more than one source file. As you are typing in the IDE, a code completion box might periodically appear.
You can either ignore the code completion box and keep typing, or you can select one of the suggested expressions. Active 10 years, 10 months ago. Viewed times. Improve this question. Show us what you've done so far. Add a comment. Active Oldest Votes. Improve this answer. Costis Aivalis Costis Aivalis Sign up or log in Sign up using Google.
0コメント