当前位置:首页 > 虚拟主机 > 正文

SSH框架配置文件在哪?路径设置与常见错误详解

SSH框架(Struts2 + Spring + Hibernate)的整合需要配置多个核心文件,以下是关键配置文件及详细说明:


web.xml (Web应用部署描述符)

<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <!-- Spring 监听器 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- Struts2 过滤器 --> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>


struts.xml (Struts2核心配置)

<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" "http://struts.apache.org/dtds/struts-2.5.dtd"> <struts> <!-- 开发模式(输出详细错误) --> <constant name="struts.devMode" value="true" /> <!-- Action扫描包 --> <package name="default" extends="struts-default" namespace="/"> <action name="userAction" class="userAction"> <result name="success">/success.jsp</result> <result name="error">/error.jsp</result> </action> </package> </struts>


applicationContext.xml (Spring核心配置)

<?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:tx="http://www.springframework.org/schema/tx" 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 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 自动扫描注解(Service/Repository) --> <context:component-scan base-package="com.example.service, com.example.dao" /> <!-- 数据源配置 --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/test_db" /> <property name="username" value="root" /> <property name="password" value="123456" /> </bean> <!-- Hibernate SessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQL8Dialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> </props> </property> <!-- 实体类映射 --> <property name="packagesToScan" value="com.example.entity" /> </bean> <!-- 事务管理器 --> <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <tx:annotation-driven transaction-manager="transactionManager" /> <!-- 将Struts2的Action交给Spring管理 --> <bean id="userAction" class="com.example.action.UserAction" scope="prototype"> <property name="userService" ref="userService" /> </bean> </beans>


Hibernate实体映射示例 (User.java)

@Entity @Table(name = "t_user") public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; private String username; private String password; // Getters & Setters }


目录结构建议

src ├── main │ ├── java │ │ ├── com.example.action # Struts2 Action │ │ ├── com.example.service # Service接口及实现 │ │ ├── com.example.dao # DAO接口及实现 │ │ └── com.example.entity # Hibernate实体类 │ ├── resources │ │ ├── struts.xml # Struts2配置 │ │ └── applicationContext.xml # Spring配置 │ └── webapp │ ├── WEB-INF │ │ └── web.xml │ └── index.jsp


关键整合说明:

  1. Struts2与Spring整合

    • 在applicationContext.xml中定义Struts2的Action Bean(scope="prototype"确保每次请求新实例)。
    • struts.xml中class属性使用Spring Bean的ID(如class="userAction")。
  2. Spring与Hibernate整合

    • 通过LocalSessionFactoryBean配置Hibernate SessionFactory。
    • 使用@Transactional注解声明式事务管理。
    • 依赖载入

      SSH框架配置文件在哪?路径设置与常见错误详解 第1张

      • Action中载入Service,Service中载入DAO。 // UserAction.java public class UserAction extends ActionSupport { private UserService userService; // Spring载入 // Struts2执行方法 public String login() { userService.checkLogin(username, password); return SUCCESS; } }
      • 常见问题排查:

        1. 事务不生效

          SSH框架配置文件在哪?路径设置与常见错误详解 第2张

          • 检查@Transactional是否添加到Service实现类方法上。
          • 确认<tx:annotation-driven>已启用。
        2. NoSuchBeanDefinitionException

          SSH框架配置文件在哪?路径设置与常见错误详解 第3张

          • 检查Spring扫描路径(<context:component-scan>)是否包含Bean所在包。
          • Hibernate映射错误

            • 确认实体类有@Entity注解,主键有@Id。
            • 检查sessionFactory的packagesToScan路径是否正确。
            • 提示:建议使用Maven管理依赖,确保各库版本兼容(如Struts2.5 + Spring5 + Hibernate5)。

0