這個作為自己學習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,點擊下載
新聞熱點
疑難解答