1.整合步骤:
第一步 没有支持多个数据库
create table student
(
sno int identity primary key,
sname varchar(20),
sage int
)
一、新建一个web项目
修改web.xml、文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
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_2_5.xsd">
<filter>
<filter-name>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/classes/applicationContext.xml
</param-value>
</context-param>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
二、在src下建立org包,用来存放Hibernate的DAO类
三、在src下建立org.action包,用来存放Struts2中的action控制器类
四、支持Hibernate,在hibernate.cfg.xml中的Configuration窗口 Add connection.autocommishi
支持Spring
五、建立一个数据库连接,选中一个表支持Hibernate
六、把Sturts-2.0.11.2-all中lib文件下所有包考到项目的WEB-INF中的lib下,删除三个包
asm.jar
asm-attrs.jar
cglib-2.1.3.jar
七、在org.action 包下新建LoginAction.java文件
代码
package org.action;
import java.io.UnsupportedEncodingException;
import org.Student;
import org.StudentDAO;
public class LoginAction {
String stuName;
int stuAge;
StudentDAO studao;
public StudentDAO getStudao() {
return studao;
}
public void setStudao(StudentDAO studao) {
this.studao = studao;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public int getStuAge() {
return stuAge;
}
public void setStuAge(int stuAge) {
this.stuAge = stuAge;
}
public String execute() throws UnsupportedEncodingException {
System.out.println(stuName);
studao.save(new Student(stuName,stuAge));
// String str = "success";
return null;
}
}
八、在src文件下新建Struts.xml文件写上如下代码
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.i18n.encoding" value="gbk"></constant>
<package name="struts" extends="struts-default">
<!-- <action name="login" class="org.action.LoginAction"> -->
<action name="login" class="loginSpring">
<result>/ok.jsp</result>
</action>
</package>
</struts>
九、在applicationContext.xml文件中添加如下代码
<bean id="loginSpring" class="org.action.LoginAction" scope="prototype">
<property name="studao">
<ref bean="StudentDAO" />
</property>
</bean>
十、在WebRoot下新建admin.jsp
<%@ page language="java" contentType="text/html;charset=GBK" pageEncoding="GBK"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK" />
<meta name="keywords" content="" />
<meta name="description" content="" />
<link href="index.css" rel="stylesheet" type="text/css" />
<title></title>
</head>
<body>
<form action="login.action" method="post">
name:<input type="text" name="stuName"/>
age:<input type="text" name="stuAge"/>
<input type="submit" value="submit"/>
</form>
</body>
</html>
第二步 在原来的项目基础上支持多数据库
重新建立hibernate.cfg.xml文件支持SqlServer 2000数据库
导入包Sql.jar
注意:在Student.hbm.xml中
<class name="org.Student" table="student" catalog="bus">去掉catalog包
2..三个框架添加顺序:hibernate,spring ,struts
配置文件:
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
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_2_5.xsd">
<!-- struts的有关配置 -->
<filter>
<filter-name>struts</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- spring 的有关配置 -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<context-param>
<param-name>
contextConfigLocation
</param-name>
<param-value>
/WEB-INF/classes/applicationContext.xml
</param-value>
</context-param>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
struts的配置,struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.i18n.encoding" value="gbk"></constant>
<package name="struts" extends="struts-default">
<!-- <action name="login" class="org.action.LoginAction"> -->
<action name="login" class="loginSpring">
<result name="sucess" type="redirect">
<param name="location">/ok.jsp</param>
</result>
<result name="failer" type="redirect">
<param name="location">/fail.jsp</param>
</result>
</action>
</package>
</struts>
spring的配置 applacationContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation"
value="classpath:hibernate.cfg.xml">
</property>
</bean>
<!-- DAO 注入 -->
<bean id="UsersDAO" class="org.UsersDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<!-- spring 的事物-->
<bean id="loginSpring" class="org.action.loginAction"
scope="prototype">
<property name="usersdao">
<ref bean="UsersDAO" />
</property>
</bean>
</beans>
jsp页面。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>
<form action="login.action?flag=12" method="post">
<input type="text" name="userName">
<input type="password" name="userPwd">
<input type="submit" value="sumit">
</form>
</body>
</html>
action处理java源代码
package org.action;
import javax.servlet.http.HttpServletRequest;
import net.sf.jasperreports.olap.mapping.Mapping;
import org.Users;
import org.UsersDAO;
import org.apache.struts2.ServletActionContext;
import org.omg.CORBA.Request;
import org.springframework.web.struts.ActionSupport;
public class loginAction extends ActionSupport {
UsersDAO usersdao;
String UserName;
String UserPwd;
public String getUserName() {
return UserName;
}
public void setUserName(String userName) {
UserName = userName;
}
public String getUserPwd() {
return UserPwd;
}
public void setUserPwd(String userPwd) {
UserPwd = userPwd;
}
public UsersDAO getUsersdao() {
return usersdao;
}
public void setUsersdao(UsersDAO usersdao) {
this.usersdao = usersdao;
}
public String execute(){
HttpServletRequest request = ServletActionContext.getRequest();
// String url = request.getHeader("Referer");
System.out.println(request.getParameter("flag"));
Users u=new Users();
u.setUserName(UserName);
u.setUserPwd(UserPwd);
if(usersdao.findByExample(u).size()>0){
String sucess = "sucess";
return sucess;
}else{
String failer = "failer";
return failer;
}
//return null;
}
}