Struts与Spring整合实现用户登录

来源:互联网 发布:c语言中求绝对值 编辑:程序博客网 时间:2024/06/11 11:13
  • 步骤
    引入jar文件
    引入struts .jar相关文件
    spring-core 相关jar文件
    spring-web 支持jar包
    spring-web-3.2.5.RELEASE.jar 【Spring源码】
    struts2-spring-plugin-2.3.4.1.jar 【Struts源码】
  • 配置
    struts.xml
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"    "http://struts.apache.org/dtds/struts-2.0.dtd"><struts>  <package name="default" extends="struts-default">    <action name="checkLogin" class="checkLoginAction">   <!--注意: class属性与以往不同 -->        <result name="success">login_suc.jsp</result>        <result name="fail">login_fail.jsp</result>    </action>  </package></struts>

bean-dao.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:p="http://www.springframework.org/schema/p"    xmlns:context="http://www.springframework.org/schema/context"    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">    <bean id="userDao" class="com.ss.dao.UserDaoImpl"  scope="singleton"></bean> <!-- 单例,只创建一个实例 --></beans>      

bean-service.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:p="http://www.springframework.org/schema/p"    xmlns:context="http://www.springframework.org/schema/context"    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">    <bean id="checkLoginService" class="com.ss.service.CheckLoginService">        <property name="userDao" ref="userDao"></property>    </bean></beans>      

bean-action.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:p="http://www.springframework.org/schema/p"    xmlns:context="http://www.springframework.org/schema/context"    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">    <bean id="checkLoginAction" class="com.ss.action.CheckLoginAction" scope="prototype">        <property name="checkLoginService" ref="checkLoginService"></property>    </bean></beans>      

web.xml

<?xml version="1.0" encoding="UTF-8"?><web-app version="3.0"     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_3_0.xsd">  <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>    <!-- 2. spring 配置 -->    <context-param>        <param-name>contextConfigLocation</param-name>        <param-value>/WEB-INF/classes/bean-*.xml</param-value>    </context-param>    <listener>        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>    </listener>  <welcome-file-list>    <welcome-file>index.jsp</welcome-file>  </welcome-file-list></web-app>
0 0