SpringMVC框架学习之框架初体验(一)
目录
前言
在之前Web项目开发过程中,大家往往会采用SSH结构来开发一套web项目,
但是,由于,struts漏洞问题的出现,这种架构显得不是那么的好了。
所以,目前,大多数人回采用,SpringMVC+MyBatis或SpringMVC+Hibernate
来开发项目以取代近乎被淘汰的SSH架构开发模式。
框架简介
SpringMVC是Spring框架中的一个很重要的模块。它实现了MVC结构
。使整个符合MVC结构的web项目的开发更加方便快捷。
Spring Web MVC的核心组件
Spring Web MVC提供了M、V和C相关的主要实现组件,具体如下:
- DispatcherServlet(控制器,请求入口)
- HandlerMapping(控制器,请求派发)
- Controller(控制器,请求处理流程)
- ModelAndView(模型,封装业务处理结果和视图)
- ViewResolver(视图,视图显示器)
Spring Web MVC的处理流程
- 浏览器向Spring发出请求,请求交给前端控制器DispatcherServlet处理。
- 控制器通过HandlerMapping找到相应的Controller组件处理请求
- 执行Controller组件约定方法请求,在约定方法调用模型组件完成
业务处理。约定方法可以返回一个ModelAndView对象,封装了处理结
果数据和视图名称信息。 - 控制器接受ModelAndView之后,调用ViewResolver组件,定位View(JSP)
并传递数据信息,生成响应界面结果。
创建一个第一个Springmvc框架。
步骤:
1创建web项目
2导入相应的jar包,其中核心jar包如下:
- spring-web、
- spring-webmvc、
- spring-core 、
- spring-context、
- spring-beans、
- commons-logging、
- spring-expression、
3创建Spring Web MVC 的XML配置文件,文件名设置为springmvc.xml(其实就是spring的配置文件)。
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:aop="http://www.springframework.org/schema/aop" xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:jms="http://www.springframework.org/schema/jms" xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:oxm="http://www.springframework.org/schema/oxm" xmlns:p="http://www.springframework.org/schema/p"
xmlns:task="http://www.springframework.org/schema/task" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-3.1.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.1.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd"
default-autowire="byName">
</beans>
4将前端控制器组件DispatherServlet配置到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" version="3.0">
<display-name>GIMS_SPRING1</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>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<welcome-file-list>
<welcome-file>/WEB-INF/jsp/index.jsp</welcome-file>
</welcome-file-list>
</web-app>
5书写Controller控制器
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
/**
* 控制器
* @author SHENGYUNT
*
*/
public class TestController extends AbstractController {
/**
*从控制窗体接受username和password并打印。
*/
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request,
HttpServletResponse response) throws Exception {
String username = request.getParameter("username");
String password = request.getParameter("password");
System.out.println(username+":"+password);
//此处的index是试图解析器InternalResourceViewResolver前缀后缀之间的值
return new ModelAndView("index");
}
}
6在springmvc.xml中配置TestController控制器
<?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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-4.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
<!-- 配置控制器 -->
<bean name="/gradController.do" class="com.gims.controller.GraduateController"/>
<!-- 配置视图解析器,prefix为前缀,suffix为后缀 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name="prefix" value="/WEB-INF/" />
<property name="suffix" value=".jsp"/>
</bean>
</beans>
7编写index.jsp页面
<%@ page language="java" import="java.util.*" 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">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="Content-Type" content="text/http;charset=utf8">
</head>
<body>
This is my JSP page. <br>
<form action="gradController.do">
<table cellpadding="1" bgcolor="#aaaaaaaa" >
<tr >
<td width="100" >username</td>
<td width="100" ><input name="username" /> </td>
</tr>
<tr>
<td width="100" >password</td>
<td width="100" ><input name="password" /></td>
</tr>
<tr><td width="100"><input type="submit" name="login" value="submit" /></td></tr>
</table>
</form>
</body>
</html>
综上,为整个SpringMVC项目的环境搭建过程。