spring_SSH整合之_架構的曆史(架構整合JSP+Hibernate+Spring+Struts2)_7

來源:互聯網
上載者:User


hibernate-3.6.0+struts2-2.3.32+spring4.0.0+所用的包如下:

antlr-2.7.6.jar
aopalliance-1.0.jar
aspectjrt.jar
aspectjweaver.jar
c3p0-0.9.1.jar
commons-collections-3.1.jar
commons-dbcp-1.4.jar
commons-fileupload-1.3.2.jar
commons-io-2.2.jar
commons-lang3-3.2.jar
commons-logging-1.1.3.jar
commons-pool-1.6.jar
dom4j-1.6.1.jar
freemarker-2.3.22.jar
hibernate-jpa-2.0-api-1.0.0.Final.jar
hibernate3.jar
javassist-3.12.0.GA.jar
jta-1.1.jar
mysql-connector-java-5.1.42-bin.jar
ognl-3.0.19.jar
slf4j-api-1.6.1.jar
spring-aop-4.0.0.RELEASE.jar
spring-aspects-4.0.0.RELEASE.jar
spring-beans-4.0.0.RELEASE.jar
spring-context-4.0.0.RELEASE.jar
spring-core-4.0.0.RELEASE.jar
spring-expression-4.0.0.RELEASE.jar
spring-framework-bom-4.0.0.RELEASE.jar
spring-jdbc-4.0.0.RELEASE.jar
spring-orm-4.0.0.RELEASE.jar
spring-tx-4.0.0.RELEASE.jar
spring-web-4.0.0.RELEASE.jar
struts2-core-2.3.32.jar
struts2-spring-plugin-2.3.32.jar
xwork-core-2.3.32.jar

SSH代碼:

User.java

package com.bjsxt.registration.model;//貧血模型 充血模型public class User {private Integer id;private String username;private String password;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}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;}}


User.hbm.xml
<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><!-- This mapping demonstrates how to use an "inverse" join to map an association table as an association with one-to-many multiplicity at the level of the object model. --><hibernate-mapping package="com.bjsxt.registration.model"><class name="User" table="user"><id name="id" type="integer" column="id"><generator class="native" /></id><property name="username" type="string" column="username"/><property name="password" type="string" column="password"/></class></hibernate-mapping>


UserDao.java
package com.bjsxt.registration.dao;import com.bjsxt.registration.model.User;public interface UserDao {/** 儲存資料 */public void save(User user);/** 查詢某個資料是否存在 */public boolean checkUserExistsWithName(String username);}


UserDaoImpl.java
package com.bjsxt.registration.dao.impl;import java.util.List;import org.springframework.orm.hibernate3.HibernateTemplate;import com.bjsxt.registration.dao.UserDao;import com.bjsxt.registration.model.User;public class UserDaoImpl implements UserDao {private HibernateTemplate hibernateTemplate;public HibernateTemplate getHibernateTemplate() {return hibernateTemplate;}public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {this.hibernateTemplate = hibernateTemplate;}public void save(User user) {hibernateTemplate.save(user);}public boolean checkUserExistsWithName(String username) {List<Object> users = hibernateTemplate.find("from User u where u.username='" + username + "'");if (users != null && users.size() > 0) {return true;}return false;}}

UserManager.java
package com.bjsxt.registration.service;import com.bjsxt.registration.model.User;public interface UserManager {/** 判斷是否存在 */public boolean exists(User user);/** 增加資料 */public void add(User user);}



UserManagerImpl.java
package com.bjsxt.registration.service.impl;import com.bjsxt.registration.dao.UserDao;import com.bjsxt.registration.model.User;import com.bjsxt.registration.service.UserManager;public class UserManagerImpl implements UserManager {private UserDao userDao;public UserDao getUserDao() {return userDao;}public void setUserDao(UserDao userDao) {this.userDao = userDao;}public boolean exists(User user) {return userDao.checkUserExistsWithName(user.getUsername());}public void add(User user) {userDao.save(user);}}



UserAction.java
package com.bjsxt.registration.action;import com.bjsxt.registration.model.User;import com.bjsxt.registration.service.UserManager;import com.opensymphony.xwork2.ActionSupport;@SuppressWarnings("serial")public class UserAction extends ActionSupport {private String username;private String password;private String password2;private UserManager userManager;public String getPassword2() {return password2;}public void setPassword2(String password2) {this.password2 = password2;}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;}public UserManager getUserManager() {return userManager;}public void setUserManager(UserManager userManager) {this.userManager = userManager;}@Overridepublic String execute() throws Exception {User user = new User();user.setUsername(username);user.setpassword(password);if (userManager.exists(user)) {return "FAIL";}userManager.add(user);return "SUCCESS";}}

