这一段是重头戏来了,如何集成SSH:
在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> <struts.version>2.3.15</struts.version> </properties>
去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>
在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; } }
去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>
最后添加两个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>
<%@ 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>
选择新建一个Maven Debug Configuration,然后goals输入tomcat7:redeploy即可。
需要调试的时候修改tomcat.bat(Windows下)或者tomcat.sh(linux下)文件,其中windows下修改为:
call "%EXECUTABLE%" jpda start %CMD_LINE_ARGS%
<!-- mysql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency>
<!-- Hibernate --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>${hibernate.version}</version> </dependency>
<properties> <struts.version>2.3.15</struts.version> <mysql.version>5.1.29</mysql.version> <hibernate.version>4.3.1.Final</hibernate.version> </properties>
<?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>
添加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); } } }
<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.0.2.RELEASE</version> <scope>runtime</scope> </dependency> </dependencies>