http://www.blogjava.net/action/archive/2009/04/09/140566.html
首选创建web.xml 主要是配置Struts的ActionServlet和Spring的字符过滤器
<?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>
首选创建web.xml 主要是配置Struts的ActionServlet和Spring的字符过滤器
<?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配置过程,但实际内容却涉及到了Struts2、Spring以及Hibernate等Java Web框架和技术的配置。为了符合您的需求,我将集中讨论SSH...
SSH 配置详解 SSH(Structs、Spring、Hibernate)框架是 JavaEE 实训课程中常用的框架组合,它可以帮助开发人员快速构建 JavaEE 应用程序。本文将详细介绍 SSH 框架的配置和使用方法。 SSH 框架的作用 -----------...
### H3C华三网络设备SSH配置详解 #### 一、引言 SSH(Secure Shell)是一种加密的网络传输协议,常用于远程登录管理网络设备。对于H3C(华三)网络设备而言,SSH提供了安全的方式来管理和配置设备。本文将详细介绍...
h3c交换机SSH配置方法h3c交换机SSH配置方法h3c交换机SSH配置方法
SSH配置文件是控制SSH客户端和服务端行为的关键文件,它允许用户根据需求自定义连接参数,提高安全性,并优化交互体验。本文将深入探讨SSH配置文件的结构、常用配置选项以及如何进行有效管理。 首先,SSH的配置文件...
在本文中,我们将深入探讨最新的SSH配置方案,特别是基于Struts2.3.12、Spring3.2.2和Hibernate4.2.0的SSH框架搭建。这个配置方案适用于开发Java Web应用,它提供了模型-视图-控制器(MVC)架构,并集成了强大的持久...
Ubuntu 下 SSH 配置详解 在 Linux 的 Ubuntu 版本下,配置 SSH 服务是一件非常重要的事情。SSH(Secure Shell)是一种安全的远程登录协议,可以实现远程登录到服务器上。下面将详细介绍在 Ubuntu 下配置 SSH 服务的...
### 思科交换机SSH配置详解 #### 一、引言 随着网络安全需求的日益增长,采用安全的方式访问网络设备变得至关重要。思科交换机作为网络基础设施中的关键组件,其安全性同样不可忽视。Secure Shell(SSH)作为一种...
"乌班图ssh配置过程" 在 Ubuntu 系统中, SSH 服务的配置是一个非常重要的步骤,特别是在 Windows 下进行 Linux 开发时。下面将详细介绍 Ubuntu 中 SSH 服务的配置过程。 首先,在 Ubuntu 系统中,需要安装 ...
SQLServer 数据库 SSH 配置详解 SQLServer 数据库 SSH 配置是将 SQLServer 数据库与 Secure Shell(SSH)协议集成,以实现加密的数据传输和身份验证。本文将详细介绍 SQLServer 数据库 SSH 配置的实现步骤和相关...
本文将深入探讨SSH配置的详细步骤以及在配置过程中可能遇到的异常处理。 首先,我们从Struts开始。Struts是一个基于MVC(Model-View-Controller)设计模式的Java Web框架。配置Struts通常包括以下步骤: 1. 添加...
【标题】:CentOS系统安装及SSH配置 在IT领域,CentOS是一款广泛使用的Linux发行版,因其稳定性、安全性以及与Red Hat Enterprise Linux的高度兼容性而备受青睐。本压缩包文件"CentOS系统安装及SSH配置.rar"包含了...
新手学习ssh配置,详细介绍ssh相关知识点
- 在IDE中进行SSH配置的步骤,例如在Eclipse或IntelliJ IDEA中添加框架支持。 这些PPT教程对于初学者来说非常有价值,可以帮助他们快速理解SSH框架的集成和配置,从而更好地进行Java Web开发。通过深入学习和实践,...
SSH的英文全称为Secure Shell,它默认的连接端口是22。通过使用SSH,可以把所有传输的数据进行加密,这样类似上面的“中间人”攻击方式就不可能实现了,而且它也能够防止DNS和IP欺骗。另外,它还有一个额外的好处...
交换机-SSH配置.docx.baiduyun.downloading
【MySQL教程SSH配置】 在学习和管理MySQL数据库时,SSH(Secure Shell)配置是一个重要的环节。SSH提供了一种安全的网络通信方式,可以让你通过加密的通道远程访问MySQL服务器,防止数据在传输过程中被窃取或篡改。...
要配置SSH服务,首先需要在CentOS上安装SSH服务。在CentOS 5.3版本中,可以通过`yum install ssh`命令来安装SSH。安装完成后,可以使用`service sshd start`启动SSH服务,并使用`chkconfig sshd on`将其设置为开机...