应用示例:用户通过浏览器提交一个请求,服务端处理器在接受到这个请求后,给出一条欢迎信息"Hello Spring MVC",并在响应页面显示该信息,采用传统的配置式开发方式。
项目目录截图
1.将spring相关jar包和spring依赖的common-logging-1.2jar包复制到目录WebContent/WEB-INF/lib下,选中这些包复制到目录WebContent/WEB-INF/lib下,选中这些包:右键build path—>add build path
2.创建com.lifeng.controller包,在该包内创建类FirstController
FirstController.java
package com.lifeng.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
public class FirstController implements Controller{
@Override
public ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {
ModelAndView mv=new ModelAndView();
mv.addObject("hello", "Hello SpringMVC!!!");
mv.setViewName("welcome");
//如果没有配置视图解释器,则视图名称应为完整路径:
//mv.setViewName("/WEB-INF/jsp/welcome.jsp");
return mv;
}
}
3.在src目录下创建spring mvc的配置文件springmvc.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 1.配置处理器映射器,确定一种请求~响应的映射规则-->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>
<!-- 2.配置处理器适配器,配置对处理器的handleRequest()方法的调用 -->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>
<!-- 3.配置自定义控制器,一条具体的映射关系,name对应客户端url请求,class对应服务端响应程序 -->
<bean name="/hello.do" class="com.lifeng.controller.FirstController"></bean>
<!-- 4.配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--逻辑视图前缀-->
<property name="prefix" value="/WEB-INF/jsp/"></property>
<!--逻辑视图后缀,匹配模式:前缀+逻辑视图+后缀,形成完整路径名-->
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
4.在WebContent目录下新建一个web.xml配置文件,在该文件配置DispatcherServlet核心控制器
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>springmvc1</display-name>
<!-- 以下代码为新加上去的 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
5.在WebContent的WEB-INF下新建一个文件夹jsp用于放置视图,在该文件夹中新建一个jsp页面welcome
welcome.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
<body>
<h1>${hello }</h1>
</body>
</html>
6.运行项目,在浏览器中输入http://localhost:8080/springmvc1/hello.do
运行结果图