web
<?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的配置文件,默认从web根目录寻找配置文件,我们可以通过spring提供的classpath:前缀指定从类路径下寻找
-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 对Spring容器进行实例化 -->
<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>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
application
<?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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml" />
</bean>
<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="get*" read-only="true" propagation="SUPPORTS" />
<tx:method name="find*" read-only="true" propagation="SUPPORTS" />
<tx:method name="*" read-only="false" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="txDAO" expression="execution (* cn.s2.dao.*.*(..))" />
<aop:advisor pointcut-ref="txDAO" advice-ref="txAdvice" />
</aop:config>
<bean id="UsersDAOImpl" class="cn.s2.dao.impl.UsersDAOImpl" scope="singleton">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="UsersAction" class="cn.s2.action.UsersAction" scope="prototype">
<property name="IUsersDao" ref="UsersDAOImpl" />
</bean>
</beans>
struct
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<!-- 将struts交给spring管理 -->
<constant name="struts.objectFactory" value="spring" />
<package name="s2sh" extends="struts-default">
<action name="login" class="UsersAction" method="doLogin">
<result name="success">/message.jsp</result>
</action>
</package>
</struts>
hibernate
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="dialect">
org.hibernate.dialect.SQLServerDialect
</property>
<property name="connection.url">
jdbc:sqlserver://localhost:1433
</property>
<property name="connection.username">sa</property>
<property name="connection.driver_class">
com.microsoft.sqlserver.jdbc.SQLServerDriver
</property>
<property name="myeclipse.connection.profile">
MS SQL Server 2005
</property>
<property name="show_sql">true</property>
<mapping resource="cn/s2/entity/Users.hbm.xml" />
</session-factory>
</hibernate-configuration>
action
package cn.s2.action;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import cn.s2.dao.IUsersDAO;
import cn.s2.dao.impl.UsersDAOImpl;
import cn.s2.entity.Users;
public class UsersAction{
private IUsersDAO IUsersDao;
private Users users;
private String outMessage;//输出信息
private HttpServletRequest request= ServletActionContext.getRequest();
//登录
public String doLogin(){
List<Users> list = IUsersDao.findByExample(users);
if(list.size()==0){
outMessage="登录失败!";
}
else{
outMessage="登录成功!";
}
return "success";
}
public IUsersDAO getIUsersDao() {
return IUsersDao;
}
public void setIUsersDao(IUsersDAO iUsersDao) {
IUsersDao = iUsersDao;
}
public Users getUsers() {
return users;
}
public void setUsers(Users users) {
this.users = users;
}
public String getOutMessage() {
return outMessage;
}
public void setOutMessage(String outMessage) {
this.outMessage = outMessage;
}
}
<?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的配置文件,默认从web根目录寻找配置文件,我们可以通过spring提供的classpath:前缀指定从类路径下寻找
-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 对Spring容器进行实例化 -->
<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>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
application
<?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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml" />
</bean>
<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="get*" read-only="true" propagation="SUPPORTS" />
<tx:method name="find*" read-only="true" propagation="SUPPORTS" />
<tx:method name="*" read-only="false" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="txDAO" expression="execution (* cn.s2.dao.*.*(..))" />
<aop:advisor pointcut-ref="txDAO" advice-ref="txAdvice" />
</aop:config>
<bean id="UsersDAOImpl" class="cn.s2.dao.impl.UsersDAOImpl" scope="singleton">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="UsersAction" class="cn.s2.action.UsersAction" scope="prototype">
<property name="IUsersDao" ref="UsersDAOImpl" />
</bean>
</beans>
struct
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<!-- 将struts交给spring管理 -->
<constant name="struts.objectFactory" value="spring" />
<package name="s2sh" extends="struts-default">
<action name="login" class="UsersAction" method="doLogin">
<result name="success">/message.jsp</result>
</action>
</package>
</struts>
hibernate
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="dialect">
org.hibernate.dialect.SQLServerDialect
</property>
<property name="connection.url">
jdbc:sqlserver://localhost:1433
</property>
<property name="connection.username">sa</property>
<property name="connection.driver_class">
com.microsoft.sqlserver.jdbc.SQLServerDriver
</property>
<property name="myeclipse.connection.profile">
MS SQL Server 2005
</property>
<property name="show_sql">true</property>
<mapping resource="cn/s2/entity/Users.hbm.xml" />
</session-factory>
</hibernate-configuration>
action
package cn.s2.action;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import cn.s2.dao.IUsersDAO;
import cn.s2.dao.impl.UsersDAOImpl;
import cn.s2.entity.Users;
public class UsersAction{
private IUsersDAO IUsersDao;
private Users users;
private String outMessage;//输出信息
private HttpServletRequest request= ServletActionContext.getRequest();
//登录
public String doLogin(){
List<Users> list = IUsersDao.findByExample(users);
if(list.size()==0){
outMessage="登录失败!";
}
else{
outMessage="登录成功!";
}
return "success";
}
public IUsersDAO getIUsersDao() {
return IUsersDao;
}
public void setIUsersDao(IUsersDAO iUsersDao) {
IUsersDao = iUsersDao;
}
public Users getUsers() {
return users;
}
public void setUsers(Users users) {
this.users = users;
}
public String getOutMessage() {
return outMessage;
}
public void setOutMessage(String outMessage) {
this.outMessage = outMessage;
}
}