zoukankan      html  css  js  c++  java
  • 【SSH】Spring 整合 Struts

    添加 spring-struts-3.2.9.RELEASE.jar

    struts-config.xml 添加

        <controller>
            <set-property property="processorClass"
                          value="org.springframework.web.struts.DelegatingRequestProcessor"></set-property>
        </controller>

    applicationContext.xml

        <!--配置 action-->
        <bean name="/login" class="k.action.LoginAction" scope="prototype"/>
        <bean name="/employee" class="k.action.EmployeeAction" scope="prototype"/>

    struts-config.xml

    <?xml version="1.0" encoding="UTF-8"?>
    
    <!DOCTYPE struts-config PUBLIC
            "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
            "http://struts.apache.org/dtds/struts-config_1_3.dtd">
    
    <struts-config>
    
        <form-beans>
            <form-bean name="employeeForm" type="k.form.EmployeeForm">
                <form-property name="id" type="java.lang.Integer"></form-property>
                <form-property name="name" type="java.lang.String"></form-property>
                <form-property name="password" type="java.lang.String"></form-property>
            </form-bean>
        </form-beans>
    
        <global-forwards>
            <forward name="ok" path="/WEB-INF/jsp/ok.jsp"></forward>
            <forward name="err" path="/WEB-INF/jsp/err.jsp"></forward>
        </global-forwards>
    
        <action-mappings>
            <action name="employeeForm" path="/login" parameter="action" type="k.action.LoginAction"
                    scope="request" attribute="employeeForm" input="index.jsp" validate="false">
                <forward name="main" path="/WEB-INF/jsp/main.jsp"></forward>
                <forward name="loginJsp" path="/WEB-INF/jsp/login.jsp"></forward>
            </action>
            <action name="employeeForm" path="/employee" parameter="action" type="k.action.EmployeeAction"
                    scope="request" attribute="employeeForm" input="index.jsp" validate="false">
                <forward name="addEmployeeUI" path="/WEB-INF/jsp/addEmployeeUI.jsp"></forward>
                <forward name="loginJsp" path="/WEB-INF/jsp/login.jsp"></forward>
            </action>
        </action-mappings>
    
        <controller>
            <set-property property="processorClass"
                          value="org.springframework.web.struts.DelegatingRequestProcessor"></set-property>
        </controller>
    
    </struts-config>

    applicationContext.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:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
               http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
               http://www.springframework.org/schema/context
               http://www.springframework.org/schema/context/spring-context-3.0.xsd ">
    
        <!-- 加载db.properties文件 -->
        <bean id="config" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
            <property name="locations" value="classpath*:*.properties"/>
        </bean>
    
        <!-- 配置配置数据库信息(替代mybatis的配置文件conf.xml) -->
        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
            <property name="driverClassName" value="${driver}"></property>
            <property name="url" value="${url}"></property>
            <property name="username" value="${username}"></property>
            <property name="password" value="${password}"></property>
            <property name="initialSize" value="${initialSize}"></property>
            <property name="maxActive" value="${maxActive}"></property>
            <property name="maxIdle" value="${maxIdle}"></property>
            <property name="minIdle" value="${minIdle}"></property>
        </bean>
    
        <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
            <property name="dataSource" ref="dataSource"></property>
            <!-- //加载实体类的映射文件位置及名称 -->
            <property name="mappingLocations" value="classpath:k/bean/*.hbm.xml"></property>
            <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
        </bean>
    
        <!--事务-->
        <bean id="txManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
            <property name="sessionFactory" ref="sessionFactory"/>
        </bean>
    
        <!--开启注解配置-->
        <context:annotation-config/>
    
        <!--配置 action-->
        <bean name="/login" class="k.action.LoginAction" scope="prototype"/>
        <bean name="/employee" class="k.action.EmployeeAction" scope="prototype"/>
    
        <!--配置 Service-->
        <bean name="testService" class="k.service.TestService"/>
        <bean name="employeeService" class="k.service.impl.EmployeeServiceImpl"/>
        <bean name="departmentService" class="k.service.impl.DepartmentServiceImpl"/>
    
    </beans>

    LoginAction

    public class LoginAction extends DispatchAction {
    
        @Resource
        private EmployeeService employeeService;
    
        public ActionForward login(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
            return mapping.findForward("loginJsp");
        }
    
        public ActionForward doLogin(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
            EmployeeForm employeeOld = (EmployeeForm) form;
            Employee employee = new Employee(Integer.parseInt(employeeOld.getId()), employeeOld.getPassword());
            employee = employeeService.checkEmployee(employee);
            if (employee != null) {
                request.getSession().setAttribute("employee", employee);
                return mapping.findForward("main");
            }
            return mapping.findForward("err");
        }
    
        public ActionForward loginOut(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
            return super.execute(mapping, form, request, response);
        }
    }
  • 相关阅读:
    mysql主从与mycat与MHA
    mycat+mysql集群:实现读写分离,分库分表
    centos7下扩展根分区(图文详解)
    MySQL高可用之MHA的搭建
    zabbix_agentd客户端安装与配置(windows操作系统)
    centos7手把手教你搭建zabbix监控
    有关添加System.Web的问题
    锚标签<a>
    MVC3出现“提供程序未返回 ProviderManifestToken 字符串”的解决办法
    解决.net后台调用js弹窗后,前台界面样式乱掉问题
  • 原文地址:https://www.cnblogs.com/kikyoqiang/p/12327653.html
Copyright © 2011-2022 走看看