tomcat+javaWeb+spring的一个都市供求管理系统

  这个作为自己学习javaweb的第一个小项目,也是跟着视频自己学的,是来自java1234的小锋写的,那边有很多java视频可以作为学习参考哦 , 视频中使用的是tomcat作为后端,也( •̀ ω •́ )y使用了     struct和hiberate这两个框架,但是自己对 struct和hiberate不熟悉,所以看完视频直接用spring框架自己写一写, 可以作为学习的参考;

  主要的几个界面:

  最重要的首页

 

信息详细页

 

某类信息列表页

 

信息发布页

 

后台审核页

 

  

  这个是项目的主要结构:

  项目是用myEclipse建立的, 包含webRoot这个视图层,所有的代码在com.nono这个包下,主要的controller全在com.nono.Controller里面; 剩下的是Dao以及封装Dao的Service方法;

  环境为java EE 6 , JDK 1.6, 以及spring commons等一些比较常用的jar包, 想看的话@我:

  数据库表结构如下:

//db_cityinfo 数据库的表, 建表
CREATE DATABASE /*!32312 IF NOT EXISTS*/`db_cityinfo` /*!40100 DEFAULT CHARACTER SET utf8 */;

USE `db_cityinfo`;

//t_info 表信息定义的几个字段
DROP TABLE IF EXISTS `t_info`;

CREATE TABLE `t_info` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `typeId` int(11) DEFAULT NULL,
  `title` varchar(40) DEFAULT NULL,
  `content` text,
  `linkman` varchar(20) DEFAULT NULL,
  `phone` varchar(20) DEFAULT NULL,
  `email` varchar(20) DEFAULT NULL,
  `infoDate` datetime DEFAULT NULL,
  `state` int(11) DEFAULT NULL,
  `payfor` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=54 DEFAULT CHARSET=utf8;

//t_infotype 表类型定义的几个字段
DROP TABLE IF EXISTS `t_infotype`;

CREATE TABLE `t_infotype` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `typeSign` int(11) DEFAULT NULL,
  `typeName` varchar(20) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8;

//t_user 用户表;
DROP TABLE IF EXISTS `t_user`;

CREATE TABLE `t_user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `userName` varchar(20) DEFAULT NULL,
  `password` varchar(20) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;

  数据库就是数据持久层了, 我们用到也就是CRUD(增删改查), 不需要懂太多;

  相关的路由数据库操作全在com.nono这个包下;

  javaweb的web.xml配置如下,辛亏有了spring这东东的依赖注入, 通过配置xml的自动注入就可以完成以前一堆servletMapping的配置,这个很重要:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
    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_3_0.xsd">
  <display-name></display-name>    
  <!---默认的访问的地址--->
  <welcome-file-list>
    <welcome-file>index.htm</welcome-file>
  </welcome-file-list>
  
  <servlet>
    <servlet-name>test</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
    </servlet>
    <!---所有请求.htm为后缀的文件都通过servlet路由--->
    <servlet-mapping>
    <servlet-name>test</servlet-name>
    <url-pattern>*.htm</url-pattern>
    </servlet-mapping>

    <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!---这个指向的是spring的xml配置--->
    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
    /WEB-INF/test-servlet.xml
    </param-value>
    </context-param>
</web-app>

  这个是test-servlet.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:p="http://www.springframework.org/schema/p"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:aop="http://www.springframework.org/schema/aop"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xmlns:mvc="http://www.springframework.org/schema/mvc"
 xmlns:task="http://www.springframework.org/schema/task"
 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
      http://www.springframework.org/schema/tx
      http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
      http://www.springframework.org/schema/aop
      http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
      http://www.springframework.org/schema/mvc
      http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
      http://www.springframework.org/schema/task
      http://www.springframework.org/schema/task/spring-task-3.0.xsd">
    
  //让spring自动扫描com.nono下的所有的包
    <context:annotation-config> </context:annotation-config>
    <context:component-scan base-package="com.nono" > </context:component-scan>
    
  //这个是mysql的bean配置, 这个我是配置成自己的本地的数据库, 连接的数据库为test,账号为root, 密码是6个1; <bean name="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://127.0.0.1:3306/test" /> <property name="username" value="root" /> <property name="password" value="111111" /> </bean>
  //我们要把配置dataSource再放到jdbcTemplate里面, 按照正常的逻辑要这样 <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" abstract="false" lazy-init="false" autowire="default"> <!-- 把这个bean传进去 --> <property name="dataSource" ref="dataSource"> </property> </bean>
  //我们生成了一个叫做jdbcDao的bean, 在com.nono包下要用jdbcDao的话直接在声明前面添加@Autowired, 那么这个bean就会被自动注入进来, 不懂的话可以去看下servlet的bean是什么; <bean id="jdbcDao" class="com.nono.Dao.JdbcDao"> <property name="jdbcTemplate" ref="jdbcTemplate"></property> </bean>
  //这个也是配置, 配置在Controller中返回的ModelAndView字符串全部加上.jsp作为结尾, 是为了方便; <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="suffix"> <value>.jsp</value> </property> </bean> </beans>

  webRoot文件夹下的代码和src文件夹下的代码地址已传到blog,点击下载 

原文地址:https://www.cnblogs.com/diligenceday/p/4377624.html