下面是创建springmvc简单案例的步骤:
项目的目录结构如下:
此案例使用maven构建。
第一步:创建动态web工程,然后项目->configure->convert to maven project,将web项目转化成maven项目
也可以直接创建maven项目
第二步:引入jar包
pom.xml
<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.0modelVersion> <groupId>springmvcgroupId> <artifactId>springmvcartifactId> <version>0.0.1-SNAPSHOTversion> <packaging>warpackaging> <properties> <spring.version>4.2.5.RELEASEspring.version> <aspectj.version>1.8.8aspectj.version> <slf4j.version>1.7.21slf4j.version> <junit.version>4.12junit.version> <commons-lang3.version>3.3.2commons-lang3.version> properties> <dependencies> <dependency> <groupId>org.apache.commonsgroupId> <artifactId>commons-lang3artifactId> <version>${commons-lang3.version}version> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-coreartifactId> <version>${spring.version}version> <exclusions> <exclusion> <groupId>commons-logginggroupId> <artifactId>commons-loggingartifactId> exclusion> exclusions> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-beansartifactId> <version>${spring.version}version> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-contextartifactId> <version>${spring.version}version> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-context-supportartifactId> <version>${spring.version}version> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-aopartifactId> <version>${spring.version}version> <exclusions> <exclusion> <groupId>commons-logginggroupId> <artifactId>commons-loggingartifactId> exclusion> exclusions> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-txartifactId> <version>${spring.version}version> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-webmvcartifactId> <version>${spring.version}version> dependency> <dependency> <groupId>org.slf4jgroupId> <artifactId>slf4j-apiartifactId> <version>${slf4j.version}version> dependency> <dependency> <groupId>org.slf4jgroupId> <artifactId>slf4j-log4j12artifactId> <version>${slf4j.version}version> dependency> <dependency> <groupId>org.slf4jgroupId> <artifactId>jcl-over-slf4jartifactId> <version>${slf4j.version}version> dependency> <dependency> <groupId>org.slf4jgroupId> <artifactId>jul-to-slf4jartifactId> <version>${slf4j.version}version> dependency> <dependency> <groupId>junitgroupId> <artifactId>junitartifactId> <version>${junit.version}version> dependency> <dependency> <groupId>javax.servletgroupId> <artifactId>jstlartifactId> <version>1.2version> dependency> <dependency> <groupId>javax.servletgroupId> <artifactId>javax.servlet-apiartifactId> <version>3.0.1version> <scope>providedscope> dependency> <dependency> <groupId>taglibsgroupId> <artifactId>standardartifactId> <version>1.1.2version> dependency> dependencies> <build> <sourceDirectory>srcsourceDirectory> <plugins> <plugin> <artifactId>maven-compiler-pluginartifactId> <version>3.3version> <configuration> <source>1.7source> <target>1.7target> configuration> plugin> <plugin> <artifactId>maven-war-pluginartifactId> <version>2.6version> <configuration> <warSourceDirectory>WebContentwarSourceDirectory> <failOnMissingWebXml>falsefailOnMissingWebXml> configuration> plugin> plugins> build> project>
第三步:配置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" 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"> <servlet> <servlet-name>springDispatcherServletservlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class> <init-param> <param-name>contextConfigLocationparam-name> <param-value>classpath:springmvc.xmlparam-value> init-param> <load-on-startup>1load-on-startup> servlet> <servlet-mapping> <servlet-name>springDispatcherServletservlet-name> <url-pattern>/url-pattern> servlet-mapping> web-app>
web.xml中配置了DispatcherServlet,指定了springmvc配置文件的位置和名称。
第四步:在类路径下创建springmvc的配置文件,文件名与web.xml中配置文件名称相同
springmvc.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:context="http://www.springframework.org/schema/context" 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/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd"> <context:component-scan base-package="com.springmvc">context:component-scan> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/">property> <property name="suffix" value=".jsp">property> bean> beans>
springmvc.xml中指定了自动扫描的包,下面我们就需要创建com.springmvc这个包
第五步:在com.springmvc中创建HelloWorldController类
HelloWorldController.java
package com.springmvc; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller public class HelloWorldController { /** * 1. 使用 @RequestMapping 注解来映射请求的 URL * 2. 返回值会通过视图解析器解析为实际的物理视图, 对于 InternalResourceViewResolver 视图解析器, 会做如下的解析: * 通过 prefix + returnVal + 后缀 这样的方式得到实际的物理视图, 然会做转发操作 * * /WEB-INF/views/hello.jsp * * @return */ @RequestMapping(value="helloWorld",method=RequestMethod.GET) public String helloWorld(){ System.out.println("Hello World"); return "hello"; } }
按照之前的目录结构创建index.jsp、hello.jsp
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>首页title> head> <body> <a href="helloWorld">点击我跳转到hello.js页面a> body> html>
hello.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>hellotitle> head> <body> <h4>hello pageh4> body> html>
将项目添加到tomcat中,并启动tomcat
访问:http://localhost:8080/springmvc/
可以看到如下页面:
点击链接,将会跳转到hello.jsp页面
这个小案例的流程大概是:点击 点击我跳转到hello.js页面 超链接发送请求,由HelloWorldController中的helloWorld方法
来进行响应,返回到/WEB-INF/views/hello.jsp页面.