"Java EE Learning Day 83rd" "Springmvc"

Source: Internet
Author: User

I. Overview of the SPRINGMVC framework

What is SPRINGMVC? SPRINGMVC is a similar thing to Struts2, their function and nature is almost the same, and even the development efficiency is similar, but in the operational efficiency SPRINGMVC higher than Struts2 Note that the SPRINGMVC here clearly indicates the use of the MVC framework, and STRUTS2 also uses the MVC framework.

1. Environmental preparedness

is almost the same as the spring environment you used earlier, but you need to add two core packages:

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

Org.springframework.web-3.0.0.release.jar

Of course This is before the Spring3.2 environment, Spring3.2 has a major upgrade, no longer use Org.springframework.web.servlet-3.0.0.release.jar, but instead use the following jar package:

Spring-webmvc-3.2.0.release.jar

Of course, it is recommended to use the version of Spring3.2, according to the declaration in the spring official documentation, there are known and obvious bugs in all versions since version 3.0 to version 3.2, and a significant number of bugs have been fixed in spring3.2.

2. List of full jar packages required

Since I am using eclipse, the associated Jstl first-off jar package IDE does not provide support, so you can only add Jstl.jar and Standard.jar manually:

Aopalliance.jar
Commons-logging.jar
Jstl.jar
Spring-aop-3.2.0.release.jar
Spring-aspects-3.2.0.release.jar
Spring-beans-3.2.0.release.jar
Spring-context-3.2.0.release.jar
Spring-context-support-3.2.0.release.jar
Spring-core-3.2.0.release.jar
Spring-expression-3.2.0.release.jar
Spring-web-3.2.0.release.jar
Spring-webmvc-3.2.0.release.jar
Standard.jar

It is clear that another great advantage of using SPRINGMVC is not to introduce a large number of jar packages as in Struts2.

Second, the first SPRINGMVC procedure

1. First configure a servlet in the Web. XML configuration file

<servlet>    <Servlet-name>Action</Servlet-name>    <Servlet-class>Org.springframework.web.servlet.DispatcherServlet</Servlet-class></servlet><servlet-mapping>    <Servlet-name>Action</Servlet-name>    <Url-pattern>*.action</Url-pattern></servlet-mapping>

2. Configure the Spring configuration file

The default servlet will read the corresponding configuration file under the Web-inf folder, and the name of the configuration file also needs to satisfy the rule: $ Servletname-servlet.xml, otherwise the servlet cannot find the corresponding configuration file, and according to the value of the servlet-name tag in the configuration above, we name the configuration file: Action-servlet.xml, the configuration file is actually spring The configuration file

Configure the Internal Resource View resolver first

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"XMLNS:AOP= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"Xmlns:context= "Http://www.springframework.org/schema/context"Xmlns:mvc= "Http://www.springframework.org/schema/mvc"xsi:schemalocation= "Http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.x SD Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2 . xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-con Text-3.2.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP/SPRING-AOP -3.2.xsd ">    <!--Internal Resource View Resolver -    <BeanID= "Internalresourceviewresolver"class= "Org.springframework.web.servlet.view.InternalResourceViewResolver">        < Propertyname= "prefix"value= "/jsps/"></ Property>        < Propertyname= "suffix"value= ". jsp"></ Property>    </Bean><Beans>

The internal view Resource resolver determines a rule that parses the Modelandview object returned by the controller and viewname it out with "prefix" and "suffix" to get the resource location to jump, and to perform a jump. Specifically redirect or forward, it needs to be determined according to the content of Modelandview.

This assumes that all JSP pages are saved to the/jsps directory and end with a. jsp.

3. New Controller

The superclass of all controllers is Abstractcontroller, which inherits the class directly, and overrides the core method of the class: Handlerequestinternal, the method return value is the Modelandview object, The object must be given a string that represents the location of the resource.

 PackageCom.kdyzm.controller;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;ImportOrg.springframework.web.servlet.ModelAndView;ImportOrg.springframework.web.servlet.mvc.AbstractController; Public classHomeControllerextendsAbstractcontroller {@OverrideprotectedModelandview handlerequestinternal (httpservletrequest request, httpservletresponse response)throwsException {System.out.println ("Hello, this is the first SPRINGMVC program!" "); return new Modelandview ("index"); }}

When the index is parsed by the internal view resource resolver, it turns into a real address:/jsps/index. JSP, you need to use http://localhost:8080/on your browser The project name/jsps/index.jsp to access.

The controller needs to be injected into spring container management after the new controller is created.

<name= "/home.action"  class= "Com.kdyzm.controller.HomeController "></bean>

4. Create a new JSP page under the/jsp/folder index.jsp

1 <%@ Page Language="Java"pageencoding="Utf-8"%>2 <!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd ">3 <HTML>4 <Head>5 <Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8">6 <title>Insert Title here</title>7 </Head>8 <Body>9 Hello, this is the first SPRINGMVC program! Ten </Body> One </HTML>

5. Enter http://localhost:8080/springmvc/home.action on the browser to test

  

The test was successful.

Three or three different URL processor mappings

The so-called processor map is a map object that stores the requested URL-to-controller mapping, which is able to pass our request to a specified controller for processing.

1.Bean Name URL Processor mapping

This is the default URL processor mapping, and is also the most commonly used URL processor mapping, the so-called default is not configured will automatically load, here explicitly declare the meaning of:

< Bean     class = "Org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" >    <  name= "Order"  value= "3"></Property  ></beans>

It has a Serorder method that determines the priority of the URL processor, and if multiple URL processor mappings are configured, the larger the value is the more limited the match; The previous example is using the default URL processor mapping because there is no explicit configuration of the URL processor mappings.

2. Simple URL Processor mapping

This kind of URL-processor mapping is basically no use, as it is necessary to enumerate the keys and values in this way, which is unbearable even in a medium-sized project.

  

"Java EE Learning Day 83rd" "Springmvc"

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.