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;
spring-framework-3.SpringMVC Learning Series (2) Classic HelloWorld Implementation.5.jar package in RELEASE/libs (I copied everything here for convenience);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:
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:
<?xml version="SpringMVC Learning Series (SpringMVC Learning Series (2) Classic HelloWorld Implementation) Classic HelloWorld Implementation.0" encoding="UTF-8"?><web-app learning series classic helloworld implementation00springmvc implementation> <display-name>SpringMVCLesson</display-name> <servlet> <servlet-name>SpringMVCLesson</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springservlet-config.xml</param-value> </init-param> <load-on-startup>SpringMVC Learning Series (SpringMVC Learning Series (2) Classic HelloWorld Implementation) Classic HelloWorld Implementation</load-on-startup><!-- load-on-startup必须放在最后 --> </servlet> <!-- Spring MVC配置文件结束 --> <servlet-mapping> <servlet-name>SpringMVCLesson</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
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>指定配置的是哪个servlet,url-pattern>则指定拦截哪些请求到该servlet,这里配置的是拦截全部请求。
三、springservlet-config.xml文件配置:
在项目中新建一个resources的Source Folder文件夹,并添加springservlet-config.xml文件。
<?xml version="SpringMVC Learning Series (SpringMVC Learning Series (2) Classic HelloWorld Implementation) Classic HelloWorld Implementation.0" encoding="UTF-8"?><beans learning series classic helloworld implementation00springmvc implementation> <!-- 默认的注解映射的支持 --> <annotation-driven></annotation-driven> <!-- 如果当前请求为“/”时,则转发到“/helloworld/index” --> <view-controller></view-controller> <!-- 静态资源映射 --> <resources></resources> <resources></resources> <resources></resources> <resources></resources> <resources></resources> <!-- 当上面要访问的静态资源不包括在上面的配置中时,则根据此配置来访问 --> <default-servlet-handler></default-servlet-handler> <!-- 开启controller注解支持 --> <!-- use-default-filters="false" 只扫描指定的注解 --> <component-scan> <include-filter></include-filter> </component-scan> <!-- 视图解析器 --> <bean> <property></property> <property></property> <property></property> <property></property> </bean> </beans>
mvc:annotation-driven/> 开启注解映射支持,它是为了简化配置的缩写形式,它相当于以下SpringMVC Learning Series (2) Classic HelloWorld Implementation个配置:
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>
由于我们在web.xml文件里面配置的是拦截所有的请求到该servlet,所以我们在这里要对静态资源文件映射作一下配置,否则请求这些资源文件会返回404:
<!-- 静态资源映射 --><mvc:resources mapping="/js/**" location="/WEB-INF/js/" /><mvc:resources mapping="/css/**" location="/WEB-INF/css/" /><mvc:resources mapping="/fonts/**" location="/WEB-INF/fonts/" /><mvc:resources mapping="/plugins/**" location="/WEB-INF/plugins/" /><mvc:resources mapping="images/**" location="/WEB-INF/images/" /><!-- 当上面要访问的静态资源不包括在上面的配置中时,则根据此配置来访问 --><mvc:default-servlet-handler/>
开启Controller注解支持,并配置只扫描指定包下面的Controller:
<context:component-scan base-package="com.demo.web.controllers" use-default-filters="false"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan>
配置视图解析器,并指定视图所在的文件夹:
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <property name="contentType" value="text/html"/> <property name="prefix" value="/WEB-INF/views/"/> <property name="suffix" value=".jsp"/></bean>
添加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; } }
其中@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"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0SpringMVC Learning Series (SpringMVC Learning Series (2) Classic HelloWorld Implementation) Classic HelloWorld Implementation Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body> ${message}</body></html>
运行项目,由于我们之前配置了:mvc:view-controller path="/" view-name="forward:/helloworld/index"/> 所以直接可以看到结果:
把请求的URL地址改为配置的地址:http://localhost:8080/SpringMVCLesson/helloworld/index,可以看到结果相同:
以上就是SpringMVC学习系列(SpringMVC Learning Series (2) Classic HelloWorld Implementation) 之 经典的HelloWorld实现的内容,更多相关内容请关注PHP中文网(www.php.cn)!