- 浏览: 16399 次
- 性别:
- 来自: 北京
文章分类
最新评论
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts/struts.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>3</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>3</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- session超时定义,单位为分钟 -->
<session-config>
<session-timeout>10</session-timeout>
</session-config>
<!-- 默认首页定义 -->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
然后创建struts.xml(String配置文件) 要注意这里集成了Spring插件,把全部Spring配置文件注入到ContextLoaderPlugIn中
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
<form-beans>
<form-bean name="loginVO" type="com.vo.LoginVO" />
</form-beans>
<global-forwards>
<forward name="error" path="/error.jsp"/>
</global-forwards>
<action-mappings>
<action path="/login"
name="loginVO"
type="org.springframework.web.struts.DelegatingActionProxy"
parameter="action"
scope="request">
<forward name="login" path="/login.jsp"/>
<forward name="index" path="/index.jsp"/>
</action>
</action-mappings>
<!-- 集成Spring插件 -->
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation" value="/WEB-INF/spring/*.xml" />
</plug-in>
</struts-config>
配置Spring配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- 读入属性文件 -->
<bean id="propertyConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:hibernate.properties</value>
</list>
</property>
</bean>
<!-- 配置数据源,可以其他方式 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${hibernate.driverClassName}" />
<property name="url" value="${hibernate.url}" />
<property name="username" value="${hibernate.username}" />
<property name="password" value="${hibernate.password}" />
<property name="maxActive" value="${hibernate.maxActive}" />
<property name="maxIdle" value="${hibernate.maxIdle}" />
<property name="maxWait" value="${hibernate.maxWait}" />
</bean>
<!-- 配置Hibernate的Session工厂,注入数据源、映射文件 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref local="dataSource"/>
</property>
<property name="mappingResources">
<list>
<value>com/po/login.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.showSQL}</prop>
</props>
</property>
</bean>
<!-- 声明Hibernate事务管理,注入Session工厂 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>
<!-- 配置事务代理,注入事务管理transactionManager,由Spring来代理事务,设置事务属性 -->
<bean id="transactionProxy" abstract="true" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref bean="transactionManager"/>
</property>
<property name="transactionAttributes">
<props>
<prop key="save*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="add*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="remove*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="delete*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="update*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="create*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="query*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
</beans>
配置Action将Service注入到Action
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- 配置Action,singleton属性设置为false表示不使用单例,每次都重新创建实例,避免并发问题,注入事务管理的Service -->
<bean name="/login" class="com.action.LoginAction" singleton="false">
<property name="loginService">
<ref bean="loginService"/>
</property>
</bean>
</beans>
配置Service将Dao注入到Service
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- 配置事务代理Service,先将Dao注入到Service,再将Service注入给事务代理 -->
<bean id="loginService" parent="transactionProxy">
<property name="target">
<ref bean="loginTempService"/>
</property>
</bean>
<bean id="loginTempService" class="com.service.LoginService">
<property name="loginDao">
<ref bean="loginDao"/>
</property>
</bean>
</beans>
配置Dao 注入Session工厂
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- 配置Dao,注入Session工厂 -->
<bean id="loginDao" class="com.dao.LoginDao">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
</beans>
数据源属性文件(注意不是Hibernate的配置文件,是为了让Spring读入的)
hibernate.dialect=org.hibernate.dialect.SQLServerDialect
hibernate.driverClassName=com.mysql.jdbc.Driver
hibernate.url=jdbc:mysql://127.0.0.1:3306/ssh
hibernate.username=root
hibernate.password=5719
hibernate.showSQL=true
hibernate.maxActive=50
hibernate.maxIdle=30
hibernate.maxWait=1000
log4j配置文件(简单)
log4j.rootLogger=ERROR,console,file
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%-5p %d [%F,%L] - %m%n
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=F:\\SSH.log
#log4j.appender.file.MaxFileSize=100000KB
#log4j.appender.file.MaxBackupIndex=1
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%-5p %d [%F,%L] - %m%n
下面是类文件
package com.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import com.service.LoginService;
import com.vo.LoginVO;
public class LoginAction extends DispatchAction {
private Log logger = LogFactory.getLog(LoginAction.class);
private LoginService loginService;
public void setLoginService(LoginService loginService) {
this.loginService = loginService;
}
public ActionForward login(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) {
try {
LoginVO loginVO = (LoginVO) form;
String username = loginVO.getUsername();
String password = loginVO.getPassword();
System.out.println(username+password);
if(loginService.validate(username, password)) {
return mapping.findForward("index");
}
return mapping.findForward("error");
} catch (Exception e) {
logger.error(e);
return mapping.findForward("error");
}
}
public ActionForward save(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) {
try {
LoginVO loginVO = (LoginVO) form;
loginService.saveUser(loginVO);
return mapping.findForward("index");
} catch (Exception e) {
logger.error(e);
return mapping.findForward("error");
}
}
}
package com.dao;
import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.po.LoginPO;
public class LoginDao extends HibernateDaoSupport {
@SuppressWarnings("unchecked")
public String getPassword(String username) {
String hql = "from LoginPO l where l.username=?";
List list = getSession().createQuery(hql).setString(0,username).list();
if(list!=null && list.size()>0) {
LoginPO loginPO = (LoginPO) list.get(0);
return loginPO.getPassword();
}
return null;
}
public void save(LoginPO loginPO) {
getSession().save(loginPO);
}
}
package com.po;
import java.io.Serializable;
public class LoginPO implements Serializable {
private static final long serialVersionUID = 1L;
private Integer id = null;
private String username = null;
private String password = null;
public Integer getId() {
return id;
}
public String getPassword() {
return password;
}
public String getUsername() {
return username;
}
public void setId(Integer id) {
this.id = id;
}
public void setPassword(String password) {
this.password = password;
}
public void setUsername(String username) {
this.username = username;
}
}
package com.service;
import com.dao.LoginDao;
import com.po.LoginPO;
import com.vo.LoginVO;
public class LoginService {
private LoginDao loginDao;
public void setLoginDao(LoginDao loginDao) {
this.loginDao = loginDao;
}
public boolean validate(String username,String password) {
String pass = loginDao.getPassword(username);
if(pass!=null) {
if(pass.equals(password)) {
return true;
}
}
return false;
}
public void saveUser(LoginVO loginVO) {
LoginPO loginPO = new LoginPO();
loginPO.setUsername(loginVO.getUsername());
loginPO.setPassword(loginVO.getPassword());
System.out.println(loginVO.getUsername()+"-"+loginVO.getPassword()+":save succeed...");
loginDao.save(loginPO);
//故意制造异常,测试事务。
//loginDao.save(null);
}
}
package com.vo;
import org.apache.struts.action.ActionForm;
public class LoginVO extends ActionForm {
private static final long serialVersionUID = 1L;
private String username = null;
private String password = null;
public String getPassword() {
return password;
}
public String getUsername() {
return username;
}
public void setPassword(String password) {
this.password = password;
}
public void setUsername(String username) {
this.username = username;
}
}
Hibernate映射文件
<?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>
<class name="com.po.LoginPO" table="login">
<comment></comment>
<id name="id" type="int">
<column name="id" />
<generator class="native" />
</id>
<property name="username" type="string">
<column name="username" not-null="true">
<comment></comment>
</column>
</property>
<property name="password" type="string">
<column name="password" not-null="true">
<comment></comment>
</column>
</property>
</class>
</hibernate-mapping>
jsp页面文件
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<body>
<form name="form" action="login.do?action=login" method="post">
<h1>Login</h1>
<h4>username:</h4><input name="username" type="text"> <span id="message"></span>
<h4>password:</h4><input name="password" type="password">
<br>
<br>
<input value="提交" type="button" onclick="form.submit();">
</form>
<br>
<br>
<br>
<form name="form" action="login.do?action=save" method="post">
<h1>Save</h1>
<h4>username:</h4><input name="username" type="text"> <span id="message"></span>
<h4>password:</h4><input name="password" type="password">
<br>
<br>
<input value="提交" type="button" onclick="form.submit();">
</form>
</body>
</html>
相关推荐
SSH整合在JavaWeb开发中是一项重要的技术组合,它包含了三个主要的开源框架:Spring、Struts和Hibernate。这些框架分别负责应用的业务逻辑管理、视图层控制和数据持久化。接下来,我们将深入探讨SSH整合的关键知识点...
SSH整合jar包是一个集合了所有在Java开发中用于Spring、Struts和Hibernate(SSH)集成应用所需的库文件的压缩包。SSH是Java企业级开发中非常流行的一种框架组合,它能够帮助开发者快速构建高效、可维护的企业级Web...
SSH整合是Java Web开发中的一种常见模式,它结合了Struts2、Spring和Hibernate三个开源框架,以实现高效、灵活的MVC(Model-View-Controller)架构。在这个"ssh整合jar包"中,包含了这三个框架的核心库和其他必要的...
SSH整合,全称为Struts2、Spring和Hibernate的整合,是Java Web开发中常见的技术栈。这三种框架分别负责表现层、业务层和持久层的管理,通过合理的整合,可以构建出高效、灵活且易于维护的Web应用。下面将详细介绍...
SSH整合是Java开发中一种常见的框架集成方式,它结合了Struts2、Spring和Hibernate三个开源框架,以实现高效、灵活的企业级应用开发。这里提到的"ssh整合所需所有架包"是一个包含这三个框架相应版本的集合,确保它们...
SSH整合是Java Web开发中常见的一个技术组合,指的是Spring、Struts和Hibernate三个开源框架的集成。Spring提供了依赖注入和面向切面编程的能力,Struts则处理MVC(Model-View-Controller)架构中的控制器部分,而...
SSH整合经典实例主要涉及到的是Java开发中的三大框架:Spring、Struts和Hibernate的集成应用,这些框架在J2EE体系中被广泛使用,为开发者提供了高效、便捷的开发环境。SSH整合是Java企业级开发中一种常见的解决方案...
SSH整合在IT行业中通常指的是Struts、Hibernate和Spring三大框架的集成应用,它们是Java Web开发中的重要组件,尤其在企业级应用系统中广泛应用。银行管理系统是一个典型的业务复杂、安全性要求高的应用场景,SSH...
SSH整合是指Spring、Struts和Hibernate这三大开源框架的集成应用。这三种框架分别负责不同的职责:Spring作为应用的管理核心,提供依赖注入(DI)和面向切面编程(AOP);Struts则主要处理MVC(模型-视图-控制器)...
SSH整合在IT行业中通常指的是Spring、Struts和Hibernate这三个开源框架的集成应用。Spring作为核心容器,负责管理对象(依赖注入DI)和事务处理;Struts则是MVC(Model-View-Controller)设计模式的实现,处理用户...
SSH整合是Java开发中一种常见的框架组合,包括Spring、Struts和Hibernate三个核心组件。Spring提供了依赖注入(DI)和面向切面编程(AOP),Struts负责MVC模式的实现,而Hibernate则是用于对象关系映射(ORM)。在这...
SSH整合是软件开发中一种常见的技术实践,主要指的是Spring、Struts和Hibernate这三大开源框架的集成应用。这个小案例提供了SSH整合的具体实现,对于项目研发具有很高的实用价值。下面我们将详细探讨SSH整合的关键...
SSH整合是指将Struts、Spring和Hibernate这三个Java开源框架结合在一起,用于构建高效、可维护的企业级Web应用程序。Struts提供了MVC(Model-View-Controller)架构,Spring强化了依赖注入和事务管理,而Hibernate则...
SSH整合是Java Web开发中的一种常见技术栈,主要包括Spring、Struts和Hibernate这三个框架的集成。这个名为"SSH整合小项目"的资源提供了一个实践示例,帮助开发者理解和掌握这三大框架协同工作的机制。 首先,...
SSH整合是Java Web开发中的一个常见实践,它指的是Struts 2、Hibernate和Spring这三大框架的集成。这个"SSH整合实例源码"提供了一种实际应用这些框架的方法,帮助开发者理解如何在项目中有效地结合它们。以下是关于...
SSH整合是指在Java Web开发中,将Spring、Struts2和Hibernate这三大开源框架进行集成,以实现高效、灵活和可扩展的业务应用。这些框架分别负责不同的职责:Spring作为核心容器,管理对象的依赖注入;Struts2是MVC...
SSH整合是软件开发中的一种常见技术,主要用于Spring、Struts和Hibernate这三大开源框架的集成。这篇文章将深入探讨SSH整合的原理以及实现步骤,帮助初学者掌握这一关键技能。 **一、SSH框架简介** 1. **Spring...