Spring learning journey (1)-First Controller

Source: Internet
Author: User
Tags jboss

After two days of unremitting efforts, my first spring WebProgramFinally, the result is displayed. This article records the detailed development process to commemorate your hard work.

 

Step 1: Prepare the development environment. My development environment uses eclipse indigo javaee 3.7.2 + JBoss tools 3.3 + springsource tool suite for eclipse indigo (3.7) 2.9.1.

 

Step 2: Download the Spring framework. The latest version is 3.1.1.release, Which is release. Decompress the downloaded file to D: \ develop \ spring-framework-3.1.1, which is the directory structure after the unzipping:

 

Step 3: Open eclipse, create a dynamic web project guestbook-spring, set D: copy all jar files under the \ develop \ spring-framework-3.1.1 \ Dist directory to the project's WEB-INF/lib directory. In fact, many files under the DIST directory will not be used in this article, but for future convenience, all jar files will be copied at one time.

 

Step 4: add the Controller hellocontroller. java. The complete content is as follows:

 1  Package  Org. Reindeer. tutorials. guestbook. Spring;  2   3   Import  Org. springframework. stereotype. Controller;  4   Import  Org. springframework. Web. Bind. annotation. requestmapping;  5   Import  Org. springframework. Web. Bind. annotation. requestmethod;  6   Import Org. springframework. Web. servlet. modelandview;  7   8   @ Controller  9   Public   Class  Hellocontroller {  10 @ Requestmapping (value = "/Hello", method = Requestmethod. Get)  11       Public  Modelandview index (){  12           Return  New Modelandview ("welcome" );  13   }  14 }

Note:

    • The @ controller in line 3 indicates that the hellocontroller class is a controller of spring MVC.
    • Line 10 @ requestmapping indicates the request ing of the method. The current method maps to the/Hello GET request.
    • The welcome of the 12th rows is the view name.

 

Step 5: Add file WEB-INF/views/welcome. jsp, the complete content is as follows:

 <%  @ Page Language  =  " Java  "  Contenttype  =  "  Text/html; charsets = UTF-8  "  Pageencoding  =  "  UTF-8 "  %>  <%  @ Page Import  =  "  Java. util .*  "   %>  <!  Doctype HTML public "-// W3C // dtd html 4.01 transitional // en" "http://www.w3.org/TR/html4/loose.dtd" >  <  Html  >  <  Head  >  <  Meta  HTTP-equiv  = "Content-Type"  Content  = "Text/html; charsets = UTF-8"  >  <  Title  > Insert title here </  Title  >  </  Head  >  <  Body  >  Welcome!  <  BR  /> Current time is <%  = New   Date  ()  %>  </  Body  >  </  Html  > 

 

Step 6: Add a file WEB-INF/config/application-context.xml, the complete content is as follows:

 <?  XML version = "1.0" encoding = "UTF-8"  ?> <  Beans  Xmlns  = "Http://www.springframework.org/schema/beans"  Xmlns: xsi  = "Http://www.w3.org/2001/XMLSchema-instance"  Xmlns: Context  = "Http://www.springframework.org/schema/context"  Default-Lazy-init  = "True"  Xmlns: MVC  = "Http://www.springframework.org/schema/mvc"  Xmlns: P = "Http://www.springframework.org/schema/p"  Xsi: schemalocation  = "Http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"  >      <  MVC: annotation-driven  />      <  Context: component-Scan  Base-Package = "Org. Reindeer. tutorials. guestbook. Spring"   />      <  Bean  Class  = "Org. springframework. Web. servlet. View. internalresourceviewresolver"  P: prefix  = "/WEB-INF/views /"  P: suffix  = ". Jsp"   />  </  Beans  > 

 

