日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > c/c++ >内容正文

c/c++

spring+springMvc+struts的SSH框架整合

發(fā)布時(shí)間:2023/12/9 c/c++ 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 spring+springMvc+struts的SSH框架整合 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1.建立一個(gè)web項(xiàng)目

2.導(dǎo)入SSH框架所需jar包

?

3.配置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>SSHDemo</display-name><!--直接訪問(wèn)項(xiàng)目時(shí),依次查找下面的jsp進(jìn)行顯示 --><welcome-file-list><welcome-file>login.jsp</welcome-file><welcome-file>index.html</welcome-file><welcome-file>index.htm</welcome-file><welcome-file>index.jsp</welcome-file><welcome-file>default.html</welcome-file><welcome-file>default.htm</welcome-file><welcome-file>default.jsp</welcome-file></welcome-file-list><!--上下文配置名字和路徑配置--><context-param><!-- 上下文參數(shù)名字 --><param-name>contextConfigLocation</param-name><!--上下文全局配置文件的位置,以根路徑作為開(kāi)始找 --><param-value>classpath:applicationContext.xml</param-value></context-param><!-- 定義監(jiān)聽(tīng)事件 --><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!-- 定義過(guò)濾的名字及其所在的地方 --><filter><filter-name>OpenSessionInViewFilter</filter-name><filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class></filter><!--定義需要攔截的后綴,只要滿足.action就會(huì)去查找對(duì)應(yīng)過(guò)濾器名字的攔截器實(shí)現(xiàn)類--><filter-mapping><filter-name>OpenSessionInViewFilter</filter-name><url-pattern>*.action</url-pattern></filter-mapping><!--定義struts的過(guò)濾器名字和實(shí)現(xiàn)類--><filter><filter-name>struts2</filter-name><filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class></filter><!--定義struts滿足路徑/*條件,就進(jìn)入到filter-name為struts2的filter-class進(jìn)行實(shí)現(xiàn) --><filter-mapping><filter-name>struts2</filter-name><url-pattern>/*</url-pattern></filter-mapping><!-- 定義會(huì)話超時(shí)時(shí)間 --><session-config><session-timeout>25</session-timeout></session-config></web-app>

4.配置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:p="http://www.springframework.org/schema/p"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.1.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-3.1.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-3.1.xsd"><!-- 加載Hibernate配置 --><!-- <bean id="sessionFactory"class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"><property name="configLocation"><value>classpath:hibernate.cfg.xml</value></property></bean> --><!-- 定義dbcp數(shù)據(jù)源 --><bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"><!-- 指定JDBC驅(qū)動(dòng)類 --><property name="driverClassName" value="com.mysql.jdbc.Driver"></property><!-- 提供連接數(shù)據(jù)庫(kù)的URL地址 --><property name="url" value="jdbc:mysql://localhost/spring?useSSL=false&amp;serverTimezone=UTC"></property><!-- 提供連接數(shù)據(jù)庫(kù)的用戶名和密碼 --><property name="username" value="root"></property><property name="password" value="root"></property></bean><!-- 定義SessionFactory Bean --><bean id="sessionFactory"class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"><!-- 為L(zhǎng)ocalSessionFactoryBean注入定義好的數(shù)據(jù)源 --><property name="dataSource"><ref bean="dataSource" /></property><!-- 添加Hibernate配置參數(shù) --><property name="hibernateProperties"><props><!-- 定義方言 --><prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop><!-- 定義運(yùn)行時(shí),是否在控制臺(tái)進(jìn)行將sql輸出 --><prop key="hibernate.show_sql">true</prop><!-- 定義運(yùn)行時(shí),是否在控制臺(tái)輸出的sql語(yǔ)句進(jìn)行格式化 --><prop key="hibernate.format_sql">true</prop></props></property><!-- 添加對(duì)象關(guān)系映射文件 --><property name="mappingDirectoryLocations"><list><value>classpath:com/sshdemo/entity/</value></list></property></bean><!-- 配置DAO --><bean id="employeeDao" class="com.sshdemo.dao.hibimpl.EmployeeDaoHibImpl"><property name="sessionFactory" ref="sessionFactory" /></bean><!-- 配置業(yè)務(wù)層 --><bean id="employeeService" class="com.sshdemo.service.impl.EmployeeServiceImpl"><property name="employeeDao" ref="employeeDao"></property></bean><!-- 定義事務(wù)管理器 --><bean id="txManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager"><property name="sessionFactory" ref="sessionFactory" /></bean><tx:advice id="txAdvice" transaction-manager="txManager"><tx:attributes><!-- read-only是表示調(diào)用這個(gè)方法時(shí)是否是子讀狀態(tài),一般只用于查詢,涉及更改時(shí)不要使用否則操作數(shù)據(jù)庫(kù)時(shí)會(huì)拋出異常并且不能對(duì)數(shù)據(jù)更改 --><tx:method name="find*" read-only="true" /><tx:method name="search*" read-only="true" /><tx:method name="query*" read-only="true" /><tx:method name="add*" propagation="REQUIRED" /><tx:method name="del*" propagation="REQUIRED" /><tx:method name="update*" propagation="REQUIRED" /><tx:method name="do*" propagation="REQUIRED" /><tx:method name="register*" propagation="REQUIRED" /><tx:method name="*" propagation="REQUIRED" read-only="true" /></tx:attributes></tx:advice><aop:config><!-- 定義切入點(diǎn),表示當(dāng)執(zhí)行com.sshdemo.service包下面所有的類的所有方法及其各種參數(shù)的方法時(shí)切入事務(wù)管理 --><aop:pointcut id="serviceMethod"expression="execution(* com.sshdemo.service.*.*(..))" /><!-- 將事務(wù)通知與切入點(diǎn)組合 --><aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethod" /></aop:config><tx:annotation-driven transaction-manager="txManager"/><!-- 控制層 (id為struts.xml中的class) 以下每個(gè)bean必須都要增加scope="prototype"屬性 --><bean id="userAction" class="com.sshdemo.action.UserAction"scope="prototype"><property name="employeeService" ref="employeeService"></property></bean></beans>

5.建立實(shí)體類及其對(duì)應(yīng)的xml文件

package com.sshdemo.entity;/*** 員工 實(shí)體類。 */ public class Employee implements java.io.Serializable {// Fieldsprivate static final long serialVersionUID = 5106663630382037556L;private String sn;private Position position;private Department department;private String password;private String name;private String status;// Constructors/** default constructor */public Employee() {}/** full constructor */public Employee(Position position, Department department, String password,String name, String status) {this.position = position;this.department = department;this.password = password;this.name = name;this.status = status;}// Property accessors/*** @return 工號(hào)*/public String getSn() {return this.sn;}public void setSn(String sn) {this.sn = sn;}/*** @return 職務(wù)*/public Position getPosition() {return this.position;}public void setPosition(Position position) {this.position = position;}/*** @return 部門*/public Department getDepartment() {return this.department;}public void setDepartment(Department department) {this.department = department;}/*** @return 密碼*/public String getPassword() {return this.password;}public void setPassword(String password) {this.password = password;}/*** @return 姓名*/public String getName() {return this.name;}public void setName(String name) {this.name = name;}/*** @return 狀態(tài)*/public String getStatus() {return this.status;}public void setStatus(String status) {this.status = status;} }

創(chuàng)建employee對(duì)應(yīng)的xml文件:Employee.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 package="com.sshdemo.entity"><class name="Employee" table="sys_employee"><id name="sn" type="string"><column length="20" name="sn" /><generator class="assigned" /></id><many-to-one class="Position" fetch="select" name="position"><column name="position_id" not-null="false"><comment>職務(wù)編號(hào)</comment></column></many-to-one><many-to-one class="Department" fetch="select" name="department"><column name="department_id" not-null="false"><comment>部門</comment></column></many-to-one><property lazy="false" name="password"type="string"><column length="45" name="password" not-null="true"><comment>密碼</comment></column></property><property lazy="false" name="name" type="string"><column length="45" name="name" not-null="true"><comment>姓名</comment></column></property><property lazy="false" name="status" type="string"><column length="20" name="status" not-null="true"><comment>狀態(tài)</comment></column></property></class> </hibernate-mapping>

創(chuàng)建部門的實(shí)體表和xml文件

package com.sshdemo.entity;/*** 部門 實(shí)體類。 */ public class Department implements java.io.Serializable {// Fieldsprivate static final long serialVersionUID = 5073258499319872911L;private Integer id;private Employee manager;private String name;// Constructors/** default constructor */public Department() {}/** minimal constructor */public Department(String name) {this.name = name;}/** full constructor */public Department(Employee manager, String name) {this.manager = manager;this.name = name;}// Property accessors/*** @return 部門編號(hào)*/public Integer getId() {return this.id;}public void setId(Integer id) {this.id = id;}/*** @return 部門經(jīng)理*/public Employee getManager() {return this.manager;}public void setManager(Employee manager) {this.manager = manager;}/*** @return 部門名稱*/public String getName() {return this.name;}public void setName(String name) {this.name = name;} } <?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"><!--package指定的是映射的實(shí)體類的位置路徑 --> <hibernate-mapping package="com.sshdemo.entity"><!-- name指定實(shí)體類的名字,table定義數(shù)據(jù)庫(kù)中建立的表的名字 --><class name="Department" table="sys_department"><id name="id" type="integer"><!-- 這個(gè)地方定義的是數(shù)據(jù)庫(kù)中對(duì)應(yīng)實(shí)體類的列名,可以不定義,它的生成就會(huì)和實(shí)體類一樣 --><column name="id" /><!-- 主鍵的增長(zhǎng)方式,native表示自增長(zhǎng) --><generator class="native" /></id><many-to-one name="manager" class="Employee" fetch="select"><column name="manager_sn" length="20"><comment>部門經(jīng)理</comment></column></many-to-one><property name="name" type="string"><column name="name" length="45" not-null="true"><comment>部門名稱</comment></column></property></class> </hibernate-mapping>

創(chuàng)建Dictionary

package com.sshdemo.entity;/*** 數(shù)據(jù)字典 實(shí)體類。 */ public class Dictionary implements java.io.Serializable {// Fieldsprivate static final long serialVersionUID = -3482598030856972288L;private long id;private String type;private String item;private String value;// Constructors/** default constructor */public Dictionary() {}/** full constructor */public Dictionary(String type, String item, String value) {this.type = type;this.item = item;this.value = value;}// Property accessors/*** @return 編號(hào)*/public long getId() {return this.id;}public void setId(long id) {this.id = id;}/*** @return 類型*/public String getType() {return this.type;}public void setType(String type) {this.type = type;}/*** @return 條目*/public String getItem() {return this.item;}public void setItem(String item) {this.item = item;}/*** @return*/public String getValue() {return this.value;}public void setValue(String value) {this.value = value;}} <?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 package="com.sshdemo.entity"><class name="Dictionary" table="sys_dictionary"><id name="id" type="long"><column name="id" /><generator class="native" /></id><property name="type" type="string"><column name="type" length="20" not-null="true" /></property><property name="item" type="string"><column name="item" length="20" not-null="true" /></property><property name="value" type="string"><column name="value" length="20" not-null="true" /></property></class> </hibernate-mapping>

創(chuàng)建positon類和xml

package com.sshdemo.entity;/*** 職務(wù) 實(shí)體類。 */ public class Position implements java.io.Serializable {// Fieldsprivate static final long serialVersionUID = 4107962667586915867L;private Integer id;private String nameCn;private String nameEn;// Constructors/** default constructor */public Position() {}/** minimal constructor */public Position(String nameCn, String nameEn) {this.nameCn = nameCn;this.nameEn = nameEn;}// Property accessors/*** @return 職務(wù)編號(hào)*/public Integer getId() {return this.id;}public void setId(Integer id) {this.id = id;}/*** @return 職務(wù)名稱(中文)*/public String getNameCn() {return this.nameCn;}public void setNameCn(String nameCn) {this.nameCn = nameCn;}/*** @return 職務(wù)名稱(英文)*/public String getNameEn() {return this.nameEn;}public void setNameEn(String nameEn) {this.nameEn = nameEn;}} <?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 package="com.sshdemo.entity"><class name="Position" table="sys_position"><id name="id" type="integer"><column name="id" /><generator class="native" /></id><property name="nameCn" type="string"><column name="name_cn" length="45" not-null="true"><comment>職務(wù)名稱(中文)</comment></column></property><property name="nameEn" type="string"><column name="name_en" length="45" not-null="true"><comment>職務(wù)名稱(英文)</comment></column></property></class> </hibernate-mapping>

創(chuàng)建CheckResult類和xml文件

package com.sshdemo.entity;import java.util.Date;/*** 審核結(jié)果 實(shí)體類。 */ public class CheckResult implements java.io.Serializable {// Fieldsprivate static final long serialVersionUID = 8366759716422180617L;private long id;private String sheetType;private long sheetId;private Date checkTime;private String type;private Employee checker;private String result;private String comment;// Constructors/** default constructor */public CheckResult() {}/** minimal constructor */public CheckResult(String sheetType, long sheetId, Date checkTime,String type, Employee checker, String result) {this.sheetType = sheetType;this.sheetId = sheetId;this.checkTime = checkTime;this.type = type;this.checker = checker;this.result = result;}/** full constructor */public CheckResult(String sheetType, long sheetId, Date checkTime,String type, Employee checker, String result, String comment) {this.sheetType = sheetType;this.sheetId = sheetId;this.checkTime = checkTime;this.type = type;this.checker = checker;this.result = result;this.comment = comment;}// Property accessors/*** @return 編號(hào)*/public long getId() {return this.id;}public void setId(long id) {this.id = id;}/*** @return 單據(jù)類型*/public String getSheetType() {return this.sheetType;}public void setSheetType(String sheetType) {this.sheetType = sheetType;}/*** @return 單據(jù)編號(hào)*/public long getSheetId() {return this.sheetId;}public void setSheetId(long sheetId) {this.sheetId = sheetId;}/*** @return 審核時(shí)間*/public Date getCheckTime() {return this.checkTime;}public void setCheckTime(Date checkTime) {this.checkTime = checkTime;}/*** @return 審核類別*/public String getType() {return this.type;}public void setType(String type) {this.type = type;}/*** @return 審核人*/public Employee getChecker() {return this.checker;}public void setChecker(Employee checker) {this.checker = checker;}/*** @return 審核結(jié)果*/public String getResult() {return this.result;}public void setResult(String result) {this.result = result;}/*** @return 審核意見(jiàn)*/public String getComment() {return this.comment;}public void setComment(String comment) {this.comment = comment;}} <?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 package="com.sshdemo.entity"><class name="CheckResult" table="biz_check_result"><id name="id" type="long"><column name="id" /><generator class="native" /></id><property name="sheetType" type="string"><column name="sheet_type" length="20" not-null="true"><comment>單據(jù)類型</comment></column></property><property name="sheetId" type="long"><column name="sheet_id" not-null="true"><comment>單據(jù)編號(hào)</comment></column></property><property name="checkTime" type="date"><column name="check_time" length="19" not-null="true"><comment>審核時(shí)間</comment></column></property><property name="type" type="string"><column name="type" length="20" not-null="true"><comment>審核類型</comment></column></property><many-to-one name="checker" class="Employee" fetch="select"><column name="checker_sn" length="20" not-null="true"><comment>審核人</comment></column></many-to-one><property name="result" type="string"><column name="result" length="20" not-null="true"><comment>審核結(jié)果</comment></column></property><property name="comment" type="string"><column name="comment"><comment>審核意見(jiàn)</comment></column></property></class> </hibernate-mapping>

創(chuàng)建ClaimVoucher類

package com.sshdemo.entity;import java.util.Date; import java.util.HashSet; import java.util.Set;/*** 報(bào)銷申請(qǐng)單 實(shí)體類。 */ public class ClaimVoucher implements java.io.Serializable {// Fieldsprivate static final long serialVersionUID = -1763618295413371212L;private long id;private Employee nextDealBy;private Employee creator;private Date createTime;private String event;private double totalAccount;private String status;private Set<ClaimVoucherDetail> details = new HashSet<ClaimVoucherDetail>(0);// Constructors /** default constructor */public ClaimVoucher() {}/** minimal constructor */public ClaimVoucher(Employee employeeByCreateSn, Date createTime,String event, double totalAccount, String status) {this.creator = employeeByCreateSn;this.createTime = createTime;this.event = event;this.totalAccount = totalAccount;this.status = status;}/** full constructor */public ClaimVoucher(Employee employeeByNextDealSn,Employee employeeByCreateSn, Date createTime, String event,double totalAccount, String status, Set<ClaimVoucherDetail> claimVoucherDetails) {this.nextDealBy = employeeByNextDealSn;this.creator = employeeByCreateSn;this.createTime = createTime;this.event = event;this.totalAccount = totalAccount;this.status = status;this.details = claimVoucherDetails;}// Property accessors/*** @return 編號(hào)*/public long getId() {return this.id;}public void setId(long id) {this.id = id;}/*** @return 待處理人*/public Employee getNextDealBy() {return this.nextDealBy;}public void setNextDealBy(Employee employeeByNextDealSn) {this.nextDealBy = employeeByNextDealSn;}/*** @return 填報(bào)人*/public Employee getCreator() {return this.creator;}public void setCreator(Employee employeeByCreateSn) {this.creator = employeeByCreateSn;}/*** @return 填寫時(shí)間*/public Date getCreateTime() {return this.createTime;}public void setCreateTime(Date createTime) {this.createTime = createTime;}/*** @return 事由*/public String getEvent() {return this.event;}public void setEvent(String event) {this.event = event;}/*** @return 總金額*/public double getTotalAccount() {return this.totalAccount;}public void setTotalAccount(double totalAccount) {this.totalAccount = totalAccount;}/*** @return 狀態(tài)*/public String getStatus() {return this.status;}public void setStatus(String status) {this.status = status;}/*** @return 明細(xì)*/public Set<ClaimVoucherDetail> getDetails() {return this.details;}public void setDetails(Set<ClaimVoucherDetail> claimVoucherDetails) {this.details = claimVoucherDetails;}} <?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 package="com.sshdemo.entity"><class name="ClaimVoucher" table="biz_claim_voucher"><id name="id" type="long"><column name="id" /><generator class="native" /></id><many-to-one name="nextDealBy" class="Employee" fetch="select"><column name="next_deal_sn" length="20"><comment>待處理人</comment></column></many-to-one><many-to-one name="creator" class="Employee" fetch="select"><column name="create_sn" length="20" not-null="true"><comment>填報(bào)人</comment></column></many-to-one><property name="createTime" type="date"><column name="create_time" length="19" not-null="true"><comment>填寫時(shí)間</comment></column></property><property name="event" type="string"><column name="event" not-null="true"><comment>事由</comment></column></property><property name="totalAccount" type="double"><column name="total_account" precision="10" not-null="true"><comment>總金額</comment></column></property><property name="status" type="string"><column name="status" length="20" not-null="true"><comment>狀態(tài)</comment></column></property><set name="details" inverse="true"><key><column name="main_id" not-null="true"><comment>明細(xì)</comment></column></key><one-to-many class="ClaimVoucherDetail" /></set></class> </hibernate-mapping>

創(chuàng)建ClaimVoucherDetail類和xml

package com.sshdemo.entity;/*** 報(bào)銷申請(qǐng)單明細(xì) 實(shí)體類。 */ public class ClaimVoucherDetail implements java.io.Serializable {// Fieldsprivate static final long serialVersionUID = -5640087904678377183L;private long id;private ClaimVoucher master;private String item;private double account;private String desc;// Constructors/** default constructor */public ClaimVoucherDetail() {}/** full constructor */public ClaimVoucherDetail(ClaimVoucher claimVoucher, String item,double account, String desc) {this.master = claimVoucher;this.item = item;this.account = account;this.desc = desc;}// Property accessors/*** @return 編號(hào)*/public long getId() {return this.id;}public void setId(long id) {this.id = id;}/*** @return 主單*/public ClaimVoucher getMaster() {return this.master;}public void setMaster(ClaimVoucher claimVoucher) {this.master = claimVoucher;}/*** @return 項(xiàng)目*/public String getItem() {return this.item;}public void setItem(String item) {this.item = item;}/*** @return 金額*/public double getAccount() {return this.account;}public void setAccount(double account) {this.account = account;}/*** @return 費(fèi)用說(shuō)明*/public String getDesc() {return this.desc;}public void setDesc(String desc) {this.desc = desc;}} <?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 package="com.sshdemo.entity"><class name="ClaimVoucherDetail" table="biz_claim_voucher_detail"><id name="id" type="long"><column name="id" /><generator class="native" /></id><many-to-one name="master" class="ClaimVoucher" fetch="select"><column name="main_id" not-null="true"><comment>報(bào)銷單(主單)編號(hào)</comment></column></many-to-one><property name="item" type="string"><column name="item" length="20" not-null="true"><comment>項(xiàng)目</comment></column></property><property name="account" type="double"><column name="account" precision="10" not-null="true"><comment>金額</comment></column></property><property name="desc" type="string"><column name="desc" length="200" not-null="true"><comment>費(fèi)用說(shuō)明</comment></column></property></class> </hibernate-mapping>

6.創(chuàng)建com.sshdemo.dao的employeeDao接口

package com.sshdemo.dao;import java.io.Serializable; import java.util.List;import com.sshdemo.entity.Employee;/*** DAO接口。* */ public interface EmployeeDao {/*** 添加用戶* * @param employee 用戶*/void add(Employee employee);/*** 根據(jù)用戶Id刪除對(duì)象* @param id主鍵*/void deleteById(Serializable id);/*** 修改用戶* * @param employee用戶*/void update(Employee employee);/*** 根據(jù)用戶id加載對(duì)象* @param id主鍵* @return 返回用戶對(duì)象*/Employee get(Serializable id);/*** 根據(jù)查詢條件查詢用戶數(shù)據(jù)。* @param condition查詢條件* @return 如果condition為null,返回所有用戶數(shù)據(jù),否則,使用用戶名和密碼查詢用戶數(shù)據(jù)*/List<Employee> find(Employee condition); }

7.創(chuàng)建對(duì)應(yīng)的實(shí)現(xiàn)類

package com.sshdemo.dao.hibimpl;import java.io.Serializable; import java.util.List;import org.springframework.orm.hibernate3.HibernateTemplate; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import org.springframework.transaction.annotation.Transactional;import com.sshdemo.dao.EmployeeDao; import com.sshdemo.entity.Employee;/*** 這個(gè)是員工類的dao層,繼承HibernateDaoSupport,可以按ctrl+F3* 可以看到這個(gè)類中包含一個(gè)getHibernateTemplate()和setHibernateTemplate()方法* 我們通過(guò)上下文配置時(shí)就已經(jīng)將template用spring進(jìn)行了注入,此時(shí)我們就通過(guò)* teaplate可以獲取到相應(yīng)的數(shù)據(jù)源,使用template進(jìn)行調(diào)用對(duì)應(yīng)的增刪改查* */ public class EmployeeDaoHibImpl extends HibernateDaoSupport implementsEmployeeDao {/** (non-Javadoc)* * @see com.sshdemo.dao.EmployeeDao#add(com.sshdemo.entity.Employee)*/@Transactionalpublic void add(Employee employee) {HibernateTemplate hibTemp = getHibernateTemplate();// hibTemp.setFlushMode(HibernateTemplate.FLUSH_AUTO); hibTemp.save(employee);}/** (non-Javadoc)* * @see com.sshdemo.dao.EmployeeDao#deleteById(java.io.Serializable)*/public void deleteById(Serializable id) {super.getHibernateTemplate().delete(this.get(id));}/** (non-Javadoc)* * @see com.sshdemo.dao.EmployeeDao#update(com.sshdemo.entity.Employee)*/public void update(Employee employee) {super.getHibernateTemplate().update(employee);}/** (non-Javadoc)* * @see com.sshdemo.dao.EmployeeDao#get(java.io.Serializable)*/public Employee get(Serializable id) {return (Employee) super.getHibernateTemplate().get(Employee.class, id);}/** (non-Javadoc)* * @see com.sshdemo.dao.EmployeeDao#find(com.sshdemo.entity.Employee)*/@SuppressWarnings("unchecked")public List<Employee> find(Employee condition) {return getHibernateTemplate().find("from Employee e where e.name=? and e.password=?",new Object[] { condition.getName(), condition.getPassword() });} }

8.在com.sshdemo.utils下定義一個(gè)MD5加密算法的類

package com.sshdemo.utils;import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.security.MessageDigest;public class MD5 {private String inStr;//輸入java.security.MessageDigest下面的一個(gè)類private MessageDigest md5;/*** 以下是一個(gè)帶參數(shù)的構(gòu)造函數(shù),創(chuàng)建對(duì)象時(shí)就將* inStr和MessageDigest賦值* @param 楊超*/public MD5(String inStr) {this.inStr = inStr;try {this.md5 = MessageDigest.getInstance("MD5");} catch (Exception e) {System.out.println(e.toString());e.printStackTrace();}}/* 下面是關(guān)鍵的md5算法 */public String compute() {//將創(chuàng)建對(duì)象時(shí)的字符串轉(zhuǎn)換成字符數(shù)組char[] charArray = this.inStr.toCharArray();//根據(jù)字符數(shù)組的長(zhǎng)度初始化一個(gè)Byte數(shù)組byte[] byteArray = new byte[charArray.length];//將字符數(shù)組轉(zhuǎn)換成byte數(shù)組for (int i = 0; i < charArray.length; i++)byteArray[i] = (byte) charArray[i];//將字節(jié)數(shù)組通過(guò)digest 計(jì)算存儲(chǔ)在byte數(shù)組中byte[] md5Bytes = this.md5.digest(byteArray);//用它來(lái)保證線程安全StringBuffer hexValue = new StringBuffer();for (int i = 0; i < md5Bytes.length; i++) {//將每個(gè)md5Bytes進(jìn)行一個(gè)或的位運(yùn)算符計(jì)算,最終將其轉(zhuǎn)換成二進(jìn)制數(shù)字int val = ((int) md5Bytes[i]) & 0xff;if (val < 16)hexValue.append("0");hexValue.append(Integer.toHexString(val));}//返回一個(gè)字符串return hexValue.toString();}/* 下面是主函數(shù)調(diào)用 */public static void main(String[] args) {String A = null;try {System.out.println("請(qǐng)輸入你要加密的數(shù)據(jù):");BufferedReader br = new BufferedReader(new InputStreamReader(System.in));A = br.readLine();} catch (IOException e) {};MD5 md5 = new MD5(A);String postString = md5.compute();System.out.println("加密后的數(shù)據(jù):" + postString);} }

?

9.創(chuàng)建MVC設(shè)計(jì)模式的C層

package com.sshdemo.action;import java.util.Map;import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; import com.sshdemo.entity.Employee; import com.sshdemo.service.EmployeeService; import com.sshdemo.utils.MD5;/*** 用戶登錄action。* */ public class UserAction extends ActionSupport {private static final long serialVersionUID = -6095372451084071851L;private Employee employee = null;private EmployeeService employeeService = null;/*** 用戶登錄。* * @return* @throws Exception*/public String login() throws Exception {Employee newEmployee = null;try {// 對(duì)登錄密碼進(jìn)行MD5加密employee.setPassword(new MD5(employee.getPassword()).compute());newEmployee = employeeService.login(employee);} catch (Exception e) {this.addActionMessage(e.getMessage());}String ret = INPUT;if (newEmployee == null) {ret = INPUT;} else {Map<String, Object> session = ActionContext.getContext().getSession();session.put("employee", newEmployee);String nameCn = newEmployee.getPosition().getNameCn();if ("普通員工".equals(nameCn)) {ret = "staff";} else if ("部門經(jīng)理".equals(nameCn)) {ret = "deptManager";} else if ("總經(jīng)理".equals(nameCn)) {ret = "manager";} else if ("財(cái)務(wù)".equals(nameCn)) {ret = "cashier";}}return ret;}/*** 用戶退出。* * @return* @throws Exception*/public String logout() throws Exception {ActionContext ac = ActionContext.getContext();ac.getSession().remove("employee");return SUCCESS;}public String register() throws Exception{String ret = "registerfail";try {// 對(duì)登錄密碼進(jìn)行MD5加密employee.setPassword(new MD5(employee.getPassword()).compute());employeeService.register(employee);ret = "registerok";} catch (Exception e) {this.addActionMessage(e.getMessage());}return ret;}public void setEmployee(Employee employee) {this.employee = employee;}public void setEmployeeService(EmployeeService employeeService) {this.employeeService = employeeService;}public Employee getEmployee() {return employee;}public EmployeeService getEmployeeService() {return employeeService;} }

10.創(chuàng)建一個(gè)ExportDb創(chuàng)建在各實(shí)體類中定義好的xml對(duì)應(yīng)的數(shù)據(jù)庫(kù)表格

package com.sshdemo.utils;import org.hibernate.cfg.Configuration; import org.hibernate.tool.hbm2ddl.SchemaExport;; /*** 將hbm生成ddl* @author BCH**/ public class ExportDb {public static void main(String[] args) {//默認(rèn)讀取hibernate.cfg.xml文件Configuration cfr = new Configuration().configure();SchemaExport export = new SchemaExport(cfr);export.create(true, true);} }

11.創(chuàng)建對(duì)應(yīng)的jsp

login.jsp

<%@ page language="java" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><head><title>登錄</title></head><body><h1>登錄</h1><s:actionmessage /><s:form action="login"><s:textfield name="employee.name" label="用戶名" /><s:password name="employee.password" label="密碼" /><s:submit value="登錄"></s:submit></s:form></body> </html> <%@ taglib prefix="s" uri="/struts-tags"%>,這里引入了一個(gè)jsp腳本的指令,定義了一個(gè)s前綴
注意:這里對(duì)應(yīng)的name=employee.name;這里對(duì)應(yīng)的是實(shí)體類中name和password屬性
register.jsp <%@ page language="java" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><head><title>注冊(cè)</title></head><body><h1>注冊(cè)</h1><s:actionmessage /><s:form action="register"><s:textfield name="employee.sn" label="序號(hào)" /><s:textfield name="employee.name" label="用戶名" /><s:password name="employee.password" label="密碼" /><s:textfield name="employee.status" label="狀態(tài)" /><s:submit value="注冊(cè)"></s:submit></s:form></body> </html>

manager,jsp

?

<%@ page language="java" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><head><title>總經(jīng)理待審核頁(yè)</title></head><body><h1>總經(jīng)理待審核頁(yè)</h1><%if (request.getSession().getAttribute("employee") != null){%><h1>${sessionScope.employee.name } 登錄成功</h1><%} else {%><h1>你還沒(méi)登錄,請(qǐng)您先登錄。</h1><%}%><br><a href="login.jsp">繼續(xù)操作</a></body> </html>

staff.jsp

<%@ page language="java" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><head><title>添加報(bào)銷單頁(yè)面</title></head><body><h1>添加報(bào)銷單頁(yè)面</h1><%if (request.getSession().getAttribute("employee") != null){%><h1>${sessionScope.employee.name } 登錄成功</h1><%} else {%><h1>你還沒(méi)登錄,請(qǐng)您先登錄。</h1><%}%><br><a href="login.jsp">繼續(xù)操作</a></body> </html>

cashier.jsp

<%@ page language="java" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><head><title>財(cái)務(wù)處理頁(yè)</title></head><body><h1>財(cái)務(wù)處理頁(yè)</h1><%if (request.getSession().getAttribute("employee") != null){%><h1>${sessionScope.employee.name } 登錄成功</h1><%} else {%><h1>你還沒(méi)登錄,請(qǐng)您先登錄。</h1><%}%><br><a href="login.jsp">繼續(xù)操作</a></body> </html>

deptManager.jsp

<%@ page language="java" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><head><title>部門經(jīng)理待審核頁(yè)</title></head><body><h1>部門經(jīng)理待審核頁(yè)</h1><%if (request.getSession().getAttribute("employee") != null){%><h1>${sessionScope.employee.name } 登錄成功</h1><%} else {%><h1>你還沒(méi)登錄,請(qǐng)您先登錄。</h1><%}%><br><a href="login.jsp">繼續(xù)操作</a></body> </html>

運(yùn)行原理以login.jsp為例:

直接在eclipse上運(yùn)行即可:點(diǎn)擊登錄后—后將表單提交到action后面對(duì)應(yīng)的login去—此時(shí)會(huì)去struts.xml文件里面找到name=login的action,然后就會(huì)根據(jù)class找到對(duì)應(yīng)業(yè)務(wù)處理的類的位置及其對(duì)應(yīng)的Method方法

—由于在jsp定義了對(duì)應(yīng)的name=employee.XX它就會(huì)去找實(shí)體類對(duì)應(yīng)的屬性,此時(shí)它對(duì)employee就已經(jīng)做了一個(gè)封裝,將前端傳回的參數(shù)new 成了一個(gè)對(duì)象,我們拿到對(duì)象之后,就可以對(duì)它進(jìn)行一系列的操作

并返回不同的結(jié)果—此時(shí)結(jié)果又會(huì)回到struts.xml中的<result name="input">/login.jsp</result>,它會(huì)根據(jù)不同的結(jié)果判斷顯示出不同的結(jié)果

?

其它注冊(cè)等原理一樣,不再贅述

?

總結(jié):對(duì)應(yīng)SSH各部分所起到的作用:spring起一個(gè)鏈接的作用,使得代碼之間的耦合性降低,如數(shù)據(jù)源的配置sqlSessionFactory、對(duì)應(yīng)的template的注入等

  struts取代了servlet,通過(guò)jsp上的指令映射到struts配置文件,然后再映射到對(duì)應(yīng)業(yè)務(wù)邏輯處理所在的位置,根據(jù)返回結(jié)果,顯示出不同的視圖

  Hibernate它起到的作用不用去管JDBC實(shí)現(xiàn),直接對(duì)對(duì)象進(jìn)行操作,最終會(huì)根據(jù)它的HQL語(yǔ)句對(duì)數(shù)據(jù)庫(kù)實(shí)現(xiàn)增刪改查

?

轉(zhuǎn)載于:https://www.cnblogs.com/fly-boy/p/7508035.html

總結(jié)

以上是生活随笔為你收集整理的spring+springMvc+struts的SSH框架整合的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。