国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

簡單搭建ssh框架(xml配置)

2019-11-14 21:46:53
字體:
來源:轉載
供稿:網友
簡單搭建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>

至此已經完成,我還沒用工作,如果哪里有問題請告訴我,謝謝


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 罗城| 阳信县| 砚山县| 蓝山县| 尼玛县| 永年县| 武鸣县| 延庆县| 化德县| 聂荣县| 梁河县| 宜兰市| 道真| 云和县| 闵行区| 宜都市| 和平县| 萍乡市| 吉林市| 兰西县| 贵港市| 温州市| 贵定县| 海盐县| 义马市| 隆林| 和龙市| 滨州市| 连城县| 沾化县| 锦州市| 巴林左旗| 荆州市| 揭阳市| 汕头市| 望江县| 玉林市| 揭西县| 自贡市| 乌恰县| 丹阳市|