• maven 创建Java web项目


    第一步创建项目

     然后 切换到Navigator视图 (在window--> Show View 下可以找到)

    在 setings 下面设置项目的一些属性 (主要是jdk编译版本 web版本)

    右键properties检查下Web Deployment Assembly设置有没有问题

    如果WEB-INF下面缺少lib文件 新建一个并 右键Build Path

    编写 pom.xml  加载jar包 根据项目需求 右键 Run as-> maven-installer

     注意(如果配置完運行Tomcat一直報錯,配置文件檢查后沒問題,可能是jar包衝突)

    
    

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>ben</groupId>
    <artifactId>sshDemo</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>sshDemo Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <properties>
    <spring.version>5.0.4.RELEASE</spring.version>
    <aspectj.version>1.8.10</aspectj.version>
    <cglib.version>3.2.5</cglib.version>
    <hibernate.version>5.2.16.Final</hibernate.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
    <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
    </dependency>
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.0.4.RELEASE</version>
    </dependency>

    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>${spring.version}</version>
    </dependency>
    <!-- aop -->
    <dependency>
    <groupId>aopalliance</groupId>
    <artifactId>aopalliance</artifactId>
    <version>1.0</version>
    </dependency>
    <dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjrt</artifactId>
    <version>${aspectj.version}</version>
    </dependency>
    <dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>${aspectj.version}</version>
    </dependency>

    <dependency>
    <groupId>cglib</groupId>
    <artifactId>cglib-nodep</artifactId>
    <version>${cglib.version}</version>
    </dependency>
    <!-- hibernate 相关包 -->
    <dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>5.2.16.Final</version>
    </dependency>
    <dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-c3p0</artifactId>
    <version>5.2.16.Final</version>
    </dependency>
    <!-- spring orm 整合ORM框架用的-->
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-orm -->
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-orm</artifactId>
    <version>5.0.4.RELEASE</version>
    </dependency>

    <!-- <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-ehcache</artifactId>
    <version>5.2.16.Final</version> </dependency> -->

    <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.46</version>
    </dependency>

    <dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.7</version>
    </dependency>

    <dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.7.7</version>
    <scope>test</scope>
    </dependency>
    <dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
    </dependency>

    </dependencies>

    <build>
    <finalName>sshDemo</finalName>

    </build>
    </project>

     

     配置Spring  ApplicationContext.xml 整合了hibernate 

    這2個是整合用到的類
    sessionFactory : org.springframework.orm.hibernate5.LocalSessionFactoryBean

    dateSource (c3p0) : ComboPooledDataSource

    <?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:aop="http://www.springframework.org/schema/aop"
        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/aop
            http://www.springframework.org/schema/aop/spring-aop.xsd">
    
    
        <!-- 读取配置文件 -->
        <context:property-placeholder
            ignore-unresolvable="true" location="classpath*:/applicationContext.properties" />
    
        <!-- 自动扫描包下面 类 属性 方法 上的注解 -->
        <context:component-scan base-package="com.sshDemo"></context:component-scan>
    
        <!-- 开启aop操作 -->
        <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
    
        <bean id="sessionFactory"
            class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
            <property name="dataSource" ref="dataSource"></property>
            <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.connection.driver_class">${hibernate.connection.driver_class}</prop>
                    <prop key="hibernate.connection.url">${hibernate.connection.url}</prop>
                    <prop key="hibernate.connection.username">${hibernate.connection.username}</prop>
                    <prop key="hibernate.connection.password">${hibernate.connection.password}</prop>
                    <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
                    <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                    <prop key="hibernate.current_session_context_class">${hibernate.current_session_context_class}</prop>
                    <prop key="hibernate.connection.provider_class">${hibernate.connection.provider_class}</prop>
                </props>
            </property>
        </bean>
        <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <property name="minPoolSize" value="10" />
            <property name="maxPoolSize" value="100" />
            <!-- 最大空闲时间,超过该时间则连接被丢弃。若为0则永不丢弃。Default: 0 -->
            <property name="maxIdleTime" value="1800" />
            <!-- 当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。 -->
            <property name="acquireIncrement" value="3" />
            <!-- JDBC的标准参数,用以控制数据源内加载的PreparedStatements数量。但由于预缓存的statements属于单个connection而不是整个连接池。所以设置这个参数需要考虑到多方面的因素。如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。 -->
            <property name="maxStatements" value="100" />
            <property name="initialPoolSize" value="10" />
            <!-- 每隔多少秒检查所有连接池中的空闲连接。 -->
            <property name="idleConnectionTestPeriod" value="120" />
        </bean>
    </beans>

     applicationContext.properties

    ## MySQL
    
    hibernate.connection.driver_class=com.mysql.jdbc.Driver
    hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
    hibernate.connection.driver_class=com.mysql.jdbc.Driver
    hibernate.connection.url=jdbc:mysql://127.0.0.1:3306/heartstore
    hibernate.connection.username=root
    hibernate.connection.password=root
    
    ##Base
    
    hibernate.current_session_context_class=thread
    hibernate.connection.provider_class=org.hibernate.connection.C3P0ConnectionProvider
    hibernate.show_sql=true
    hibernate.format_sql true
    
    
    
    
    ## Oracle
    
    #hibernate.dialect org.hibernate.dialect.Oracle8iDialect
    #hibernate.dialect org.hibernate.dialect.Oracle9iDialect
    #hibernate.dialect org.hibernate.dialect.Oracle10gDialect
    #hibernate.connection.driver_class oracle.jdbc.driver.OracleDriver
    #hibernate.connection.username ora
    #hibernate.connection.password ora
    #hibernate.connection.url jdbc:oracle:thin:@localhost:1521:orcl
    #hibernate.connection.url jdbc:oracle:thin:@localhost:1522:XE
    
    
    ## PostgreSQL
    
    #hibernate.dialect org.hibernate.dialect.PostgreSQLDialect
    #hibernate.connection.driver_class org.postgresql.Driver
    #hibernate.connection.url jdbc:postgresql:template1
    #hibernate.connection.username pg
    #hibernate.connection.password
    

    配置 web.xml 整合了SpringMVC

    <?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
    http://www.springmodules.org/schema/cache/springmodules-cache.xsd
    http://www.springmodules.org/schema/cache/springmodules-ehcache.xsd"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
    http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    version="3.1"
    metadata-complete="true">
    <display-name>Archetype Created Web Application</display-name>
    <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>
    <!-- springMVC -->
    <servlet>
    <servlet-name>springMVC</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
    <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:/springMVC.xml</param-value>
    </init-param>
    </servlet>
    <servlet-mapping>
    <servlet-name>springMVC</servlet-name>

    <url-pattern>/</url-pattern>
    </servlet-mapping>
    </web-app>

    sprinMVC.xml

    <?xml version="1.1" 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:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"
        xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
        
        
        <context:component-scan base-package="com.sshDemo"/>
        <!-- 適配器注解驅動 -->
        <mvc:annotation-driven></mvc:annotation-driven>
        <!-- 定义JSP文件的位置 -->
        <bean
            class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/view/" />
            <property name="suffix" value=".jsp" />
        </bean>
        <!-- ... -->
    
    </beans>

    運行試下 (需要設置TOMCAT運行配置)

     

  • 相关阅读:
    3.17JSP作业
    JSP第二次作业
    JSP第一次作业
    软件测试课堂练习
    增删改查
    登录界面
    购物商城
    图床
    JSP-2020年4月14日-第七周
    JSP-2020年4月12日-第六周
  • 原文地址:https://www.cnblogs.com/temporary/p/8725990.html
Copyright © 2020-2023  润新知