Spring MVC 3 is the simplest example.

Source: Internet
Author: User

Spring MVC 3 is the simplest example.

I recently reviewed springMVC and tried to find another demo online. It seems that domestic demos are too complicated. Later, I found a simple spring mvc demo on a foreign website. Here I will record it and give it to anyone who needs it.

Step 1: Prepare the package:

Log packages

Jcl-over-slf4j-1.6.1.jar
Logback-classic-0.9.29.jar
Logback-core-0.9.29.jar
Slf4j-api-1.6.1.jar

Jstl package

Jstl-1.2.jar

Spring packages
Org. springframework. asm-3.1.1.RELEASE.jar
Org. springframework. beans-3.1.1.RELEASE.jar
Org. springframework. context-3.1.1.RELEASE.jar
Org. springframework. core-3.1.1.RELEASE.jar
Org. springframework. expression-3.1.1.RELEASE.jar
Org. springframework. web-3.1.1.RELEASE.jar

The previous version seems to be called the springmvc package. Now it is changed to the servlet package.
Org. springframework. web. servlet-3.1.1.RELEASE.jar

Step 2:

Create a Dynamic Web project in the eclipse project, perform wizard-based development, and click next To Get A web project.

Generate an index. jsp file under the WebContent directory

?
1 2 3 4 5 6 7 8 9 10 11 <%@ page language=java contentType=text/html; charset=ISO-8859-1 pageEncoding=ISO-8859-1%> <html> <head> <title>Spring 3.0 MVC demo title> head> <body> <a href=hello.html>Say Hello a> body> html>

Generate a jsp folder and two configuration files under the WebContentWEB-INF Directory: spring-servlet.xml, web. xml

The content of web. xml is

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 19 20 21 22 xml version=1.0 encoding=UTF-8?> <web-app version=2.5 xmlns=http://java.sun.com/xml/ns/javaee xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xsi:schemaLocation=http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd> <display-name>Spring3MVC display-name> <servlet> <servlet-name>spring servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet servlet-class> <load-on-startup>1 load-on-startup> servlet> <servlet-mapping> <servlet-name>spring servlet-name> <url-pattern>*.html url-pattern> servlet-mapping> <welcome-file-list> <welcome-file>index.jsp welcome-file> welcome-file-list> web-app>

The spring-servlet.xml content is

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 19 20 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 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> <context:component-scan base-package=net.spring.controller /> <bean id=viewResolver class=org.springframework.web.servlet.view.UrlBasedViewResolver> <property name=viewClass value=org.springframework.web.servlet.view.JstlView /> <property name=prefix value=/WEB-INF/jsp/ /> <property name=suffix value=.jsp /> bean> beans>

Then generate the java file HelloWorldController. the java package path is net. spring. controller.

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 package net.spring.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; @Controller public class HelloWorldController { @RequestMapping(/hello) public ModelAndView helloWorld() { String message = Hello World, Spring 3.0!; System.out.println(message); return new ModelAndView(hello, message, message); } }

At this point, all the code and configurations have been completed. All the documents and configurations are added in about 60 lines, which is relatively simple.

After that, you can run it in tomcat and other containers, and then open the address in the web.

 

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.