A simple Spring Entry example

Source: Internet
Author: User
At the request of a friend, write one of the simplest spring examples, use spring's MVC, and apply spring's dependency injection to implement a simple application, simply put it here for the beginners of spring enthusiasts reference, a preliminary feel the spring application ( Spring masters do not have to look, there is no high-level features, such as integration with ORM Framework, transaction management, remote invocation, agents and other functions.

One of the most important part of spring is the assembly, the writing of the configuration file, and I'm going to explain it step-by-step in the actual process.

First, to configure Dispatcherservlet in Web.xml, it is the front-end controller for spring MVC. Must be configured in Web.xml as follows


Ntx
Org.springframework.web.servlet.DispatcherServlet
1

In fact, the spring configuration file can be sliced into multiple XML files, and our simple example configures it into Ntx.xml.

! DOCTYPE Beans Public
"-//spring//dtd bean//en"
"Http://www.springframework.org/dtd/spring-beans.dtd" >

Default-autowire= "No"
Default-lazy-init= "false"
Default-dependency-check= "None"
>




/showresult.jsp




logincontroller




After you configure these, you want to create the ntx-servlet.xml below Web-inf:

! DOCTYPE beans Public "-//spring//dtd bean//en" "Http://www.springframework.org/dtd/spring-beans.dtd"

org.springframework.web.servlet.view.JstlView




Next, to indicate which requests will be handed over to Spring's dispatcherservlet, add the <servlet-mapping> in Web.xml


Ntx
*.do

In order to properly load configuration files such as Dispatcherservlet, We're going to configure a context loader Contextloaderlistener or Contextloaderservlet in Web.xml, and we're here to use the second one for compatible versions of the lower Serlvet container (actually I'm using 2.4):


Context
Org.springframework.web.context.ContextLoaderServlet


This is all configured, of course, the above ntx.xml is my project after the completion of the configuration, here no more, there are bean elements of the configuration you can refer to the information to understand, it is easy to understand, the following gives a complete web.xml configuration and Java<?xml Version= "1.0" encoding= "UTF-8"?

xmlns= "http://java.sun.com/xml/ns/j2ee"
xmlns:xsi= "http://www.w3.org/2001/" Xmlschema-instance "
xsi:schemalocation=" HTTP://JAVA.SUN.COM/XML/NS/J2EE
http://java.sun.com/xml/ns/j2ee/ Web-app_2_4.xsd ">


contextconfiglocation
/ Web-inf/ntx.xml


ntx
org.springframework.web.servlet.DispatcherServlet
1


context
org.springframework.web.context.ContextLoaderServlet
100


ntx
*.do


index.jsp
welcome-file-list>

According to Ntx.xml, there are a total of three Java files, Logincontroller.java is the controller, inherits the simplest controller (actually spring has a lot of controllers for us to choose), followed by the source of a simple controller:

/** *//**
* Program Ntxspring
* Date 2006-9-27
* @author Zhang Yixuan
*/
Package Ntx.controller;

Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;

Import Org.springframework.web.servlet.ModelAndView;
Import Org.springframework.web.servlet.mvc.Controller;

Import Ntx.service.LoginService;

/** *//** @spring. Bean id= "Logincontroller"
* @spring. Property name= "Gotourl" value= "/showresult.jsp"
* @spring. Property name= "Loginservice" ref= "Loginservice"
*/

/** *//**
* Role Description: spring example
*
* Description: Spring example, the spring tag above makes it easy to use Xdoclet to generate the spring configuration file
*
* @author Zhang Yixuan
* Copyright (c) 2006 Cleverfox
*/
public class Logincontroller implements Controller ... {

Private Loginservice Loginservice;
Private String Gotourl;
Public Modelandview HandleRequest (httpservletrequest request,httpservletresponse response) throws Exception ... {
String userName = Request.getparameter ("UserName");
This.getuserinfo (Request, userName);
return new Modelandview (This.getgotourl ());
}

private void GetUserInfo (HttpServletRequest request,string userName) ... {
String userInfo = Loginservice.getuserinfo (userName);
Request.setattribute ("UserInfo", userInfo);
}

Public String Getgotourl () ... {
return gotourl;
}
public void Setgotourl (String gotourl) ... {
This.gotourl = Gotourl;
}
Public Loginservice getloginservice () ... {
return loginservice;
}
public void Setloginservice (Loginservice loginservice) ... {
This.loginservice = Loginservice;
}

}

Also has the service layer the interface as well as realizes, is simpler.

Package ntx.service;
Public interface Loginservice ... {
public string GetUserInfo (string userName);
}package Ntx.service.serviceimpl;

Import Ntx.service.LoginService;

public class Loginserviceimpl implements Loginservice ... {
public string GetUserInfo (String userName) ... {
Return "Your name is:" + userName;
}
}

OK, finally, there are two JSP files, a index.jsp used to display a form, enter a name, a showresult.jsp to display the results, and only post the relevant code:


This is the My Test Spring page.









The result:



Published to Tomcat or other servlet containers can be used normally and will be displayed after submission:

The result:

Your name is: Gavin


This is a simple introductory example, hoping to help people who have just come in contact with spring to deepen their understanding of spring.
Related Article

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.