上回写过搭建SSH框架(Struts 2 + Hibernate + Spring)的教程,这里做一篇SpringMVC + Hibernate + Spring 的教程,由于SpringMVC是Spring的一个web子项目,所以在MyEclipse中有很好的集成环境可以搭建.搭建步骤见下
1.新建一个web工程
2.集成Sping框架(其实内置Spring + SpringMVC)
所需要的包是AOP,Core,Persistence Core(支持hibernate),Misc,Web 共5个包,对应的勾选图中5项
3.这样就已经集成了SpringMVC(Spring的web展现层)+Spring,接下来需要在集成数据持久层,这里我们选择全自动化,功能强大的Hibernate框架
接下来的步骤就和搭建SSH(Struts 2 + Hibernate + Spring)的步骤一样了,不造车轮.
4.这样就集成好了该三大框架的开发环境,但SpringMVC还需要配置一些xml,才能正常使用.
在web.xml中配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<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-servlet.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</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:springmvc-servlet.xml</param-value>
</context-param>
</web-app>
接着在src目录下创建springmvc-servlet.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/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 扫描所有的 controller -->
<context:component-scan base-package="com.sunline.controller" />
<!-- 启动注解驱动 SpringMVC 功能 -->
<mvc:annotation-driven />
<bean id="internalResourceViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
在/WebRoot目录下创建一个jsp文件,用于存放要展示的视图层页面.接着在new一个index.jsp
在src下建一个com.sunline.controller包,再新建一个TestController,用于跳转视图的控制,相当于Struts 2 的Action类
package com.sunline.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class TestController {
@RequestMapping(value="home")
public String index(){
return "/main";
}
}
再在jsp文件夹下新建一个main.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 'main.jsp' starting page</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="styles.css">
-->
</head>
<body>
<b>欢迎来到主页</b>. <br>
</body>
</html>
修改建项目时,自动产生的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="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="styles.css">
-->
</head>
<body>
This is my JSP page. <br>
<a href="${pageContext.request.contextPath }/home">点击到主页</a>
</body>
</html>
一切工作做好之后的工程目录是这样的
运行工程:
大功告成,接下来就可以在里面尽情地造车轮了!