如何搭建ssh项目(基于注解)

本文详细介绍如何使用Struts、Hibernate和Spring(SSH)框架搭建JavaEE Web应用程序,包括配置web.xml、db.properties、applicationContext.xml、struts.xml等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在JavaEE开发中会使用到struts+hibernate+spring框架组合起来搭建web应用程序,在这里讲一下如何搭建ssh项目

第一步,下载jar包,strutshibernatespring

第二步,分别将以上的jar包及mysql驱动jar包及c3p0jar包放到lib目录下,并build path

第三步,配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <display-name>Struts Blank</display-name>

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

    <!-- needed for ContextLoaderListener -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>

    <!-- Bootstraps the root web application context before servlet initialization -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

</web-app>

第四步,配置db.properties

user=root
password=
jdbcUrl=jdbc:mysql:///ssh
driverClass=com.mysql.jdbc.Driver

第五步,新建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"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:p="http://www.springframework.org/schema/p"
    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-4.1.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">
    <!-- 导入资源文件 -->
    <context:property-placeholder location="classpath:db.properties"/>
    <!-- 配置数据源 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
    p:user="${user}" p:password="${password}" p:jdbcUrl="${jdbcUrl}" 
    p:driverClass="${driverClass}"/>

    <!-- 配置sessionFactory -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="hibernateProperties">
            <props>
                <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">
            <list>
                <value>com.dobeyue.ssh.entities</value>
            </list>
        </property>
    </bean>

    <!-- 配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

    <!-- 使用注解配置事务 -->
    <tx:annotation-driven transaction-manager="transactionManager"/>

    <!-- 配置切面 -->
    <aop:aspectj-autoproxy/>

    <!-- 配置扫描的包 -->
    <context:component-scan base-package="com.dobeyue.ssh"/>

</beans>

第六步,新建struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
    <package name="default" namespace="/" extends="struts-default">

    </package>

</struts>

第七步,新建包com.dobeyue.ssh.entities,新建User 类文件

package com.dobeyue.ssh.entities;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

import org.springframework.stereotype.Component;
@Component
@Table(name="users")
@Entity
public class User {
    private Integer id;
    private String userName;
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Id
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    @Column(name="userName",columnDefinition="varchar(255) default 'dobeyue'")
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
}

最后,运行该应用程序,在数据库中生成users表,即为成功

一、项目简介 本项目是一套基于SSH的旅游网站,主要针对计算机相关专业的正在做毕设的学生和需要项目实战练习的Java学习者。 包含:项目源码、数据库脚本、软件工具、项目说明等,该项目可以直接作为毕设使用。 项目都经过严格调试,确保可以运行! 二、技术实现 ​后台框架:SpringStruts2、Hibernate ​数据库:MySQL 开发环境:JDK、Eclipse、Tomcat 三、系统功能 本旅游网站以旅游攻略发布、旅游线路发布、酒店预订、美食预订、景区图片和景区订票等功能为核心,其宗旨是及时、准确、完整发布游客所需要的相关信息,同时给游客带来方便和给公司创造利益。 它的主要功能如下:  一、旅游线路信息发布以及预定功能:顾客可通网站查看相应的旅游线路信息和预定旅游景点门票功能,管理员可以在后台对旅游线路进行发布、更新,对于线路预定可以进行相应的处理。  二、酒店查询、预订功能:顾客可以通过网站查询酒店情况,同时还可以对合适的酒店进行预订。   三、旅游景点介绍功能:顾客在网站可以查看景区主要旅游景区和景点介绍信息,管理员可以在后台对旅游景点信息进行更新、修改和删除。  四、旅游攻略发布功能:顾客可以通过旅游网站查看景区最新的旅游资讯和旅游动态,网站管理员可以在后台发布、更新和删除旅游新闻。  五、旅游风景图片发布功能:游客可以通过网站查看景区最新的风光图片,可以点击小图看大图,图片按照相应的旅游景点分类,管理员可以上传和修改、删除图片信息。  六、旅游服务在线留言功能:游客可以通过网站查看一些景区旅游的注意事项和在景区旅游经常遇到的一些问题,管理员在后台可以对信息进行更新和修改、删除功能。 该系统功能完善、界面美观、操作简单、功能齐全、管理便捷,具有很高的实际应用价值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值