Springmvc Simple starter Source jar package download and file upload

Source: Internet
Author: User

Introduction to 1.Spring MVC


the project source code download, SPRINGMVC and spring jar package, please go to manyjar.com download. Download Mode 1: Click Download download mode 2: Baidu search Manyjar (Manyjar official website), search springmvcunit for download

Spring MVC is a lightweight web framework that implements the request-driven type of web MVC design Patterns in Java, decoupling the web layer from responsibility and simplifying web development.

The SPRINGMVC framework separates data, business, and presentation by implementing the Model-view-controller model.

Spring MVC and struts, Struts2-like frameworks.

Springmvc and the natural and spring frameworks can be seamlessly integrated (IOC container, AOP container).

Spring MVC is designed around Dispatcherservlet, and Dispatcherservlet is responsible for distributing requests to specific handler. With configurable handler mappings, view resolution, locale, and theme resolution to process requests and go to the corresponding views, it provides a powerful convention that is larger than the configured contractual programming.

The following standard architecture for Web MVC


The overall flow of Spring MVC request processing



2 Introduction to SPRINNGMVC development

the project source code download, SPRINGMVC and spring jar package, please go to manyjar.com download. Download Mode 1: Click Download download mode 2: Baidu search Manyjar (Manyjar official website), search springmvcunit for download

A controller class is defined in 2.1 spring3.x and must be marked with a @controller annotation.

2.2 Use the @requestmapping annotation to map the method to a number of requests so that the method processes those requests.

2.3 Views can get the displayed data. The method parameters are also available for the view. For example, if the processing method takes a map parameter, the view can get the value of the map by key.


3 Springmvc version of HelloWorld

the project source code download, SPRINGMVC and spring jar package, please go to manyjar.com download. Download Mode 1: Click Download download mode 2: Baidu search Manyjar (Manyjar official website), search springmvcunit for download

Next we use spring MVC to develop a most simple Web application. First create a dynamic Web Project.

I put all the jar packages under the Spring dist directory into the Web-inf/lib directory.

3.1 Springmvc and spring jar packages, www.manyjar.com or search Springmvcunit

3.2 Add the following fragment to the Web. xml file:

The configuration file for configuration Dispatcherservlet,springmvc is named: spring-mvc.xml



Configure the contextloaderlistener,spring configuration name for spring: Beans.xml


3.3 Develop a simple controller class Hellocontroller:

Package Springmvc.controller; Import Org.springframework.stereotype.controller;import Org.springframework.ui.model;import Org.springframework.web.bind.annotation.requestmapping;import Org.springframework.web.bind.annotation.RequestMethod; @Controllerpublic class Hellocontroller {         @RequestMapping (value= "/hello", Method=requestmethod.get)    public String SayHello (         model model,         map<object, object> resultmap,         @RequestParam (value= "SayHello", Required=false) String  sayHello) {          Model.addattribute ("msg", "Hello, world!");         Resultmap.put ("MyKey", "This was from  Resultmap");         The front end is passed over         System.out.println (SayHello);         return "Hello";    }}

First, the @controller annotation indicates that the class is a controller, and then, in this case, the SayHello method handles only the get type of/hello request by @requestmapping annotations for the method SayHello which requests are processed.

The SayHello method receives a parameter of type Org.springframework.ui.Model MODEL,SPRINGMVC automatically encapsulates the request parameter into the model, and we can simply interpret the model as a map. We take the value of the person from the model in the method and print it, and then add an attribute msg to the model with a value of "hello,world! ", and then return the view name hello.

Next we need to configure a view resolver in the Spring MVC configuration file, configured in Spring-mvc.xml

<?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:mvc= "Http://www.springframework.org/schema/mvc" xsi: schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-3.0.xsd Http://www.springframework.org/schema/context Http://www.springframework.org/schema/context /spring-context-3.0.xsd Http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/ Spring-mvc-3.0.xsd > <!--default annotation mappings Support--<mvc:annotation-driven/> <!--enable auto-scan and <c Ontext:component-scan base-package= "Springmvc.controller"/> <!--internal Resource View resolver prefix + logicname + suffix/web- inf/jsps/+ index +. JSP--<bean id= "INTERNALRESOURCEVIEWREsolver "class=" Org.springframework.web.servlet.view.InternalResourceViewResolver "> <!--prefix-- <property name= "prefix" value= "/web-inf/jsps/"/> <!--suffix--<property name= "suffix" value= ". jsp"/> </bean></beans>



There's nothing to say, but notice that the MVC namespace is added, followed by spring automatic scanning, and the default annotation mapping support is set. The key here is to explain the bean in the configuration file. Its type is the most commonly used view resolver in spring MVC, and of course there are many other types, as this blog focuses on a simple introduction to spring MVC, so it is not highlighted, and subsequent posts will be added. The prefix property refers to the view prefix, suffix is the view suffix, which is configured in. JSP, we return in the controller's method SayHello is Hello, and then combined with the configuration here, the corresponding complete view is:/web-inf/jsp/hello.jsp. Next we complete this view, we simply take out the information we put msg:

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "    pageencoding=" UTF-8 "%><! DOCTYPE html>

Next we deploy the app, and we visit Http://localhost:8080/springmvcnew/hello?sayHello=helloController to display the contents of the view, and the contents of the MSG are removed:

Console output: Hellocontroller

Browser output:



4 SPRINGMVC Image upload

the project source code download, SPRINGMVC and spring jar package, please go to manyjar.com download. Download Mode 1: Click Download download mode 2: Baidu search Manyjar (Manyjar official website), search springmvcunit for download

To configure the Commonsmultipartresolver processing class, the contents of the Spring-mvc.xml file are as follows:


Writing the Controller Personcontroller.java


Write Page jpersoncreate.jsp



Next we deploy the app, and we visit http://localhost:8080/springmvcnew/tocreate to access the Add User Information page.


The browser shows the following



At this point, Springmvc's simple project is done. Basic Simple SPRINGMVC Development Introduction! Hope to learn progress together ...


Refer to the Blog Park http://www.cnblogs.com/wawlian/archive/2012/11/17/2775435.html



Springmvc Simple starter Source jar package download and file upload

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.