- 浏览: 21210 次
- 性别:
- 来自: 郑州
最新评论
-
yufei2999:
光写问题,把解决办法也写上啊,不然贴出来有啥用
getSession().saveOrUpdate(u);//不保存,不更新 -
qingfeng_101:
呵呵 我也有个分页的东东 好像比这个好用些 是个JavaBea ...
Hibernate+Spring+Struts2整合开发中的一个分页显示方案 (转)
首选创建web.xml 主要是配置Struts的ActionServlet和Spring的字符过滤器
1. <?xml version="1.0" encoding="UTF-8"?>
2. <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">
3.
4. <filter>
5. <filter-name>CharacterEncodingFilter</filter-name>
6. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
7. <init-param>
8. <param-name>encoding</param-name>
9. <param-value>UTF-8</param-value>
10. </init-param>
11. <init-param>
12. <param-name>forceEncoding</param-name>
13. <param-value>true</param-value>
14. </init-param>
15. </filter>
16.
17. <filter-mapping>
18. <filter-name>CharacterEncodingFilter</filter-name>
19. <url-pattern>/*</url-pattern>
20. </filter-mapping>
21.
22. <servlet>
23. <servlet-name>action</servlet-name>
24. <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
25. <init-param>
26. <param-name>config</param-name>
27. <param-value>/WEB-INF/struts/struts.xml</param-value>
28. </init-param>
29. <init-param>
30. <param-name>debug</param-name>
31. <param-value>3</param-value>
32. </init-param>
33. <init-param>
34. <param-name>detail</param-name>
35. <param-value>3</param-value>
36. </init-param>
37. <load-on-startup>0</load-on-startup>
38. </servlet>
39.
40. <servlet-mapping>
41. <servlet-name>action</servlet-name>
42. <url-pattern>*.do</url-pattern>
43. </servlet-mapping>
44.
45. <!-- session超时定义,单位为分钟 -->
46. <session-config>
47. <session-timeout>10</session-timeout>
48. </session-config>
49.
50. <!-- 默认首页定义 -->
51. <welcome-file-list>
52. <welcome-file>index.jsp</welcome-file>
53. <welcome-file>index.html</welcome-file>
54. </welcome-file-list>
55.
56. </web-app>
1===========1
然后创建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>
=================
1. <?xml version="1.0" encoding="UTF-8"?>
2. <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">
3.
4. <filter>
5. <filter-name>CharacterEncodingFilter</filter-name>
6. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
7. <init-param>
8. <param-name>encoding</param-name>
9. <param-value>UTF-8</param-value>
10. </init-param>
11. <init-param>
12. <param-name>forceEncoding</param-name>
13. <param-value>true</param-value>
14. </init-param>
15. </filter>
16.
17. <filter-mapping>
18. <filter-name>CharacterEncodingFilter</filter-name>
19. <url-pattern>/*</url-pattern>
20. </filter-mapping>
21.
22. <servlet>
23. <servlet-name>action</servlet-name>
24. <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
25. <init-param>
26. <param-name>config</param-name>
27. <param-value>/WEB-INF/struts/struts.xml</param-value>
28. </init-param>
29. <init-param>
30. <param-name>debug</param-name>
31. <param-value>3</param-value>
32. </init-param>
33. <init-param>
34. <param-name>detail</param-name>
35. <param-value>3</param-value>
36. </init-param>
37. <load-on-startup>0</load-on-startup>
38. </servlet>
39.
40. <servlet-mapping>
41. <servlet-name>action</servlet-name>
42. <url-pattern>*.do</url-pattern>
43. </servlet-mapping>
44.
45. <!-- session超时定义,单位为分钟 -->
46. <session-config>
47. <session-timeout>10</session-timeout>
48. </session-config>
49.
50. <!-- 默认首页定义 -->
51. <welcome-file-list>
52. <welcome-file>index.jsp</welcome-file>
53. <welcome-file>index.html</welcome-file>
54. </welcome-file-list>
55.
56. </web-app>
1===========1
然后创建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>
=================
发表评论
-
Parsing error processing resource path jndi:/localhost/JOA/WEB-INF/struts-config
2010-02-01 14:50 1429Parsing error processing resour ... -
getSession().saveOrUpdate(u);//不保存,不更新
2010-01-26 20:10 3652/**更新员工信息*/ public void updat ... -
open session in view 的用法
2010-01-26 09:45 1680第一种方式:Filter过滤器实现 <filter> ... -
S2SH整合框架的错误信息及解决办法
2010-01-25 20:08 774(_) 常规处理办法这是转载文章,但是不用不行! 2009- ... -
Hibernate+Spring+Struts2整合开发中的一个分页显示方案 (转)
2010-01-22 19:41 1252Hibernate+Spring+Struts2整合开发中的一 ... -
通用分页:pager taglib执行分页(本例采用struts2+hibernate)
2010-01-21 19:22 1268使用此分页插件很简单 ...
相关推荐
SSH整合是指将Spring、Struts和Hibernate这三个Java开发框架集成在一起,用于构建高效、稳定的企业级Web应用程序。在Java世界中,Spring提供了依赖注入和面向切面编程,Struts负责控制层逻辑,而Hibernate则作为持久...
- 小例子可能展示了如何创建一个简单的SSH应用,包括创建Action、Service、DAO,定义数据库表和实体类,编写Hibernate映射文件,最后展示如何在Struts的Action中调用Service,由Service调用DAO完成数据的CRUD操作。...
SSH框架整合是Java Web开发中常见的一种技术组合,它包括Spring、Struts和Hibernate三个主要组件。SSH框架的整合能够帮助开发者实现模型-视图-控制器(MVC)设计模式,提供事务管理、数据持久化以及用户界面控制等...
在IT行业中,SSH、Ajax...总结来说,"省市级联 ssh整合实现"项目结合了SSH框架的强大功能,通过Ajax和DWR实现前后端的数据交互,提供了流畅的用户体验。通过深入理解这些技术,开发者可以构建出高效、响应式的Web应用。
### **1.3 整合一个简单的例子** 通过具体的实例,教程展示了如何将Struts的控制流与Spring的依赖注入及事务管理、Hibernate的数据持久化能力结合起来,构建一个功能完整、架构清晰的应用系统。 **三、Spring整合...
这个项目作为一个学习资源,可以帮助开发者了解如何整合SSH框架与EasyUI DataGrid,实现数据操作和分页功能,对于初学者或者想要提升Web应用开发技能的人来说是非常有价值的。通过这个实例,你可以学习到如何设置...
##### 1.3 整合一个简单的例子 为了更好地理解SSH框架是如何工作的,我们可以考虑一个简单的例子。假设有一个用户管理系统,该系统需要处理用户的登录、注册等功能。 - **Struts**可以用来处理用户提交的表单数据...
最近温习ssh2整合编程,顺便浏览下struts2有什么更新的消息,下载了新版本的struts2的2.1.8.1版,使用的是MyEclipse8.0开发,但是问题就随之而来了。MyEclipse8.0中自带的struts2版本是2.1.6,spring版本有2.0,2.5...
1.3 RAC的特点 1.3.1双机并行 1.3.2高可用性 1.3.3易伸缩性 1.3.4低成本 1.3.5高吞吐量 1.4 RAC存在的问题 1.4.1稳定性 1.4.2高性能 1.5 RAC软件 1.5.1存储管理软件 1.5.2集群管理软件 1.5.3数据库管理...