Spring 3.1.0 Hibernate 3.0 Eclipse Spring WEB例子
1. 准备工作:
1.1 在Eclipse上配置WEB例子所需的服务器,此处以Tomcat为例
1.1 进入 Windows --> Preference --> Servers --> Runtime Environment --> Add --> choose corresponding server version and path
1.2 添加 2.1中建立的Web项目到服务器中
1.2 下载Spring Library 和 Hibernate library
1.2.1 Spring library
下载地址: http://www.springsource.com/download/community
1.2.2 hibernate library
下载地址: http://sourceforge.net/projects/hibernate/files/hibernate3/
PS: 建议下载含文档的Library,分别为Javadoc和referrence.特别是初学者正要好好阅读该两个文档。
2. 开始建立Spring + Hibernate Web例子
2.1 在Eclipse上建立一个Dynamic Web project
2.2 添加该Web工程到Tomcat
2.3 导入spring 和 hibernate 所有的库入 /[新建项目]/WEB-INF/lib/文件夹中
2.3.1 spring
我提供的Spring以3.1.0.RC1版为例( 我的工程中需要用到transaction和对应的Context的Annotation,根据需要可以调整jar的数目。

2.3.2 hibernate
提供hibernate 3.6.8 为例。

3. 添加解析配置文件等其他配置所需要的库( 此处为我在进行事务管理和注解管理,配置文件管理时所碰到的各种问题所缺少的库 )
jstl.jar 为了使用 jsp 的标签导入库
mysql-connector-java-5.1.18-bin 为了使用JDBC连接MySQL
配置文件的一些解析错误:
其中cglib-nodep-2.2.2 与 asm*.jar的版本可能有冲突,具体的可参看官网提供信息。

4. 拷贝完所有Web项目需要的Jar后,进行一些简单的测试页面
5. 修改web.xml, 使得Spring 接管用户的Web请求。
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>SpringTutorial</display-name>
<servlet>
<servlet-name>dispatch</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatchServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/springservlet.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dispatch</servlet-name>
<url-pattern>*.*</url-pattern>
</servlet-mapping>
</web-app>
6. 在WEB-INF目录下新建springservlet.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:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
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.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<context:component-scan base-package="com.baidu.controllers"/>
<bean id="viewResolver" class="org.springframwork.web.servlet.view.InternalResourceViewResolver"
p:preffix="/WEB-INF/jsp" p:suffix=".jsp"/>
</beans>
7. 新建com.baidu.controllers包,在该包下新建一个UserShow.java
package com. baidu.controllers;
@Controller
@RequestMapping("/welcome.htm")
public class UserShow {
@RequestMapping(method=RequestMethod.GET)
public String showWelcome(){
return "welcome";
}
}
8. 在WEB-INF目录下新建jsp目录,并jsp目录下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>
</head>
<body>
Welcome to this website.
</body>
</html>
9. 测试 http://localhost:8080/新建项目/welcome.htm 显示页面为welcome.jsp

浙公网安备 33010602011771号