spring MVC入门示例 springmvcdemo
来源:互联网
1. Spring MVC介绍
Spring Web MVC是一种基于Java的实现了Web MVC设计模式的请求驱动类型的轻量级Web框架,即使用了MVC架构模式的思想,将web层进行职责解耦,基于请求驱动指的就是使用请求-响应模型,框架的目的就是帮助我们简化开发,Spring Web MVC也是要简化我们日常Web开发的。
Spring Web MVC也是服务到工作者模式的实现,但进行可优化。前端控制器是DispatcherServlet;应用控制器其实拆为处理器映射器(Handler Mapping)进行处理器管理和视图解析器(View Resolver)进行视图管理;页面控制器/动作/处理器为Controller接口(仅包含ModelAndView handleRequest(request, response) 方法)的实现(也可以是任何的POJO类);支持本地化(Locale)解析、主题(Theme)解析及文件上传等;提供了非常灵活的数据验证、格式化和数据绑定机制;提供了强大的约定大于配置(惯例优先原则)的契约式编程支持。
2. Spring MVC的优点
让我们能非常简单的设计出干净的Web层和薄薄的Web层;
进行更简洁的Web层的开发;
天生与Spring框架集成(如IoC容器、AOP等);
提供强大的约定大于配置的契约式编程支持;
能简单的进行Web层的单元测试;
支持灵活的URL到页面控制器的映射;
非常容易与其他视图技术集成,如Velocity、FreeMarker等等,因为模型数据不放在特定的API里,而是放在一个Model里(Map数据结构实现,因此很容易被其他框架使用);
非常灵活的数据验证、格式化和数据绑定机制,能使用任何对象进行数据绑定,不必实现特定框架的API;
提供一套强大的JSP标签库,简化JSP开发;
支持灵活的本地化、主题等解析;
更加简单的异常处理;
对静态资源的支持;
支持Restful风格。
2. 环境配置
Hello World入门
准备开发环境和运行环境:
☆开发工具:eclipse
☆运行环境:tomcat6.0.20/tomcat7.0.52
☆工程:动态web工程(springmvc-chapter2)
☆spring框架下载:
spring-framework-3.1.1.RELEASE-with-docs.zip
☆依赖jar包:
1、 Spring框架jar包:
为了简单,将spring-framework-3.1.1.RELEASE-with-docs.zip/dist/下的所有jar包拷贝到项目的WEB-INF/lib目录下;
2、 Spring框架依赖的jar包:
需要添加Apache commons logging日志,此处使用的是commons.logging-1.1.1.jar;
需要添加jstl标签库支持,此处使用的是jstl-1.1.2.jar和standard-1.1.2.jar;
下面是具体步骤
新建一个web工程,file->new->web project,取名为springmvcdemo。将spring中的jar中复制到springmvcdemo/WEB-INF/lib下。spring中有些与struts相关的包可以不复制,因为这里用不到。除此之外,还需要
commons-logging.jar/jstl.jar/standard.jar这三个依赖包。配置完后,lib下的jar包如下所示:
[img]https://dl2.iteye.com/upload/attachment/0110/8123/6f632d87-7f7a-3109-afdf-fde0eaee0bc8.png[/img]
3. 入门示例
3.1 我们首先从web.xml开始配置。
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xmlns="https://java.sun.com/xml/ns/javaee" xmlns:web="https://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="https://java.sun.com/xml/ns/javaee https://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<display-name>springmvcdemo</display-name>
<servlet>
<servlet-name>springmvcdemo</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvcdemo</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>helloWorld.jsp</welcome-file>
</welcome-file-list>
</web-app>
load-on-startup:表示启动容器时初始化该Servlet;
url-pattern:表示哪些请求交给Spring Web MVC处理, “/” 是用来定义默认servlet映射的。也可以如“*.html”表示拦截所有以html为扩展名的请求。
自此请求已交给Spring Web MVC框架处理,因此我们需要配置Spring的配置文件,默认DispatcherServlet会加载WEB-INF/[DispatcherServlet的Servlet名字]-servlet.xml配置文件。本示例为WEB-INF/ SpringMVC-servlet.xml。
3.2 再来配置springmvcdemo-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="https://www.springframework.org/schema/beans"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xmlns:p="https://www.springframework.org/schema/p"
xmlns:context="https://www.springframework.org/schema/context"
xmlns:mvc="https://www.springframework.org/schema/mvc"
xsi:schemaLocation="
https://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans-3.1.xsd
https://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context-3.1.xsd
https://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
<bean name="/test1/helloWorld" class="com.test.web.controller.HelloWorldController" />
<context:component-scan base-package="com.test.web.controller"/>
<!-- ViewResolver -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
InternalResourceViewResolver:用于支持Servlet、JSP视图解析;
prefix和suffix:查找视图页面的前缀和后缀(前缀[逻辑视图名]后缀),比如传进来的逻辑视图名为hello,则该jsp视图页面应该存放在“WEB-INF/hello.jsp”;
<bean name="test1/helloWorld" ... />这里表示传来的url与代码中的controller的对应关系。
3.3 下面我们来写controller类代码
package com.test.web.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 HelloWorldController implements Controller{
public ModelAndView handleRequest(HttpServletRequest arg0,
HttpServletResponse arg1) throws Exception {
System.out.println("--------------Hello World! 大家好!--------------------");
return new ModelAndView("/helloWorld");
}
}
这里实现了一个接口,重写了其中的handleRequest函数。函数中,在后台打印一段文件,并将helloWorld.jsp页面返回给浏览器。因为在-servlet.xml中进行了配置,所以helloWorld.jsp文件应在WEB-INF文件夹下,且此处返回时仅写helloWorld即可,无需加后缀.jsp。
3.4 jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "https://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<base href="/go.html?url=<%=basePath%>">
<title>Hello World!</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="/go.html?url=styles.css">
-->
</head>
<body>
-----------------Hello World! 大家好! ----------------------<br>
</body>
</html>
3.5 完成后的目录结构
[img]https://dl2.iteye.com/upload/attachment/0110/8125/d0deef92-3613-30eb-8c85-a22c7ca7c7a2.png[/img]
5. 运行、访问页面
将springmvcdemo部署到tomcat服务器上,运行。在浏览器中输入访问地址:https://localhost:8080/springmvcdemo/test1/helloWorld应该就可以看到前台显示helloWorld.jsp页面,后台打印“--------------Hello World! 大家好!--------------------”字符串了。1. Spring MVC介绍
Spring Web MVC是一种基于Java的实现了Web MVC