SpringMVC 学习-入门篇
一、都需要哪些 Jar 包
<properties> <spring.version>4.2.3.RELEASE</spring.version> </properties> <dependencies> <!-- SpringMVC start --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <!-- SpringMVC end --> </dependencies>
二、web.xml 配置文件
<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" version="3.0" metadata-complete="true"> <servlet> <servlet-name>springmvc</servlet-name> <!-- 负责分发 --> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/*-servlet.xml</param-value> </init-param> <!-- 启动服务时加载 --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
三、在默认路径 WEB-INF 下创建配置文件 springmvc-servlet.xml
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd "> <!-- 注解驱动 --> <mvc:annotation-driven/> <!-- 注解扫描包 --> <context:component-scan base-package="com.erim.web" /> <!-- 对加载页面的路径解析 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 前缀 --> <property name="prefix" value="/"></property> <!-- 后缀 --> <property name="suffix" value=".html"></property> </bean> </beans>
四、我们创建一个 Controller 层 Java 类
package com.erim.web; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class HelloWorldController { /** * @Description: 测试SpringMVC * @Author: Libra * @CreateTime: 2017年11月30日 下午3:49:09 * @return */ @ResponseBody @RequestMapping("/getHelloWorld") public String getHelloWorld() { return "Hello World!"; } }
五、SpringMVC 访问静态文件
在过滤所有请求的前提下,访问静态文件也是一种请求,那我们如何配置才可以访问静态文件呢。
如下:还是 springmvc-servlet.xml 配置文件
<!-- 静态资源访问 --> <mvc:resources location="/img/" mapping="/img/**"/> <mvc:resources location="/js/" mapping="/js/**"/>
六、跳转页面且传递参数
@RequestMapping("/helloWorld") public String openPage(HttpServletRequest request) { request.setAttribute("name", "LiMing"); return "hello"; }
html 页面无法接受参数,JSP 页面通过 EL 表达式可以接收参数,如下:
${name}