jdbc.properties
jdbc.driverClassName=com.mysql.jdbc.Driverjdbc.url=jdbc\:mysql\://localhost\:3306/springjdbc.username=rootjdbc.password=root

beans.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:context="http://www.springframework.org/schema/context"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.xsd        http://www.springframework.org/schema/context        http://www.springframework.org/schema/context/spring-context.xsd        http://www.springframework.org/schema/aop        http://www.springframework.org/schema/aop/spring-aop.xsd        http://www.springframework.org/schema/tx        http://www.springframework.org/schema/tx/spring-tx.xsd"><!-- 依賴注入UserDaoImpl --><bean id="userDaoImpl" class="com.bjsxt.registration.dao.impl.UserDaoImpl"><property name="hibernateTemplate" ref="hibernateTemplate" /></bean><!-- 依賴注入UserManagerImpl --><bean id="userManagerImpl" class="com.bjsxt.registration.service.impl.UserManagerImpl"><property name="userDao" ref="userDaoImpl" /></bean><!-- 依賴注入UserAction,每次訪問都建立一個新執行個體 --><bean id="userAction" class="com.bjsxt.registration.action.UserAction" scope="prototype"><property name="userManager" ref="userManagerImpl" /></bean><!-- 配置DataSource --><beanclass="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="locations"><value>classpath:jdbc.properties</value></property></bean><bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"destroy-method="close"><property name="driverClassName" value="${jdbc.driverClassName}" /><property name="url" value="${jdbc.url}" /><property name="username" value="${jdbc.username}" /><property name="password" value="${jdbc.password}" /></bean><!-- 配置SessionFactory --><bean id="sessionFactory"class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"><property name="dataSource" ref="dataSource" /><property name="mappingDirectoryLocations"><list><value>classpath:com/bjsxt/registration/model/</value></list></property><property name="hibernateProperties"><props><!-- 顯示SQL語句 --><prop key="hibernate.show_sql">true</prop><!-- 自動構建表結構 --><prop key="hibernate.hbm2ddl.auto">update</prop></props></property></bean><!-- 配置HibernateTeimplate --><bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"><property name="sessionFactory" ref="sessionFactory" /></bean><!-- 配置事務txManager --><bean id="txMeanage"class="org.springframework.orm.hibernate3.HibernateTransactionManager"><property name="sessionFactory" ref="sessionFactory" /></bean><!-- 配置事務範圍txAdvisor --><tx:advice id="txAdvice" transaction-manager="txMeanage"><tx:attributes><tx:method name="exists" read-only="true" /><tx:method name="add*" propagation="REQUIRED" /></tx:attributes></tx:advice><!-- 配置AOP範圍 --><aop:config><aop:pointcutexpression="execution(public * com.bjsxt.registration.service..*.*(..))"id="pointcut" /><aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut" /></aop:config></beans>

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><!-- 指定開啟.jsp頁面預設編碼 --><constant name="struts.i18n.encoding" value="UTF-8" /><!-- 與spring集合時,指定由spring負責action對象的建立(自動裝配) --><constant name="struts.objectFactory.spring.autoWire" value="type" /><package name="registration" namespace="/bjsxt" extends="struts-default"><action name="user_*" class="com.bjsxt.registration.action.UserAction"method="{1}"><result name="SUCCESS" type="redirect">/registSuccess.jsp</result><result name="FAIL" type="redirect">/registFail.jsp</result></action></package></struts>

web.xml
<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"><!-- Spring監聽器 --><context-param><param-name>contextConfigLocation</param-name><!-- 去classpath下找一個叫beans.xml檔案 --><param-value>classpath:beans.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!-- 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><!-- 預設頁面 --><welcome-file-list><welcome-file>regist.jsp</welcome-file></welcome-file-list></web-app>


regist.jsp
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><title>使用者註冊</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"></head><body><h1>使用者註冊</h1><br/><form action="<%=request.getContextPath() %>/bjsxt/user_execute.action" method="post">使用者名稱:<input type="text" name="username"/><br/>密碼:<input type="password" name="password"/><br/>確認密碼:<input type="password" name="password2"/><br/><input type="submit" value=" 提 交 "/></form></body></html>



registFail.jsp
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><title>失敗</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"></head><body><h1>Fail!</h1></body></html>

registSuccess.jsp

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><title>成功</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"></head><body><h1>Success!</h1></body></html>


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.