这一段是重头戏来了,如何集成SSH:
1. 集成Struts2
1.1 修改pom.xml
在dependencies中添加:
<!-- freemarker -->
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.18</version>
</dependency>
<!-- Struts2 -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>${struts.version}</version>
</dependency>
然后添加properties段:
<properties>
<struts.version>2.3.15</struts.version>
</properties>
1.2 修改web.xml
去WEB-INF目录找到web.xml文件并添加:
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
1.3 添加Action
在src/main/java目录下添加Action:
package com.freesoft.action;
import com.opensymphony.xwork2.ActionSupport;
public class TestAction extends ActionSupport {
private String username;
private String password;
@Override
public String execute() throws Exception {
return super.execute();
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
1.4 添加struts.xml
去src/main/resources目录添加struts.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="default" extends="struts-default">
<action name="test" class="com.freesoft.action.TestAction">
<result>/result.jsp</result>
</action>
</package>
</struts>
1.5 添加jsp
最后添加两个jsp,其中一个test.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%
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 'input.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>
<s:form action="test.action">
<s:textfield name="username" label="username"></s:textfield>
<s:password name="password" label="password"></s:password>
<s:submit value="submit"></s:submit>
</s:form>
</body>
</html>
最后建立一个result.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%
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 'input.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>
<s:property value="username"/><br>
<s:property value="password"/><br>
</body>
</html>
1.6 运行和调试
选择新建一个Maven Debug Configuration,然后goals输入tomcat7:redeploy即可。
需要调试的时候修改tomcat.bat(Windows下)或者tomcat.sh(linux下)文件,其中windows下修改为:
call "%EXECUTABLE%" jpda start %CMD_LINE_ARGS%
然后启动Tomcat,MyEclipse打开远程调试即可调试程序。
2. 集成Hibernate
2.1 集成MySQL Driver
首先我们集成MySQL的数据库驱动,我们在pom.xml中添加:
<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
2.2 集成Hibernate
同样修改pom.xml:
<!-- Hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
properties段:
<properties>
<struts.version>2.3.15</struts.version>
<mysql.version>5.1.29</mysql.version>
<hibernate.version>4.3.1.Final</hibernate.version>
</properties>
2.3 添加hibernate.cfg.xml
在/src/resource中添加hibernate.cfg.xml,内容是:
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<!-- 连接url -->
<property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>
<!-- 用户名 -->
<property name="connection.username">root</property>
<!-- 密码 -->
<property name="connection.password">root</property>
<!-- 驱动 -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- 方言 -->
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="show_sql">true</property>
<!-- <property name="format_sql">true</property> -->
<mapping resource="hbm/User.hbm.xml" />
</session-factory>
</hibernate-configuration>
然后添加源代码目录:src/test/java和src/test/resources,并且将其加入到工程的源代码目录中。
2.4 添加测试
添加hbm.xml:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="com.freesoft.bean.User" table="user" catalog="hibernate">
<id name="id" type="long">
<column name="id" />
<generator class="increment" />
</id>
<property name="username" type="string">
<column name="username" length="50" />
</property>
<property name="password" type="string">
<column name="password" length="50" />
</property>
<property name="telephone" type="integer">
<column name="telephone" />
</property>
<property name="gender" type="character">
<column name="gender" length="1" />
</property>
<property name="graduation" type="boolean">
<column name="graduation" />
</property>
<property name="birthday" type="date">
<column name="birthday" length="10" />
</property>
<property name="marryTime" type="timestamp">
<column name="marryTime" length="19" />
</property>
<property name="file" type="blob">
<column name="file" />
</property>
</class>
</hibernate-mapping>
然后编写测试代码。
package com.freesoft.test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Blob;
import java.sql.Timestamp;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;
import com.freesoft.bean.User;
import com.freesoft.util.db.HibernateUtil;
public class TestUser {
@Test
public void testAdd() {
Session session = HibernateUtil.getSession();
Transaction tx = null;
try {
User user = new User();
user.setUsername("张三");
user.setPassword("password");
user.setGender('F');
user.setBirthday(new java.sql.Date(new java.util.Date().getTime()));
user.setGraduation(true);
user.setTelephone(1234567890);
user.setMarryTime(new Timestamp(new java.util.Date().getTime()));
InputStream ins = new FileInputStream(new File("src/test/resources/test.xml"));
Blob file = Hibernate.getLobCreator(session).createBlob(ins, ins.available());
user.setFile(file);
tx = session.beginTransaction();
session.save(user);
tx.commit();
} catch (Exception e) {
e.printStackTrace();
} finally {
HibernateUtil.closeSession(session);
}
}
@Test
public void testGet() {
Session session = HibernateUtil.getSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
User user = (User) session.get(User.class, 1L);
Blob file = user.getFile();
long length = file.length();
byte[] buf = file.getBytes(1, (int) length);
OutputStream outs = new FileOutputStream(new File("src/test/resources/111.xml"));
outs.write(buf);
outs.flush();
outs.close();
tx.commit();
} catch (Exception e) {
e.printStackTrace();
} finally {
HibernateUtil.closeSession(session);
}
}
}
注意这里为了避免由于文件内容是中文引起的"Data too long for column 'file' at row 1"错误,我没有将file字段设置为byte[]而是设置为了Blob类型。
再新建一个maven的debug configuration,目录选择工程目录,goals选择test,然后run。
3. 集成Spring
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.0.2.RELEASE</version>
<scope>runtime</scope>
</dependency>
</dependencies>