Spring MVC Getting Started configuration

Source: Internet
Author: User

1. Introduction to Spring MVC

Spring mvc spring A subproject in the framework for web Application development provides mvc Mode support. The function of spring mvcstruts2 frame, which can be java web programmers liberated from the cumbersome serlvet apistruts2 different, especially after spring 3.1spring mvc widely adopted " specification better than configuration convention-over-configuration

2.Spring MVC the characteristics

    • Controller for the center to complete the control of the system process
    • Collect data from the request
    • Validation of incoming parameters
    • Return the results to the view
    • Provide different solutions for different views
    • providing a tag library for JSP View technology
    • Interception device
    • Uploading files

3.MVC core classes and principles of architecture

    • Dispatchservlet: A central controller that forwards requests to a specific controller class
    • Controller: specific class of controllers
    • Handlermapping: Mapping processor, responsible for mapping the central processing unit to the specific controller class strategy
    • Modelandview: wrapper class for the data and view layers returned by the service layer
    • Viewresolver: View parser, resolving a specific view
    • Inteceptors: Interceptor

is the Spring MVC core component structure and execution process.

3. Build a Spring MVC application

3.1 Importing Spring MVC -related jar packages

(1) Manual import requires the following:

3.2 Configuring the core controller-dispatcherservlet

Configure the Spring MVC core Controller in Web. xml:

<servlet>

<servlet-name>springmvc</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>springmvc</servlet-name>

<url-pattern>/</url-pattern>

</servlet-mapping>

3.3 defining the Spring MVC configuration file

The spring MVC configuration file is a regular spring bean configuration file that simply introduces the Spring-mvc namespace (see the Blue Word section below).

<?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: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.xsd

Http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context.xsd ">

<!-- View Resolver- -

<bean class= "Org.springframework.web.servlet.view.InternalResourceViewResolver" >

<property name= "Viewclass"

value= "Org.springframework.web.servlet.view.JstlView"/>

<!-- prefixes: From the root directory to the View folder --

<property name= "prefix" value= "/web-inf/views/"/>

<!-- View file suffix-- -

<property name= "suffix" value= ". jsp"/>

</bean>

</beans>

3.4 Define a controller class in src homecontroller inherit abstract class abstractcontroller

@Override

Protected Modelandview handlerequestinternal (HttpServletRequest arg0,

httpservletresponse arg1) throws Exception {

System.out.println ("hello,spring mvc ...");

Modelandview mv=new modelandview ("index"); Index: represents the name between the prefix and the suffix

// If the request is placed under admin index.jsp the view name is "Admin/index"

return MV;

}

3.5 Configuring the controller in Springmvc-servlet

<!-- Default mapping processor parsing controller-->

<bean id= "Home" name= "/hello" class= "Com.demo.springmvc.HomeController"/>

where : Name= "/hello" represents the URL requested in the address bar

3.6 Publish and run Http://localhost:8080/mvchello/hello

Note: The mapping processor is not configured here, using the default mapping processor, which can not be configured Org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping

There are other mapping processors such as : org.springframework.web.servlet.handler.SimpleUrlHandlerMapping

Test the work of multiple mapping processors by adding the following configuration to the configuration file Springmvc-servlet

<!-- Default mapping processor- -

<bean class= "org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>

<!-- simple URL mapping processor --

<bean class= "Org.springframework.web.servlet.handler.SimpleUrlHandlerMapping" >

<property name= "Mappings" >

<props>

<prop key= "/hello1" >homeController</prop>

</props>

</property>

</bean>

when using The ttp://localhost:8080/mvchello/hello1 url is accessed using a simple URL Mapping processor

Spring MVC Getting Started configuration

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.