一、步骤:
– 加入 jar 包
– 在 web.xml 中配置 DispatcherServlet
– 加入 Spring MVC 的配置文件
– 编写处理请求的处理器,并标识为处理器
– 编写视图
commons-logging-1.1.1.jar
jstl.jar
spring-aop-4.0.0.RELEASE.jar
spring-beans-4.0.0.RELEASE.jar
spring-context-4.0.0.RELEASE.jar
spring-core-4.0.0.RELEASE.jar
spring-expression-4.0.0.RELEASE.jar
spring-web-4.0.0.RELEASE.jar
spring-webmvc-4.0.0.RELEASE.jar
standard.jar
二、配置 web.xml
配置 DispatcherServlet:DispatcherServlet默认加载/WEBINF/<servletName-servlet>.xml 的 Spring 配置文件, 启动 WEB 层
的 Spring 容器。可以通过 contextConfigLocation初始化参数自定
义配置文件的位置和名称
<!-- 配置 DispatcherServlet -->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置 DispatcherServlet的一个初始化参数:配置 SpringMVC配置文件的位置和名称 -->
<!--
实际上也可以不通过 contextConfigLocation来配置 SpringMVC的配置文件,而使用默认的.
默认的配置文件为:/WEB-INF/<servlet-name>-servlet.xml
-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
三、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.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<context:component-scan base-package="com.atguigu.springmvc"></context:component-scan>
<!-- 视图解释类 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/><!--可为空,方便实现自已的依据扩展名来选择视图解释类的逻辑 -->
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
</bean>
</beans>
四。
packagecom.atguigu.springmvc.handlers;
importorg.springframework.stereotype.Controller;
importorg.springframework.web.bind.annotation.RequestMapping;
@Controller /**@Controller这样标识就是控制器了。 **/
public class HelloWorld {
/**
* 1. 使用@RequestMapping注解来映射请求的 URL
* 2. 返回值会通过视图解析器解析为实际的物理视图,对于 InternalResourceViewResolver视图解析器,会做如下的解析:
* 通过 prefix + returnVal +后缀这样的方式得到实际的物理视图,然会做转发操作
*
*/WEB-INF/views/success.jsp
*
* @return
*/
@RequestMapping("/helloworld") /** servlet请求这里 */
public String hello(){
System.out.println("hello world");
return"success";
}
}
Success.jsp
<%@ page language="java"contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%-- <%@ taglib prefix="fmt"uri="http://java.sun.com/jsp/jstl/fmt"%> --%>
<!DOCTYPEhtmlPUBLIC "-//W3C//DTDHTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<metahttp-equiv="Content-Type"content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h4>Sucess Page</h4>
time: ${requestScope.time }
<br><br>
names: ${requestScope.names }
<br><br>
request user: ${requestScope.user}
<br><br>
session user: ${sessionScope.user}
<br><br>
request school:${requestScope.school }
<br><br>
session school:${sessionScope.school }
<br><br>
abc user:${requestScope.abc }
<br><br>
mnxyz user:${requestScope.mnxyz }
<br><br>
<fmt:messagekey="i18n.username"></fmt:message>
<br><br>
<fmt:messagekey="i18n.password"></fmt:message>
<br><br>
</body>
</html>