自己一直在用java的一些框架,但是自己从来没有搭过,昨天正好有点时间,就自己搭了一个SSH框架,这个是比较常用的,今天把一些东西记录下,以便以后查看~~~(我是在eclipse下搭,如果是MyEclipse,它直接生成就行了,不需要什么配置以及包的导入了)
首先新建一个WEB项目,导入SSH框架需要的包,这个就不说了~~~
接下来看整个项目的关键部分,就是配置文件啦~~~
1.web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>SimpleSSH</display-name> <!-- struts2的拦截器 --> <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> <!-- spring的监听器 --> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <!-- spring配置文件 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:applicationContext*.xml </param-value> </context-param> <welcome-file-list> <welcome-file>login.jsp</welcome-file> </welcome-file-list> </web-app>
2.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>
<!-- 声明action由spring管理 -->
<constant name="struts.objectFactory" value="spring" />
<constant name="struts.i18n.encoding" value="utf-8"></constant>
<package name="default" extends="struts-default">
<!-- 此处action的class属性直接由spring管理,loginAction是在spring中的一个bean -->
<action name="login" class="loginAction">
<result name="success">/success.jsp</result>
<result name="input">/login.jsp</result>
<result name="error">/login.jsp</result>
</action>
</package>
</struts>
3.hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 数据库驱动 -->
<property name="connection.driver_class">
oracle.jdbc.driver.OracleDriver
</property>
<!-- 数据库url -->
<property name="connection.url">
jdbc:oracle:thin:@localhost:1521:orcl
</property>
<!-- 用户名 -->
<property name="connection.username">wwd</property>
<!-- 密码 -->
<property name="connection.password">edison</property>
<!-- 数据库方言 -->
<property name="dialect">
org.hibernate.dialect.OracleDialect
</property>
<!-- 后台是否显示SQL -->
<property name="show_sql">true</property>
<!-- 根据model类生成表 -->
<property name="hbm2ddl.auto">update</property>
<!-- 是否自动提交 -->
<property name="connection.autocommit">true</property>
<!-- 对应表xml文件 -->
<mapping resource="test.hbm.xml"/>
</session-factory>
</hibernate-configuration>
4.test.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.action.model.People" table="test" schema="wwd">
<id name="id" type="java.lang.Long">
<column name="id" not-null="true"/>
</id>
<property name="name" type="java.lang.String">
<column name="name" length="50" not-null="true"/>
</property>
<property name="age" type="java.lang.Integer">
<column name="age" not-null="true"/>
</property>
</class>
</hibernate-mapping>
5.applicationContext.xml(这个是最关键的,他整合的struts和hibernate )
<?xml version="1.0" encoding="UTF-8"?>
<beans default-autowire="autodetect" xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<!-- 设置hibernate文件(其实数据源直接在spring的配置文件中设置也行的) -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>
<!-- 事物处理(暂时没用) -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>
<!-- 注入hibernateTemplate -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<!-- 抽象DAO的实现,要用到hibernateTemplate,这里我是直接注入的,你也可以让你的实现DAO继承自HibernateDaoSupport这样就可以直接getHibernateTemplate()获取 -->
<bean id="testDaoImpl" class="com.action.dao.impl.TestDaoImpl">
<property name="hibernateTemplate">
<ref bean="hibernateTemplate"/>
</property>
</bean>
<!-- 与struts中的class相对应 -->
<!-- 此处就是由spring来管理action -->
<bean id="loginAction" class="com.action.LoginAction" scope="prototype">
<property name="testDaoImpl">
<ref bean="testDaoImpl"/>
</property>
</bean>
</beans>
经过上面的配置之后,我们可以进行测试了~~~
测试代码:
1.login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!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>
<s:form action="login" name="form1">
<s:textfield name="userName" label="userName"/>
<s:password name="password" label="password"/>
<input type="submit" value="提交"/>
</s:form>
<s:actionerror/>
</body>
</html>
2.success.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>
this is a success page!
<input type="button" οnclick="alert('ok')"/>
</body>
</html>
3.People.java
public class People implements Serializable {
private static final long serialVersionUID = 4372014843795725183L;
private Long id;
private String name;
private Integer age;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
4.testDaoImpl.java
public class TestDaoImpl implements BaseDao{
private HibernateTemplate hibernateTemplate;
@Override
public void saveOrUpdate(Object obj) {
hibernateTemplate.saveOrUpdate(obj);
}
public void test(){
System.out.println("test");
}
public HibernateTemplate getHibernateTemplate() {
return hibernateTemplate;
}
public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
}
}
5.LoginAction.java
public class LoginAction extends ActionSupport{
private static final long serialVersionUID = 5654569961853096359L;
private TestDaoImpl testDaoImpl;
public String execute(){
People p = new People();
p.setId(6L);
p.setAge(15);
p.setName("test");
testDaoImpl.saveOrUpdate(p);
return SUCCESS;
}
public TestDaoImpl getTestDaoImpl() {
return testDaoImpl;
}
public void setTestDaoImpl(TestDaoImpl testDaoImpl) {
this.testDaoImpl = testDaoImpl;
}
}
好了,这样启动服务器就能运行了,这只是一个很简单的SSH框架,要灵活的运用还要慢慢去深入和了解~~~