美文网首页
编写web项目的基本流程

编写web项目的基本流程

作者: cc7f78569e0d | 来源:发表于2019-04-28 19:51 被阅读0次

(利用spring+springMVC+hibernate三大框架的项目整合步骤)
第一步:首先下载一个java编写工具(eclipse和myeclipse都可以)
第二步:在myeclise工程区右击创建一个web项目工程包(我用的是myeclipse)


第三步:导入工程所需jar包,jar包统一放入WEB-INF/bin目录下

第四步:创建工程类包(bean、dao、daoimpl、service、serviceimpl、filter、tools、controller、hbm)

第五步:在WEB-INF目录下创建spring配置存放文件包和jsp存放包


第六步:创建或导入spring和springMVC的xml配置文件,然后在web中配置好spring环境

aplicationContext.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">
    <context:component-scan base-package="com.waorkit.*"></context:component-scan>
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <!-- 连接数据库 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="${jdbc.driverClassName}"></property>
        <property name="url" value="${jdbc.url}"></property>
        <property name="username" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>
    
    <!-- 创建连接数据库会话工厂 -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource">    <!-- 连接数据库 -->
            <ref bean="dataSource" />
        </property>
        <property name="hibernateProperties">   
            <props>
                <prop key="hibernate.dialect">  <!-- 方言 -->
                    org.hibernate.dialect.Oracle9Dialect
                </prop>
                   <!--<prop key="hibernate.show_sql">true </prop>
                <prop key="hibernate.format_sql">true</prop>
                -->
                <prop key="hibernate.hbm2ddl.auto">none</prop>
                <!-- 获取当前session并开启事物和关闭资源 -->
                <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate3.SpringSessionContext</prop>
            </props>
        </property>
        <property name="mappingDirectoryLocations"> <!-- 映射 -->
            <list>
                <value>classpath:hbm</value>
            </list>
        </property>
    </bean>
</beans>

applictionContext-MVC.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:mvc="http://www.springframework.org/schema/mvc"   
    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.2.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-2.5.xsd
    http://www.springframework.org/schema/mvc  
    http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
    <!-- 注解扫描 -->
    <context:component-scan base-package="com.workit.*"></context:component-scan>
    <!-- 默认的注解映射支持(所有mvc传值都是经过这里) -->
    <mvc:annotation-driven/>
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/views/"></property>     <!-- 前缀(在这个目录下去找与传过来的值相匹配的jsp文件) -->
            <property name="suffix" value=".jsp"></property>                <!-- 后缀 -->
            <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property><!-- 显示标签库 -->
        </bean>
</beans>

web.xml配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <!-- 配置spring -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  <!-- 配置spring监听 -->
  <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>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:conf/applicationContext-MVC.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <!-- 配置springMVC映射 -->
  <servlet-mapping>
    <servlet-name>springMVC</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
</web-app>

配好之后这个项目的基本架子就搭建起来了,接下来就是往过程包中写各种类了。
(仅限自己参考,哈哈)

相关文章

网友评论

      本文标题:编写web项目的基本流程

      本文链接:https://www.haomeiwen.com/subject/gehcnqtx.html