我机器的开发环境为:
开发工具:Eclipse For JavaEE;
数据库:MySql5.5.35;
运行环境:TomCat V7.0;
JDK:JDK 1.7.0_45;
项目工程为:Dynamic Web Project;
一、项目依赖的jar包:
[html] view plain copy print ?
- <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/maven-v4_0_0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <groupId>com.somnus</groupId>
- <artifactId>SpringMVC</artifactId>
- <packaging>war</packaging>
- <version>0.0.1-SNAPSHOT</version>
- <name>SpringMVC Maven Webapp</name>
- <url>http://maven.apache.org</url>
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- </properties>
- <!-- maven依赖包的书写是有顺序的,请不要更改下面dependency的顺序,避免造成JAR包冲突 -->
- <dependencies>
- <!-- servlet & jsp -->
- <dependency>
- <groupId>javax.servlet</groupId>
- <artifactId>servlet-api</artifactId>
- <version>2.5</version>
- <scope>provided</scope>
- </dependency>
- <dependency>
- <groupId>javax.servlet</groupId>
- <artifactId>jstl</artifactId>
- <version>1.2</version>
- </dependency>
- <dependency>
- <groupId>javax.servlet.jsp</groupId>
- <artifactId>jsp-api</artifactId>
- <version>2.2</version>
- <scope>provided</scope>
- </dependency>
- <!-- 加入springmvc依赖包 -->
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-webmvc</artifactId>
- <version>3.2.5.RELEASE</version>
- </dependency>
- <!-- 加入fileupload依赖包 -->
- <dependency>
- <groupId>commons-fileupload</groupId>
- <artifactId>commons-fileupload</artifactId>
- <version>1.3.1</version>
- </dependency>
- <!-- .jackson -->
- <dependency>
- <groupId>org.codehaus.jackson</groupId>
- <artifactId>jackson-core-asl</artifactId>
- <version>1.9.12</version>
- </dependency>
- <dependency>
- <groupId>org.codehaus.jackson</groupId>
- <artifactId>jackson-mapper-asl</artifactId>
- <version>1.9.12</version>
- </dependency>
- <!-- bean validator -->
- <dependency>
- <groupId>javax.validation</groupId>
- <artifactId>validation-api</artifactId>
- <version>1.1.0.Final</version>
- </dependency>
- <dependency>
- <groupId>org.hibernate</groupId>
- <artifactId>hibernate-validator</artifactId>
- <version>4.3.2.Final</version>
- </dependency>
- </dependencies>
- </project>
二、在/WEB-INF中添加web.xml文件,文件内容如下:
<?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_3_0.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<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>1</load-on-startup><!-- load-on-startup必须放在最后 -->
</servlet>
<!-- Spring MVC配置文件结束 -->
<servlet-mapping>
<servlet-name>SpringMVCLesson</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
首先是配置DispatcherServlet,根据系列(1)的Spring MVC响应流程图,可以看出DispatcherServlet主要就是拦截请求,然后调用对应的Controller和Action,解析和渲染指定的视图并返回响应 。
其中classpath:springservlet-config.xml指定具体的配置文件为springservlet-config.xml。
<load-on-startup>1</load-on-startup>是启动顺序,让这个Servlet随Servletp容器一起启动,必须放在<servlet>
servlet-mapping中的<servlet-name>指定配置的是哪个servlet,<url-pattern>则指定拦截哪些请求到该servlet,这里配置的是拦截全部请求。
三、springservlet-config.xml文件配置:
在项目中新建一个resources的Source Folder文件夹,并添加springservlet-config.xml文件。
<?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:util="http://www.springframework.org/schema/util"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd" >
<!-- 默认的注解映射的支持 -->
<mvc:annotation-driven/>
<!-- 如果当前请求为“/”时,则转发到“/helloworld/index” -->
<mvc:view-controller path="/" view-name="forward:/helloworld/index"/>
<!-- 静态资源映射 -->
<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注解支持 -->
<!-- use-default-filters="false" 只扫描指定的注解 -->
<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>
</beans>
<mvc:annotation-driven/>
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>
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.01 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,可以看到结果相同:
标签:www,http,SpringMVC,springframework,实现,HelloWorld,org,servlet,schema From: https://blog.51cto.com/u_15912341/5937187