SPRING3 MVC Primer Sample

Source: Internet
Author: User

Spring3 MVC Introduction:

1. Spring MVC is a Web component of the spring framework that can develop Web engineering

2. Able to integrate well with other frameworks (Struts2)

3. Spring MVC is a servlet-centric, distributed request to the controller via Dispatcherservlet

4. Dispatcherservlet is a fully integrated spring IOC container that can use other spring features

5. Spring3 MVC Support Annotation Technology

6. Spring3 can support JAP2.0 very well.

Then let's write an example:

1. Prepare the required tools and jar packages:

(1). JDK 1.7

(2). apache-tomcat-7.0.52

(3). Eclipse-javaee version of

(4). Jar Required:

Commons-logging-1.1.1.jar

Jstl-1.2.jar

Org.springframework.asm-3.0.0.release.jar

Org.springframework.beans-3.0.0.release.jar

Org.springframework.context-3.0.0.release.jar

Org.springframework.core-3.0.0.release.jar

Org.springframework.expression-3.0.0.release.jar

Org.springframework.web-3.0.0.release.jar

Org.springframework.web.servlet-3.0.0.release.jar

Jar:http://pan.baidu.com/s/1eqvxlom

2. Create a Dynamic Web project, and select the server, select the version of the servlet (2.5)

3. Copy the required jar to the webcontent---web-inf> lib folder

4.Spring Controller Class

Create a control class for Spring MVC and process the request, print a word ' Spring MVC example ', then we create the package Com.li.controller and then create a class Hellocontroller.java underneath the package, Add code to this class. Specify @controller and @requestmapping ("/test") in the Hellocontroller class. @Controller: When spring scans the package, it is represented as a bean that processes the request. @RequestMapping ("/test"): The request URL address should be processed

1  PackageCom.li.controller;2 3 ImportOrg.springframework.stereotype.Controller;4 Importorg.springframework.web.bind.annotation.RequestMapping;5 ImportOrg.springframework.web.servlet.ModelAndView;6 7 @Controller8  Public classHellocontroller {9     Ten@RequestMapping ("/test") One      PublicModelandview Test () { AString str= "Spring MVC Sample"; -         return NewModelandview ("message", "str", str); -     } the}
Hellocontroller.java

5. Create a JSP

Create a JSP make a request: index.jsp

Create a JSP display message: message.jsp

Send a request to hellocontroller with a hyperlink inside the index.jsp, and return to message.jsp to display STR's information

1 <%@ Page Language="Java"ContentType="text/html; Charset=utf-8"2 pageencoding="UTF-8"%>3 <!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd ">4 <HTML>5 <Head>6 <Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8">7 <title>Insert Title here</title>8 </Head>9 <Body>Ten <ahref= "Test.do">Spring MVC Example</a> One </Body> A </HTML>
index.jsp
1 <%@ Page Language="Java"ContentType="text/html; Charset=utf-8"2 pageencoding="UTF-8"%>3 <!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd ">4 <HTML>5 <Head>6 <Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8">7 <title>Insert Title here</title>8 </Head>9 <Body>Ten ${str} One </Body> A </HTML>
message.jsp

6.Spring MVC mapping in Web. xml

Main definition: Org.springframework.web.servlet.DispatcherServlet

You need to add the following configuration to the Web. XML configuration file in the project:

1   <servlet>2       <Servlet-name>Spring</Servlet-name>3       <Servlet-class>4 Org.springframework.web.servlet.DispatcherServlet5       </Servlet-class>6       <Load-on-startup>1</Load-on-startup>7   </servlet>8   <servlet-mapping>9       <Servlet-name>Spring</Servlet-name>Ten       <Url-pattern>/</Url-pattern> One   </servlet-mapping>
View Code

7.Spring configuration file

Note the name of the servlet in the previous step of the <servlet-name> tag in Web. Xml.

After initialization of the Dispatcherservlet, a file name is found in Web-inf [servlet-name]-servlet.xml

In this example, you should find the spring-servlet.xml

Create a spring configuration file under Web-inf, with the file name: Spring-servlet.xml, and copy the following to the file:

1 <?XML version= "1.0" encoding= "UTF-8"?>2 <Beansxmlns= "Http://www.springframework.org/schema/beans"3 Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"XMLNS:AOP= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"4 Xmlns:tx= "Http://www.springframework.org/schema/tx"Xmlns:context= "Http://www.springframework.org/schema/context"5 xsi:schemalocation=" 6 Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-3.0.xsd7 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd8 Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/ Spring-context-3.0.xsd9 HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop-3.0.xsd " Ten Default-autowire= "ByName"> One     <Context:component-scanBase-package= "Com.li.controller"/> A     <BeanID= "Viewresolver" - class= "Org.springframework.web.servlet.view.UrlBasedViewResolver"> -         < Propertyname= "Viewclass" the value= "Org.springframework.web.servlet.view.JstlView"/> -         < Propertyname= "prefix"value="/"/> -         < Propertyname= "suffix"value= ". jsp"/> -     </Bean> + </Beans>
Spring-servlet.xml

SOURCE Download: Http://pan.baidu.com/s/1bnwJs8R

Original link: http://jingyan.baidu.com/article/c843ea0b7f8b7777931e4ae8.html

SPRING3 MVC Primer Sample

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.