Spring Learning's first spring MVC program (IDEA development environment)

Source: Internet
Author: User

Looking back on the Web development process on the Java platform, from the beginning of the servlet, to the JSP flourish, then the servlet+jsp era, and finally evolve into the era of web development framework prevails. General access to a new web framework, would like to ask the advantages of this framework where? Or better than other frameworks? The conversion framework might not be a good idea if you are not using the Spring MVC framework and you are using a different framework and are well satisfied with the requirements. Spring MVC is also a priority if I get into the Web development framework for the first time as I do.

    • Web tier, or, more specifically, in the framework processor, Spring MVC makes a reasonable and complete separation of the various concerns involved during request processing, and explicitly sets the response role to model and process the various concerns throughout the declaration cycle. These include: Handlermapping is used to handle the mapping relationship between Web requests and specific request processing controllers; Localeresolver is used for internationalization processing; Viewresolver is used for flexible view selection.
    • From the presentation layer, Spring MVC uses a logical named View strategy, by introducing viewresolver and view, clearly separating the selection of the view strategy and the coupling between rendering and the specific controller, suitable for various view technologies can easily be integrated into Spring MVC, whether jsp/ Jstl as a view technique, or as a velocity/freemarker, or even a binary format view of Pdf/excel, uses them in a simple configuration.
    • Spring MVC has another feature, the "Guru-famous door," as a member of the spring family, is easily supported by other brothers in the family, from IOC to AOP support, and the data access layer, the support of the transaction management layer, and so on. Spring MVC is a request-driven web framework that uses a single servlet as the front controller for the entire application, which, after receiving a specific Web request, will refer to the mapping of the settings and forward the pending Web request to the controller at the secondary level for processing.
Building the Spring MVC Project

LZ's development environment is idea, these days using the idea of the feeling, the idea of the overall interface than eclipse refreshing, open and close much faster, but good multi-functional still do not know where, woo-woo ...

1 Open idea, new project

2 project selection is shown below

Of course, the new project can also choose Spring–spring MVC (here to choose the Web application)

3 The project is named Mvcdemo, as shown below, then click Finash

4 After the new project is completed, you need to manually add the associated jar package (LZ does not use Maven, why, because it is not yet ...) ), right-click on the open Module Settings to add the corresponding jar package to the mouse mobile project name

5 The final project is as follows, for convenience, all spring jar packages are added, as well as the commons-logging package.

6 Configuring the Web. xml file and the Mvcdemo-servlet.xml file

<?XML version= "1.0" encoding= "UTF-8"?><Web-appxmlns= "Http://xmlns.jcp.org/xml/ns/javaee"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "Http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"version= "3.1">    <Display-name>Spring MVC</Display-name>    <servlet>        <Servlet-name>Mvcdemo</Servlet-name>        <Servlet-class>Org.springframework.web.servlet.DispatcherServlet</Servlet-class>        <Load-on-startup>1</Load-on-startup>    </servlet>    <servlet-mapping>        <Servlet-name>Mvcdemo</Servlet-name>        <Url-pattern>/</Url-pattern>    </servlet-mapping></Web-app>

The above is the Web. xml file, Dispatcherservlet as the front Controller for the entire application, which handles all requests, and does not get the support for the URL mapping matching of the website container as a "one Web request corresponds to a servlet". Instead, we have to deal with the mapping between specific Web requests and specific processing classes, which requires the help of controllerbeannamehandlermapping.

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" >    <!--handlermapping Find the corresponding controller according to Benaname -    <Beanclass= "Org.springframework.web.servlet.mvc.support.ControllerBeanNameHandlerMapping"/>    <!--Controller Configuration Processor -    <Beanname= "/hello"class= "Com.luoxn28.hello.HelloController"/>    <!--viewresolver View Parser -    <Beanclass= "Org.springframework.web.servlet.view.InternalResourceViewResolver">        <!--Prefixes and suffixes -        < Propertyname= "prefix"value= "/web-inf/"/>        < Propertyname= "suffix"value= ". jsp"/>    </Bean></Beans>

Controllerbeannamehandlermapping is used to handle the mapping between specific Web requests and specific processing classes. The controller (Hellocontroller is the controller's implementation Class) is also a specific processing class. Internalresourceviewresolver is configured for view resolution related information.

7 Create a new Com.luoxn28.hello package under the SRC folder, and then create a new Hellocontroller class under the package.

 PackageCom.luoxn28.hello;ImportOrg.springframework.web.servlet.ModelAndView;ImportOrg.springframework.web.servlet.mvc.AbstractController;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse; Public classHellocontrollerextendsAbstractcontroller {@OverrideprotectedModelandview handlerequestinternal (httpservletrequest request, httpservletresponse response) {String Hello 
    = Request.getparameter ("Hello"); System.out.println ("Hellocontroller:" +hello); Modelandview Mav=NewModelandview ("Hello"); Mav.addobject ("Hello", hello); returnMav; }}

8 Create a new hello.jsp file under the Web-inf folder with the following contents:

<%@ page contenttype= "Text/html;charset=utf-8" language= "java"%>    Spring MVC<br/>    ${hello} </body>

9 Change the index.jsp file as follows:

<%@ PageContentType= "Text/html;charset=utf-8"language= "Java" %><HTML>  <Head>    <title>$Title $</title>  </Head>  <Body>    <formAction= "Hello"Method= "POST">Hello:<inputtype= "text"name= "Hello"/>      <inputtype= "Submit"value= "Submit"/>    </form>  </Body></HTML>

10 at this point, the entire project was established, now the Project view is as follows:

Then mouse move project name right click on open Module Settings, configure, add spring under Modules, select Spring Application The context is web-inf under Mvc-demo-servlet.xml, as shown in:

Click Artifacts, below is the message, click Fix ... button, as shown below:

After everything is set up, click on the Start button and the display screen looks like this:

Simply enter a string and click Submit, such as enter "Luoxn28", and finally jump to the following interface, to the end of the project.

Resources

1. Spring mvc from Getting started to mastering video tutorials

2. Spring Revelation Spring MVC Chapter

Spring Learning's first spring MVC program (IDEA development environment)

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.