Home> Java> javaTutorial> body text

SpringMVC Learning Series (2) Classic HelloWorld Implementation

黄舟
Release: 2017-03-03 10:36:32
Original
1361 people have browsed it

The previous article briefly introduced some knowledge of Spring MVC. Next, we will start to learn how to apply Spring MVC to specific projects.

First let’s start with a simple Hello World project:

The development environment of my machine is:

UbuntuSpringMVC Learning Series (SpringMVC Learning Series (2) Classic HelloWorld Implementation) Classic HelloWorld ImplementationSpringMVC Learning Series (2) Classic HelloWorld Implementation.04 ( Different operating systems have no impact on this series of projects);

Development tool: Eclipse For JavaEE;

Database: MySql5.5.35;

Running environment: TomCat V7.0;

JDK: JDK SpringMVC Learning Series (SpringMVC Learning Series (2) Classic HelloWorld Implementation) Classic HelloWorld Implementation.7.0_45;

Project Engineering For: Dynamic Web Project;

SpringMVC Learning Series (SpringMVC Learning Series (2) Classic HelloWorld Implementation) Classic HelloWorld Implementation. The jar package the project depends on:

SpringMVC Learning Series (SpringMVC Learning Series (2) Classic HelloWorld Implementation) Classic HelloWorld Implementation. The jar package the Spring framework depends on:

Log: commons-logging-SpringMVC Learning Series (SpringMVC Learning Series (2) Classic HelloWorld Implementation) Classic HelloWorld Implementation.SpringMVC Learning Series (SpringMVC Learning Series (2) Classic HelloWorld Implementation) Classic HelloWorld Implementation.3.jar;

JSTL support: jstl.jar and standard.jar in jakarta-taglibs-standard-SpringMVC Learning Series (SpringMVC Learning Series (2) Classic HelloWorld Implementation) Classic HelloWorld Implementation.SpringMVC Learning Series (SpringMVC Learning Series (2) Classic HelloWorld Implementation) Classic HelloWorld Implementation.SpringMVC Learning Series (2) Classic HelloWorld Implementation;

SpringMVC Learning Series (2) Classic HelloWorld Implementation.Spring’s jar package:

spring-framework-3.SpringMVC Learning Series (2) Classic HelloWorld Implementation.5.jar package in RELEASE/libs (I copied everything here for convenience);

Copy all the above jar packages to the WebContent/WEB-INF/lib directory of the project.

SpringMVC Learning Series (2) Classic HelloWorld Implementation. Add the web.xml file in /WEB-INF. The content of the file is as follows:

 SpringMVCLesson  SpringMVCLesson org.springframework.web.servlet.DispatcherServlet  contextConfigLocation classpath:springservlet-config.xml  SpringMVC Learning Series (SpringMVC Learning Series (2) Classic HelloWorld Implementation) Classic HelloWorld Implementation    SpringMVCLesson /  
Copy after login


First, configure the DispatcherServlet , According to the Spring MVC response flow chart of series (SpringMVC Learning Series (SpringMVC Learning Series (2) Classic HelloWorld Implementation) Classic HelloWorld Implementation), it can be seen that DispatcherServlet mainly intercepts requests, and then calls the corresponding Controller and Action, parses and renders the specified view and returns the response.

Classpath:springservlet-config.xml specifies the specific configuration file as springservlet-config.xml.

load-on-startup>SpringMVC Learning Series (SpringMVC Learning Series (2) Classic HelloWorld Implementation) Classic HelloWorld Implementationload-on-startup>是启动顺序,让这个Servlet随Servletp容器一起启动,必须放在servlet>配置的最后。

servlet-mapping中的servlet-name>指定配置的是哪个servleturl-pattern>则指定拦截哪些请求到该servlet,这里配置的是拦截全部请求

三、springservlet-config.xml文件配置:

在项目中新建一个resources的Source Folder文件夹,并添加springservlet-config.xml文件。

                         
Copy after login


mvc:annotation-driven/>开启注解映射支持,它是为了简化配置的缩写形式,它相当于以下SpringMVC Learning Series (2) Classic HelloWorld Implementation个配置:

 
Copy after login

由于我们在web.xml文件里面配置的是拦截所有的请求到该servlet,所以我们在这里要对静态资源文件映射作一下配置,否则请求这些资源文件会返回404

Copy after login


开启Controller注解支持,并配置只扫描指定包下面的Controller:

  
Copy after login

配置视图解析器,并指定视图所在的文件夹:

    
Copy after login

添加HelloWorldController,在项目中新建一个web的Source Folder文件夹,并在文件夹下面添加com.demo.web.controllers包,在包中添加一个HelloWorldController类,类中内容如下:


package com.demo.web.controllers;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.servlet.ModelAndView; @Controller @RequestMapping(value = "/helloworld")public class HelloWorldController { @RequestMapping(value="/index", method = {RequestMethod.GET}) public ModelAndView index(){ ModelAndView modelAndView = new ModelAndView(); modelAndView.addObject("message", "Hello World!"); modelAndView.setViewName("index"); return modelAndView; } }
Copy after login


其中@Controller 注解把该class指定为controller,controller 上的@RequestMapping 注解的 value值指定该controller所映射的请求。

方法上的@RequestMapping 注解指定该方法为一个action,value 值指定该action所映射的请求,method 中的RequestMethod.GET指定该action只接受get请求。

ModelAndView 中的setViewName指定了该action所对应的视图名称,解析视图时会在springservlet-config.xml文件指定的视图文件夹中寻找对应的视图。

添加视图,在项目/WEB-INF文件夹下新建一个views文件夹,并在views中添加index.jsp视图,视图内容如下:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>Insert title here
${message}
Copy after login


运行项目,由于我们之前配置了:mvc:view-controllerpath="/"view-name="forward:/helloworld/index"/>所以直接可以看到结果:

SpringMVC Learning Series (SpringMVC Learning Series (2) Classic HelloWorld Implementation) Classic HelloWorld Implementation

把请求的URL地址改为配置的地址:http://localhost:8080/SpringMVCLesson/helloworld/index,可以看到结果相同:

SpringMVC Learning Series (2) Classic HelloWorld Implementation

代码下载:http://pan.baidu.com/s/SpringMVC Learning Series (SpringMVC Learning Series (2) Classic HelloWorld Implementation) Classic HelloWorld Implementationo6LRw7o

以上就是SpringMVC学习系列(SpringMVC Learning Series (2) Classic HelloWorld Implementation) 之 经典的HelloWorld实现的内容,更多相关内容请关注PHP中文网(m.sbmmt.com)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!