首页 > 代码库 > 简单搭建ssh框架(xml配置)
简单搭建ssh框架(xml配置)
过两天就要找工作了,希望能找个好工作,啥叫好工作?就是妹子多点的哈哈
这篇是纯xml配置ssh,过阵子我再做纯注释的。。。
开始
首先要导入所需要的jar包,这不多说了
配置spring3
sping 官方文档里明确告诉一个applicationContext.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/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="..." class="..."> <!-- collaborators and configuration for this bean go here --> </bean> <bean id="..." class="..."> <!-- collaborators and configuration for this bean go here --> </bean> <!-- more bean definitions go here --></beans>
接下来编写我们自己的applicationContext.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/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="empDao" class="com.bjsxt.dao.EmpDaoImpl" scope="prototype"> </bean> <bean id="empService" class="com.bjsxt.service.EmpServiceImpl" scope="prototype"> <property name="empDao" ref="empDao"></property> </bean> <bean id="empAction" class="com.bjsxt.action.EmpAction" scope="prototype"> <property name="empService" ref="empService"></property> </bean> <!-- more bean definitions go here --></beans>
别忘了在web.xml里加上
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:ApplictionContext.xml</param-value></context-param><listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
至此,spring的简单配置已经完成,接下来配置struts2
在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>
配置Struts2.xml
官方的文档里明确告诉我们一个Struts2.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> <constant name="struts.enable.DynamicMethodInvocation" value="false" /> <constant name="struts.devMode" value="true" /> <package name="default" namespace="/" extends="struts-default"> <default-action-ref name="index" /> <global-results> <result name="error">/error.jsp</result> </global-results> <global-exception-mappings> <exception-mapping exception="java.lang.Exception" result="error"/> </global-exception-mappings> <action name="index"> <result type="redirectAction"> <param name="actionName">HelloWorld</param> <param name="namespace">/example</param> </result> </action> </package> <include file="example.xml"/> <!-- Add packages here --></struts>
我们来配置我们自己的Struts2.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> <!-- 这句非常重要,把控制权交给Spring --> <constant name="struts.objectFactory" value="spring" /> <constant name="struts.enable.DynamicMethodInvocation" value="false" /> <constant name="struts.devMode" value="true" /> <package name="default" namespace="/" extends="struts-default"> <!-- 因为控制权交给Spring了,所以我们不用自己再写类路径了,直接跟Spring要就行了,名字是在applicationContext.xml里配置的bean名字 --> <action name="UserAction" class="empAction" method="fandAll"> <result name="success">/index.html</result> </action> </package> <!-- <include file="example.xml" /> --> <!-- Add packages here --></struts>
至此Struts2配置完毕
接下来配置hibernate3
首先要配置数据源,我用dbcp
在applicationContext.xml里添加以下内容
<!-- 配置数据源 --> <bean id="dataSource" class="org.apache.tomcat.dbcp.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property> <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"></property> <property name="username" value="scott"></property> <property name="password" value="tiger"></property> </bean> <!-- 配置sessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <!-- hibernate的设置 --> <property name="hibernateProperties"> <props> <prop key="dialect">org.hibernate.dialect.OracleDialect</prop> <prop key="show_sql">true</prop> </props> </property> <!-- 映射文件 --> <property name="mappingResources"> <list> <value>com/bjsxt/entity/Emp.hbm.xml</value> </list> </property> </bean><!-- 配置事务 --><bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 配置事务范围 --> <tx:advice id="txManager" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="set*" propagation="REQUIRED" /> <tx:method name="add*" propagation="REQUIRED" /> <tx:method name="updata*" propagation="REQUIRED" /> <tx:method name="*" propagation="REQUIRED" read-only="true" /> </tx:attributes> </tx:advice> <!-- 配置参与事务的类 --> <aop:config proxy-target-class="true"> <aop:pointcut id="aopMananger" expression="execution(* com.bjsxt.service.*Impl.*(..))" /> <aop:advisor advice-ref="txManager" pointcut-ref="aopMananger" /> </aop:config></beans>
BasicDataSource不一定是我的那个路径,我的好像比别人多了个dbcp,具体的请ctrl+shift+h 输入BasicDataSource,找到你项目中的这个类,右键复制路径
还要引入tx和aop标签
<?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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
到这步就整合完毕了,下面给出完整的文档:
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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>sshexml1</display-name> <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> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:ApplictionContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list></web-app>
applicationContext.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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 配置数据源 --> <bean id="dataSource" class="org.apache.tomcat.dbcp.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property> <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"></property> <property name="username" value="scott"></property> <property name="password" value="tiger"></property> </bean> <!-- 配置sessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <!-- hibernate的设置 --> <property name="hibernateProperties"> <props> <prop key="dialect">org.hibernate.dialect.OracleDialect</prop> <prop key="show_sql">true</prop> </props> </property> <!-- 映射文件 --> <property name="mappingResources"> <list> <value>com/bjsxt/entity/Emp.hbm.xml</value> </list> </property> </bean><!-- 配置事务 --><bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 配置事务范围 --> <tx:advice id="txManager" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="set*" propagation="REQUIRED" /> <tx:method name="add*" propagation="REQUIRED" /> <tx:method name="updata*" propagation="REQUIRED" /> <tx:method name="*" propagation="REQUIRED" read-only="true" /> </tx:attributes> </tx:advice> <bean id="empDao" class="com.bjsxt.dao.EmpDaoImpl" scope="prototype"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <bean id="empService" class="com.bjsxt.service.EmpServiceImpl" scope="prototype"> <property name="empDao" ref="empDao"></property> </bean> <bean id="empAction" class="com.bjsxt.action.EmpAction" scope="prototype"> <property name="empService" ref="empService"></property> </bean> <!-- 配置参与事务的类 --> <aop:config proxy-target-class="true"> <aop:pointcut id="aopMananger" expression="execution(* com.bjsxt.service.*Impl.*(..))" /> <aop:advisor advice-ref="txManager" pointcut-ref="aopMananger" /> </aop:config></beans>
Struts2.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> <!-- 这句非常重要,把控制权交给Spring --> <constant name="struts.objectFactory" value="spring" /> <constant name="struts.enable.DynamicMethodInvocation" value="false" /> <constant name="struts.devMode" value="true" /> <package name="default" namespace="/" extends="struts-default"> <!-- 因为控制器交给Spring了,所以我们不用自己再写类路径了,直接跟Spring要就行了,名字是在applicationContext.xml里配置的bean名字 --> <action name="UserAction" class="empAction" method="fandAll"> <result name="success">/index.html</result> </action> </package> <!-- <include file="example.xml" /> --> <!-- Add packages here --></struts>
EmpAction:
package com.bjsxt.action;import java.util.List;import com.bjsxt.service.EmpServiceImpl;public class EmpAction<T> { private Integer page; private Integer rows; private EmpServiceImpl<T> empService; public Integer getPage() { return page; } public void setPage(Integer page) { this.page = page; } public Integer getRows() { return rows; } public void setRows(Integer rows) { this.rows = rows; } public List<Object> fandAll(){ List<Object> l = (List<Object>) empService.fandAll(); return l; } public EmpServiceImpl<T> getEmpService() { return empService; } public void setEmpService(EmpServiceImpl<T> empService) { this.empService = empService; } }
EmpServiceImpl:
package com.bjsxt.service;import java.util.List;import com.bjsxt.dao.EmpDaoImpl;public class EmpServiceImpl<T> implements EmpServiceI<T> { private EmpDaoImpl<T> empDao; public EmpDaoImpl<T> getEmpDao() { return empDao; } public void setEmpDao(EmpDaoImpl<T> empDao) { this.empDao = empDao; } public List<T> fandAll() { empDao.fandAll(); return null; }}
EmpDaoImpl:
package com.bjsxt.dao;import java.util.List;import org.hibernate.Query;import org.hibernate.SessionFactory;public class EmpDaoImpl<T> implements EmpDaoI<T> { private SessionFactory sessionFactory; public SessionFactory getSessionFactory() { return sessionFactory; } public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } @SuppressWarnings("unchecked") @Override public List<T> fandAll() { System.out.println("testss"); Query q = sessionFactory.getCurrentSession().createQuery("from Emp e"); System.out.println(q); for(int i=0;i<q.list().size();i++){ System.out.println(q.list().get(i)); } return q.list(); }}
Emp.java
package com.bjsxt.entity;import java.util.Date;public class Emp { private Integer empno; private String ename; private String job; private Integer mgr; private Date hiredate; private Integer sal; private Integer comm; private Integer deptno; public Integer getEmpno() { return empno; } public void setEmpno(Integer empno) { this.empno = empno; } public String getEname() { return ename; } public void setEname(String ename) { this.ename = ename; } public String getJob() { return job; } public void setJob(String job) { this.job = job; } public Integer getMgr() { return mgr; } public void setMgr(Integer mgr) { this.mgr = mgr; } public Date getHiredate() { return hiredate; } public void setHiredate(Date hiredate) { this.hiredate = hiredate; } public Integer getSal() { return sal; } public void setSal(Integer sal) { this.sal = sal; } public Integer getComm() { return comm; } public void setComm(Integer comm) { this.comm = comm; } public Integer getDeptno() { return deptno; } public void setDeptno(Integer deptno) { this.deptno = deptno; }}
简单的映射文件Emp.hbm.xml
<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping package="com.bjsxt.entity"> <class name="Emp" table="EMP"> <id name="empno" column="EMPNO"></id> </class></hibernate-mapping>
至此已经完成,我还没用工作,如果哪里有问题请告诉我,谢谢
简单搭建ssh框架(xml配置)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。