【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

    
    <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 ">

    
    <bean id="config" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
        <property name="locations" value="classpath*:*.properties"/>
    bean>

    
    <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/>

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

    
    <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);
    }
}

 

你可能感兴趣的:(【SSH】Spring 整合 Struts)