Simple spring entry instance

Source: Internet
Author: User
Preface

This article is just a simple introduction to spring MVC.

Environment and jar package preparation

1. ide-eclipse3.7

2. Spring:

Spring3.2.3 --

Http://repo.springsource.org/libs-release-local/org/springframework/spring/3.2.3.RELEASE/spring-framework-3.2.3.RELEASE-dist.zip
Spring3.0.2--
Http://s3.amazonaws.com/dist.springframework.org/release/SPR/spring-framework-3.0.2.RELEASE-dependencies.zip

3.2.3 is a newer version of spring. Currently, the highest version is 4.0.0.

Why are the addresses of two versions provided here?

The reason is:
Spring requires support from other dependent packages, such as log4j and jstl,

In the spring version release, some versions have released dependency packages synchronously, but some have not. Versions 3.0.2 and 3.2.3 are available. (It may be because the dependent package has not been changed)

If you only want to learn, you can download version 3.0.2. If you want to use the latest version of spring for development, you can use the latest version of spring and the old version of the dependent package.

The example here is very simple. Spring MVC is used to switch from an index. jsp page to a login page (login. jsp ).

Procedure

1. Create a dynamic web project under eclipse.

(The project name is springweb ")

2. Copy the downloaded spring package and dependency package to the project's webcontent/WEB-INF/lib.

3. Modify the Web. xml file under the WEB-INF

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">  <display-name>springweb</display-name>  <servlet>          <servlet-name>spring-dispatcher</servlet-name>          <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>             <init-param>              <param-name>contextConfigLocation</param-name>              <param-value>classpath*:com/oscar999/resource/spring-dispather.xml</param-value>          </init-param>          <load-on-startup>1</load-on-startup>    </servlet>         <servlet-mapping>          <servlet-name>spring-dispatcher</servlet-name>          <url-pattern>*.do</url-pattern>    </servlet-mapping>      <welcome-file-list>          <welcome-file>index.jsp</welcome-file>    </welcome-file-list>  </web-app>

The core controller is configured here to intercept *. Do requests and submit them to spring for processing,

The processing is based on the spring-dispather.xml document.

The configuration method of this file is: classpath *: COM/oscar999/resource/spring-dispather.xml, that is, the path of the configuration XML file, under the com. oscar999.resource package of javasrc.

4. Add index. jsp

Add index. jsp in the webcontent path

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

It is easy to import directly to the logon page after entering the home page.

Note that the page here is not login. jsp, but login. Do

5. added the xml configuration file required by the IOC container.

That is the spring-dispather.xml file mentioned above. The file name and path can be specified or not.

If no path or name is specified: only spring will automatically go to the/WEB-INF directory to find the file named <servlet-Name>-servlet. xml.

Specify the path and name as described above.

Add a package for com. oscar999.resource under Java SRC. Add a spring-dispather.xml with the following content:

<?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:p="http://www.springframework.org/schema/p"      xmlns:context="http://www.springframework.org/schema/context"      xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc"      xsi:schemaLocation="  http://www.springframework.org/schema/utilhttp://www.springframework.org/schema/util/spring-util-3.1.xsdhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.1.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.1.xsdhttp://www.springframework.org/schema/mvc       http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">             <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">    <property name="mappings">         <props>             <prop key="login.do">loginController</prop>         </props>    </property>    </bean>        <bean id="loginController" class="org.springframework.web.servlet.mvc.ParameterizableViewController">    <property name="viewName" value="login" />    </bean>    <bean class="org.springframework.web.servlet.view.UrlBasedViewResolver">    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />    <property name="prefix" value="/WEB-INF/jsp/" />    <property name="suffix" value=".jsp" />    </bean> </beans>

An action and controller are defined here,

When the action is "login. Do", use logincontroller to find that the viewname is "login", and then use urlbasedviewresolver.

Finally, the JSP processing this action is =>/WEB-INF/JSP/login. jsp

6. Create login. jsp

The content here does not matter;

<% @ Page Language = "Java" contenttype = "text/html; charset = UTF-8" pageencoding = "UTF-8" %> <! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en" "http://www.w3.org/TR/html4/loose.dtd"> <HTML> 

Summary

In this example, we only use *. Do to replace *. jsp.

Then configure the *. Do action in XML to use which JSP.

It seems that C and V are separated.

More advantages of spring require deep learning ....

Additional

1.

Above

    <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">    <property name="mappings">         <props>             <prop key="login.do">loginController</prop>         </props>    </property>    </bean>        <bean id="loginController" class="org.springframework.web.servlet.mvc.ParameterizableViewController">    <property name="viewName" value="login" />    </bean>

Can be replaced by the following methods or

    <bean name="/login.do" class="org.springframework.web.servlet.mvc.ParameterizableViewController">    <property name="viewName" value="login" />    </bean>

2.Urlfilenameviewcontroller

The Controller parses XXX. Do into XXX. jsp.

3.Parameterizableviewcontroller: Create a controller with a parameterized View

You can configure the property of viewname to configure the view name.

The preceding example can be login or login2. You can create a corresponding JSP.


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.