Once the DispatcherServlet is initialized, it will looks for a file name -servlet.xml in WEB-INF folder of web application. One thing to note here is the name of servlet in tag in web.xml. Also note that we have define index.jsp as welcome file. 1 spring *.html Code language: HTML, XML ( xml )The above code in web.xml will map DispatcherServlet with url pattern *.html. Create hello.jsp under WEB-INF/jsp directory and copy following content into it. Note that this JSP is created in folder /WEB-INF/jsp. The View: Create JSP To display the hello world message we will create a JSP. Normally this will be a value object in form of java bean that will contain the data to be displayed on our view. This is the data that we are passing to our view. The ModelAndView object also contains a message with key “message” and value “Hello World, Spring 3.0!”. We will discuss this shortly how the logical name “hello” which is return in ModelAndView object is mapped to path /WEB-INF/jsp/hello.jsp. The logical view name will resolve to "/WEB-INF/jsp/hello.jsp". The ModelAndView object tries to resolve to a view named “hello” and the data model is being passed back to the browser so we can access the data within the JSP. The helloWorld() method returns ModelAndView object. The annotation tells Spring that this Controller should process all requests beginning with /hello in the URL path. When Spring scans our package, it will recognize this bean as being a Controller bean for processing requests. Return new ModelAndView( "hello", "message", message) Ĭode language: Java ( java )Note that we have annotated the HelloWorldController class with and on line 7 and 10. String message = "Hello World, Spring 3.0!" Create a class called HelloWorldController in package and copy following content into it. This package will contain the Controller file. For this we will create a package in the source folder. The Spring Controller Class We will need a spring mvc controller class that will process the request and display a “Hello World” message. Create this folder if it does not exists. Now copy all the required JAR files in WebContent > WEB-INF > lib folder. Once the project is created, you can see its structure in Project Explorer. This is to run the project inside Eclipse environment. Once this is done, select the target runtime environment (e.g. Open Eclipse and goto File -> New -> Project and select Dynamic Web Project in the New Project wizard screen.Īfter selecting Dynamic Web Project, press Next. Getting Started Let us start with our first Spring 3.0 MVC based application. On clicking this link, user will be redirected to another page hello which will display a message “Hello World, Spring 3.0!”. There will be an index page which will display a link “Say Hello” to user. Our Goal Our goal is to create a basic Spring MVC application using latest 3.0 version. Note that depending on the current version of Spring MVC, the version number of above jar files may change. Following are the list of JAR files required for this application.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |