First example of Spring MVC Learning

Source: Internet
Author: User

At the beginning of last year, Spring MVC framework development and management tools were used. However, it takes only half a year to create non-development projects. Now we need to use Spring MVC framework to develop applications. Since the previous application development platform was built by the predecessors, I only need to add my own code to it. That is, I didn't have enough knowledge about the Spring MVC framework. In order to better understand Spring MVC itself, I plan to learn this framework from the very beginning to provide a guarantee for future use.

The Spring versions I use are 3.1.1, JDK1.6, Eclipse 3.4, and Maven 3.0.3. The following describes how to develop a Helloworld program step by step.

1. Create a simple Maven project in Ecplise. the directory structure of the project is as follows:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/141J31C7-0.jpg "title =" maven.jpg "alt =" 224558842.jpg"/>

When a maven project is created, webapp is empty and src/main/java is empty. Remember to select the appropriate Packaging when creating a maven project, and select war instead of the default jar.

2. Create a directory.

3. For the web. xml file, the content is:

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">  <display-name>Mince</display-name>  <context-param>    <param-name>webAppRootKey</param-name>    <param-value>mince.idm</param-value>  </context-param>                                                                                                                                                                                                                                                       <servlet>    <servlet-name>Mince</servlet-name>    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>    <init-param>      <param-name>contextConfigLocation</param-name>      <param-value>/WEB-INF/spring/mince-servlet.xml</param-value>    </init-param>    <load-on-startup>2</load-on-startup>  </servlet>  <servlet-mapping>    <servlet-name>Mince</servlet-name>    <url-pattern>/</url-pattern>  </servlet-mapping>                                                                                                                                                                                                                                                       </web-app>

All requests in Spring MVC must be forwarded by DispatcherServlet, which is the core content of Spring MVC framework.

3. A file/WEB-INF/spring/mince-servlet.xml is specified in web. xml. Therefore, you need to create a new file mince-servlet.xml with the following content:

<?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"        xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"        xsi:schemaLocation="            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd            http://www.springframework.org/schema/mvc  http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">                                                                                                                                                                                                <context:component-scan base-package="com.bizenit.idm.admin.web" />                                                                                                                                                                                        <mvc:annotation-driven/>                                                                                                                                                                                        <mvc:view-controller path="/" view-name="index"/>                                                                                                                                                                                        <!-- Resolves view names to protected .jsp resources within the /WEB-INF/views directory -->    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">        <property name="prefix" value="/WEB-INF/views/"/>        <property name="suffix" value=".jsp"/>    </bean>                                                                                                                                                                                    </beans>

Line 21 indicates the package name of the class to be loaded in the project. Line 23 indicates that the request and Controller are matched using the annotation method. When the user accesses the URL of the projectcom.bizenit.idm.admin.webFind the class with the @ Controller annotation, and match the user's URL to each method in the class. To obtain the required resources. The 25-line configuration indicates that the index page is displayed when the user accesses the root address of the project.

4. Create two simple JSP pages: index. jsp and world. jsp.

5. How to display the world. jsp page? This requires the role of the class HelloWorld. java:

package com.bizenit.idm.admin.web;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;@Controller@RequestMapping(value = "/hello")public class HelloWorld {                                                                                 @RequestMapping(value = "/world",method = RequestMethod.GET)    public String viewWorld(Model model) {        return "hello/world";    }}

6. The index is displayed when the user accesses http: // localhost: 8080/Mince. jsp page. The world is displayed when you access http: // localhost: 8080/Mince/hello/world. jsp page.

7. Another important file, pom. xml, is:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  <modelVersion>4.0.0</modelVersion>  <groupId>Mince</groupId>  <artifactId>Mince</artifactId>  <version>0.0.1-SNAPSHOT</version>  <packaging>war</packaging>                                    <properties>    <webappDirectory>${project.build.directory}/${project.build.finalName}</webappDirectory>    <spring.version>3.1.1.RELEASE</spring.version>    

Each Maven project has a unique pom. xml file. Maven is the first method to simplify the management of jar packages.


Note: I am not familiar with Spring MVC. If there is any discrepancy between the above content, please forgive me and correct me. Thank you.

This article from "Graph learning" blog, please be sure to keep this source http://feiquan16.blog.51cto.com/336861/1304086

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.