Step 7: Add a file WEB-INF/config/servlet-context.xml with the complete content as follows:

 <?  XML version = "1.0" encoding = "UTF-8"  ?>  <  Beans  Xmlns  = "Http://www.springframework.org/schema/beans"  Xmlns: xsi  = "Http://www.w3.org/2001/XMLSchema-instance"  Xmlns: Context  = "Http://www.springframework.org/schema/context"  Default-Lazy-init  = "True" Xmlns: MVC  = "Http://www.springframework.org/schema/mvc"  Xmlns: P  = "Http://www.springframework.org/schema/p"  Xsi: schemalocation  = "Http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"  >  </  Beans  > 

 

Step 8: Modify WEB-INF/Web. XML with the complete content:

 <?  XML version = "1.0" encoding = "UTF-8"  ?>  <  Web-app  ID  = "Webapp_id"  Version  = "3.0"  Xmlns  = "Http://java.sun.com/xml/ns/javaee"  Xmlns: xsi  = "Http://www.w3.org/2001/XMLSchema-instance"  Xsi: schemalocation = "Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"  >   <  Display-name  > Guestbook-spring </  Display-name  >   <  Context-Param  >    <  Param-name  > Contextconfiglocation</  Param-name  >    <  Param-Value  > /WEB-INF/config/application-context.xml </  Param-Value  >   </  Context-Param  >   <  Listener  >    < Listener-class  > Org. springframework. Web. Context. contextloaderlistener </  Listener-class  >   </  Listener  >   <  Servlet  >    <  Servlet-name  > Spring-Servlet </ Servlet-name  >    <  Servlet-class  > Org. springframework. Web. servlet. dispatcherservlet </  Servlet-class  >    <  Init-Param  >     <  Param-name  > Contextconfiglocation </ Param-name  >     <  Param-Value  > /WEB-INF/config/servlet-context.xml </  Param-Value  >    </  Init-Param  >   </  Servlet  >   <  Servlet-Mapping >    <  Servlet-name  > Spring-Servlet </  Servlet-name  >    <  URL-Pattern  > /Spring /* </  URL-Pattern  >   </  Servlet-Mapping  > </  Web-app  > 

 

Last step: Release the project to JBoss as, open the browser, and enter the URL http: // localhost: 8080/guestbook-spring/Hello, we can see our welcome page.

 

Let's explain in detail the entire implementation process:

  • To integrate the spring MVC framework in a web application, you must register it in the web. xml file.Org. springframework. Web. Context. contextloaderlistenerListeners andOrg. springframework. Web. servlet. dispatcherservletService Program.
  • ContextloaderlistenerListener usage<Context-param> contextconfiglocation </context-param>The specified configuration file (in this example/WEB-INF/config/application-context.xml) Initialize the application context.
  • DispatcherservletService Program usage<Init-param> contextconfiglocation </init-param>The specified configuration file (in this example/WEB-INF/config/servlet-context.xml) Initialize the application context.
  • ContextloaderlistenerContext andDispatcherservletThe context is subordinate to the upper and lower levels. Only one web application can be definedContextloaderlistenerListeners, but multiple listeners can be defined.DispatcherservletService Program. EachDispatcherservletService programs can have their own local context, but they all share the sameContextloaderlistenerContext.
  • Application-context.xmlThe annotation function of spring MVC is used.<MVC: annotation-driven>Indicates that the spring annotation function is enabled,<Context: component-scan>Indicates spring searchOrg. Reindeer. tutorials. guestbook. SpringAll classes in the package.
  • Org. springframework. Web. servlet. View. internalresourceviewresolverIndicates that the built-in view Parsing Method of spring should be used, and each view should correspond to a file/WEB-INF/views/view name. jsp. For example,Hellocontroller. Index ()Method returnsWelcomeTo the internal file.// WEB-INF/views/welcome. jsp.
  • In the Web. xml file, we specify the request format of spring-Servlet/Spring /*, CorrespondingHellocontroller. Index ()The request URI of the method should be/Spring/Hello.

 

Guestbook-spring project file (excluding spring library files): guestbook-spring-0.01.rar

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.