Simple and simple spring entry examples are also popular

Source: Internet
Author: User
At the request of a friend, write a simple spring example using spring MVC and apply spring dependency injection to implement simple applications, simply put it here for the reference of spring fans who are not getting started. I have a preliminary idea about the spring application (Spring experts don't have to read it. It does not involve advanced features, such as integration with the ORM framework, transaction management, remote calls, proxy, and other functions)

The most important part of spring is assembly, that is, the compilation of configuration files. Next I will explain it step by step in the actual process.

First, configure dispatcherservlet in Web. XML as the front-end controller of spring MVC. It must be configured in Web. XML as follows:

<Servlet>
<Servlet-Name> NTX </servlet-Name>
<Servlet-class> org. springframework. Web. servlet. dispatcherservlet </servlet-class>
<Load-on-startup> 1 </load-on-startup>
</Servlet>

In fact, the spring configuration file can be split into multiple XML files. In this simple example, we configure it to NTX. xml.

<? XML version = "1.0" encoding = "UTF-8"?>
<! Doctype beans public
"-// Spring // DTD bean // en"
Http://www.springframework.org/dtd/spring-beans.dtd>

<Beans
Default-autowire = "no"
Default-Lazy-init = "false"
Default-dependency-check = "NONE"
>

<Bean id = "loginservice" class = "NTX. Service. serviceimpl. loginserviceimpl"/>

<Bean id = "logincontroller" class = "NTX. Controller. logincontroller">
<Property name = "loginservice">
<Ref bean = "loginservice"/>
</Property>
<Property name = "gotourl">
<Value>/showresult. jsp </value>
</Property>
</Bean>

<Bean id = "simpleurlhandlermapping" class = "org. springframework. Web. servlet. handler. simpleurlhandlermapping">
<Property name = "mappings">
<Props>
<Prop key = "/userlogin. Do"> logincontroller </prop>
</Props>
</Property>
</Bean>
</Beans>

After configuring these above, you need to create a WEB-INF under the ntx-servlet.xml as follows:

<? XML version = "1.0" encoding = "UTF-8"?>
<! Doctype beans public "-// spring // DTD bean // en" "http://www.springframework.org/dtd/spring-beans.dtd">
<Beans>
<Bean id = "viewresolver" class = "org. springframework. Web. servlet. View. internalresourceviewresolver">
<Property name = "viewclass"> <value> org. springframework. Web. servlet. View. jstlview </value> </property>
<Property name = "prefix"> <value> </property>
<Property name = "suffix"> <value> </property>
</Bean>
</Beans>

Next, specify which requests will be sent to spring's dispatcherservlet for processing. Therefore, add <servlet-mapping> in Web. xml.

<Servlet-mapping>
<Servlet-Name> NTX </servlet-Name>
<URL-pattern> *. DO </url-pattern>
</Servlet-mapping>

To load configuration files such as dispatcherservlet correctly. configure a context loader contextloaderlistener or contextloaderservlet in XML. Here we use the second method to be compatible with the Serlvet container of a lower version (actually I use 2.4:

<Servlet>
<Servlet-Name> context </servlet-Name>
<Servlet-class> org. springframework. Web. Context. contextloaderservlet </servlet-class>
<Load-on-startup> 100 </load-on-startup>
</Servlet>

In this way, all the configurations are complete. Of course, the above NTX. XML is configured only after the project is completed. I will not talk about it more here. You can refer to the relevant materials for bean element configuration, which is easy to understand. The complete Web is provided below. xml configuration and Java <? XML version = "1.0" encoding = "UTF-8"?>

<Web-app version = "2.4"
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>

<Context-param>
<Param-Name> contextconfiglocation </param-Name>
<Param-value>/WEB-INF/NTX. xml </param-value>
</Context-param>
<Servlet>
<Servlet-Name> NTX </servlet-Name>
<Servlet-class> org. springframework. Web. servlet. dispatcherservlet </servlet-class>
<Load-on-startup> 1 </load-on-startup>
</Servlet>
<Servlet>
<Servlet-Name> context </servlet-Name>
<Servlet-class> org. springframework. Web. Context. contextloaderservlet </servlet-class>
<Load-on-startup> 100 </load-on-startup>
</Servlet>
<Servlet-mapping>
<Servlet-Name> NTX </servlet-Name>
<URL-pattern> *. DO </url-pattern>
</Servlet-mapping>
<Welcome-file-List>
<Welcome-File> index. jsp </welcome-File>
</Welcome-file-List>
</Web-app>

According to NTX. XML knows that there are a total of three java files, logincontroller. java is a controller that inherits the simplest controller (in fact, spring has many controllers for us to choose from). The following is the source code 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"
*/

/***//**
* Function Description: Spring example
*
* Description: Spring example. The above spring labels are used to generate spring configuration files conveniently using XDoclet.
*
* @ Author Zhang yixuan
* Copyright (c) 2006 cleverfox
*/
Public class logincontroller implements controller ...{

Private 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 )...{
This. loginservice = loginservice;
}

}

There are also service-layer interfaces and implementations, which are relatively simple.

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;
}
}

Now, there are two JSP files. One index. jsp is used to display a form, the input name, and the other showresult. jsp is used to display the result.Code:

<Body>
This is my test spring page. <br>
<Div>
<Form method = "Post" Action = "/userlogin. Do">
<Input type = "text" name = "username" size = "30"/> <br/>
<Input type = "Submit" value = "Submit"/>
</Form>
</Div>
</Body>

<Body>
This is the result: <br>
<C: Out value = "$ {userinfo}" default = "no result"/>
</Body>

Publishing to tomcat or other servlet containers can be used normally. After submission, the following information will be displayed:

This is the result:

Your name is Gavin.

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.