Spring MVC Series: (1) SPRINGMVC Quick Start

Source: Internet
Author: User
Tags naming convention


A review of the STRUTS2,STRUTS2 framework has the following features:

struts.xml configuration file, must be named Struts.xml, and placed in the SRC directory "Configuration"

Create an action instance "singleton/Multiple cases" each time the action is requested

The action class continues to Actionsupport class "class level" directly or indirectly.

The business control method in the action class is always a similar signature and has no reference to the "method plane"

In the action class, the receive parameter is used by the member variable and the corresponding set method or Set/get method "member variable Plane"



1. What is SPRINGMVC, and what does it have to do with spring?

SPRINGMVC is a follow-on product of the spring framework, developed in the MVC-based presentation layer, similar to the STRUTS2 framework


650) this.width=650; "src=" http://s5.51cto.com/wyfs02/M02/87/64/wKiom1fe39Ty2frxAAD0XFzN6gU647.jpg "title=" 03_ The relationship between SPRINGMVC and spring. JPG "alt=" Wkiom1fe39ty2frxaad0xfzn6gu647.jpg "/>

2, the first knowledge springmvc work flow


Approximate process: Request action (/hello.action) →dispatcherservlet→handler→adapter→ Developer's Custom action Class (helloaction) →modelandview→view→dispatcherservlet→jsp page

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/87/61/wKioL1fe4BXTFQr9AAFQJDQB9G0041.jpg "title=" 04_ Initial knowledge SPRINGMVC Work flow chart solution. JPG "alt=" Wkiol1fe4bxtfqr9aafqjdqb9g0041.jpg "/>

3. Springmvc Quick Start

Steps:

(1) Introduction of JAR Package

(2) Configuring Web. Xml

(3) Write the action code and configure the URL map



3.1. Introduction of JAR Package

The jar package that needs to be introduced is divided into three parts:

Spring-core

Spring-web

Spring-webmvc

Spring-core

Commons-logging-1.2.jar

Spring-beans-3.2.5.release.jar

Spring-context-3.2.5.release.jar

Spring-core-3.2.5.release.jar

Spring-expression-3.2.5.release.jar

Spring-web Spring-web-3.2.5.release.jar
Spring-webmvc Spring-webmvc-3.2.5.release.jar



3.2. Configure Web. xml

Registering the core controller of the SPRINGMVC framework in the Web. XML text

<?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 " 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>springmvc01</display-name>  < welcome-file-list>    <welcome-file>index.jsp</welcome-file>   </welcome-file-list>  <!--  Register SPRINGMVC framework Core Controllers  -->  <servlet>   <servlet-name>springmvc</servlet-name>  <servlet-class> org.springframework.web.servlet.dispatcherservlet</servlet-class>  </servlet>   <servlet-mapping>  <servlet-name>springmvc</servlet-name>  < Url-pattern>*.action</url-pattern>  </servlet-mApping></web-app> 



3.3. Write the action class and configure the URL map


(1) Code to write the action class

Helloworldaction.java

Package Com.rk.web.action;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;import Org.springframework.web.servlet.modelandview;import Org.springframework.web.servlet.mvc.controller;public class Helloworldaction implements Controller {public Modelandview HandleRequest (httpservletrequest request,httpservletresponse response) throws Exception {Modelandview Modelandview = new Modelandview (); Modelandview.addobject ("Message", "This is my first SPRINGMVC application"); modelandview.setviewname ("/jsp/success.jsp"); return Modelandview;}}


(2) URL mapping configuration for helloworldaction

Create Springmvc-servlet.xml configuration file under the webroot/web-inf/directory with the same XML header information as Spring.xml

Note: The configuration file naming convention: The value of <servlet-name> configured in the Web. xml File-servlet.xml

<servlet-name>-servlet.xml

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/87/61/wKioL1fe5SDTVsoqAABhCEjdpYo051.png "title=" 002. PNG "alt=" Wkiol1fe5sdtvsoqaabhcejdpyo051.png "/>


Springmvc-servlet.xml

<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: Mvc= "Http://www.springframework.org/schema/mvc" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi: schemalocation= "Http://www.springframework.org/schema/beans HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/BEANS/SPR Ing-beans.xsd Http://www.springframework.org/schema/mvc Http://www.springframework.org/schema/mvc/spring-mvc . xsd "> <bean name="/helloworld.action "class=" Com.rk.web.action.HelloWorldAction "></bean></ Beans>

Note the Name property of:<bean> begins with "/", otherwise access is not available.



3.4. Add JSP page

Create a jsp/success.jsp under/webroot/

<%@ page language= "java" pageencoding= "UTF-8"%><!  DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >


650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/87/64/wKiom1fe5BygWtsYAAA0yMi7bNk327.png "title=" 001. PNG "alt=" Wkiom1fe5bygwtsyaaa0ymi7bnk327.png "/>


4. Load the Springmvc.xml configuration file in the custom directory


By default: The configuration file for the SPRINGMVC framework must be called <servlet-name>-servlet.xml and must be placed in the webroot/web-inf/directory.

We can configure an initialization parameter for Dispatcherservlet in the Web. xml file, and let it load the Springmvc-helloworld.xml configuration file under the directory we specify.

(1) If the springmvc-helloworld.xml is placed under the Com.rk.web.action package in the SRC directory

The Web. XML code reads as follows:

<?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 " 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>springmvc01</display-name>  < welcome-file-list>    <welcome-file>index.jsp</welcome-file>   </welcome-file-list>  <!--  Register SPRINGMVC framework Core Controllers  -->  <servlet>   <servlet-name>springmvc</servlet-name>  <servlet-class> org.springframework.web.servlet.dispatcherservlet</servlet-class>  <init-param>   <param-name>contextconfiglocation</param-name>  <param-value>/web-inf/ Classes/com/rk/web/action/springmvc-helloworld.xml</param-value>  </init-param>  </servlet>  <servlet-mapping>  < Servlet-name>springmvc</servlet-name>  <url-pattern>*.action</url-pattern>   </servlet-mapping></web-app>

Where,<param-value> can also be written Classpath: ...

<init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:com/ Rk/web/action/springmvc-helloworld.xml</param-value> </init-param>


(2) If the Springmvc-helloworld.xml configuration file is placed in the SRC directory

The Web. XML code reads as follows:

<?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 " 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>springmvc01</display-name>  < welcome-file-list>    <welcome-file>index.jsp</welcome-file>   </welcome-file-list>  <!--  Register SPRINGMVC framework Core Controllers  -->  <servlet>   <servlet-name>springmvc</servlet-name>  <servlet-class> org.springframework.web.servlet.dispatcherservlet</servlet-class>  <init-param>   <param-name>contextconfiglocation</param-name>  <param-value>classpath: Springmvc-helloworld.xml</param-value>  </init-parAm>  </servlet>  <servlet-mapping>  <servlet-name>springmvc </servlet-name>  <url-pattern>*.action</url-pattern>  </ Servlet-mapping></web-app>





Spring MVC Series: (1) SPRINGMVC Quick Start

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.