SSH整合之入門級例子:簡易留言板(完整原始碼)
一.環境說明
- ※Windows 10 ver 1703
- ※IDEA 2017 2.5
- ※JDK 1.8
- ※MySQL 5.7
- ※Maven 3.5
二.框架說明
- ※Spring 4.3.11
- ※SpringMVC 4.3.11
- ※Hibernate 5.2.11
- ※Bootstrap 3.3.7
- ※Flat UI 2.3
三.專案說明
利用SSH三大框架,實現一個留言頁面,可以展示留言以及新增留言,基於註解.
四.原始碼
專案結構:
實體類:
Msg.class
package com.cs.qsx.domain;
import com.alibaba.fastjson.annotation.JSONField;
import javax.persistence.*;
import java.util.Date;
@Entity
@Table(name = "tb_msg")
public class Msg {
@Id //主鍵
@GeneratedValue(strategy = GenerationType.IDENTITY) //自增
private Integer id;
@Column(length = 10)
private String name;
@Column(length = 100)
private String context;
@Column (columnDefinition = "DATE") //指定屬性在表中的欄位型別
@JSONField(format = "yyyy-MM-dd") //指定json格式化時如何格式化date
private Date createDate;
public Msg() {
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName () {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getContext() {
return context;
}
public void setContext(String context) {
this.context = context;
}
public Date getCreateDate() {
return createDate;
}
public void setCreateDate(Date createDate) {
this.createDate = createDate;
}
}
資料庫連線配置
dbconfig.properties
jdbc.driverclass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/Msg_Test?characterEncoding=UTF-8
jdbc.username=root
jdbc.password=123456
Spring配置檔案
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<!--Spring的約束檔案 使用那些模組就標記那些模組的約束文件 -->
<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" xmlns:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd ">
<!--1、載入外部配置檔案 -->
<context:property-placeholder location="classpath:dbconfig.properties" />
<context:component-scan base-package="com.cs.qsx.dao"></context:component-scan>
<context:component-scan base-package="com.cs.qsx.service"></context:component-scan>
<!--2、配置資料庫連線池 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<!-- 基本屬性 url、user、password -->
<property name="driverClassName" value="${jdbc.driverclass}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!-- 配置監控統計攔截的filters -->
<property name="filters" value="stat" />
<!-- 配置初始化大小、最小、最大 -->
<property name="maxActive" value="20" />
<property name="initialSize" value="1" />
<property name="minIdle" value="1" />
<!-- 配置獲取連線等待超時的時間 -->
<property name="maxWait" value="60000" />
<!-- 配置間隔多久才進行一次檢測,檢測需要關閉的空閒連線,單位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<!-- 配置一個連線在池中最小生存的時間,單位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="300000" />
<property name="testWhileIdle" value="true" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" />
<!-- 開啟PSCache,並且指定每個連線上PSCache的大小 -->
<property name="poolPreparedStatements" value="true" />
<property name="maxOpenPreparedStatements" value="20" />
</bean>
<!-- 3、配置SessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!--配置資料庫連線池 -->
<property name="dataSource" ref="dataSource"></property>
<!--Hibernate的設定資訊 -->
<property name="hibernateProperties">
<props>
<!--方言 -->
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL57Dialect</prop>
<!--顯示SQL -->
<prop key="hibernate.show_sql">false</prop>
<!--格式化sql語句 -->
<prop key="hibernate.format_sql">false</prop>
<!--ddl自動語句 -->
<prop key="hibernate.hbm2ddl.auto">update</prop>
<!-- <prop key="hibernate.current_session_context_class">thread</prop> -->
<!-- 最好使用Spring中對SessionContext的子類 -->
<prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate5.SpringSessionContext
</prop>
</props>
</property>
<!--掃描Hibernate註解的實體類包 -->
<property name="packagesToScan">
<value>com.cs.qsx.domain</value>
</property>
</bean>
<!--4、配置事務管理物件 -->
<bean id="txManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!--配置AOP註解 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<!--設定註解事務的管理物件 -->
<tx:annotation-driven transaction-manager="txManager"></tx:annotation-driven>
</beans>
SpringMVC配置檔案
springmvc.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.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<mvc:annotation-driven>
<mvc:message-converters register-defaults="false">
<!--解決@Responcebody中文亂碼問題 -->
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<constructor-arg value="UTF-8"></constructor-arg>
</bean>
<!--配合fastjson支援 -->
<bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<property name="defaultCharset" value="UTF-8"></property>
<property name="supportedMediaTypes">
<list>
<value>application/json</value>
<value>text/html;charset=UTF-8</value>
</list>
</property>
<property name="fastJsonConfig" ref="fastJsonConfig"></property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<!--fastJsonConfig -->
<bean id="fastJsonConfig" class="com.alibaba.fastjson.support.config.FastJsonConfig">
<!--預設編碼格式 -->
<property name="charset" value="UTF-8"></property>
<!--預設日期格式化格式 -->
<property name="dateFormat" value="yyyy-MM-dd"></property>
<property name="serializerFeatures">
<list>
<value>WriteNullListAsEmpty</value>
</list>
</property>
</bean>
<!--fastjson支援配置結束 -->
<!--放行靜態資源 -->
<mvc:default-servlet-handler></mvc:default-servlet-handler>
<!--配置掃描包 -->
<context:component-scan base-package="com.cs.qsx.controller"></context:component-scan>
<!--檢視解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 上傳檔案大小上限,單位為位元組(10MB) -->
<property name="maxUploadSize">
<value>10485760</value>
</property>
<!-- 請求的編碼格式,必須和jSP的pageEncoding屬性一致,以便正確讀取表單的內容,預設為ISO-8859-1 -->
<property name="defaultEncoding">
<value>UTF-8</value>
</property>
</bean>
</beans>
專案配置檔案
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<!--配置Spring -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext*.xml</param-value>
</context-param>
<!--配置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>/WEB-INF/springmvc-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--配置過濾器,解決post請求亂碼 -->
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
Maven依賴
pom.xml
<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>com.cs.qsx</groupId>
<artifactId>SSH_Msg</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>SSH_Msg Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.3.11.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-orm -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.3.11.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.11.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.11.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.44</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet.jsp/javax.servlet.jsp-api -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.1</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.38</version>
</dependency>
</dependencies>
<build>
<finalName>SSH_Msg</finalName>
<!--提高編譯級別 -->
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
頁面
leaveMessage.jsp
<%--
Created by IntelliJ IDEA.
User: 17535
Date: 2017/10/11
Time: 8:31
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>留言板</title>
<link href="/css/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
<link href="/css/flat-ui.min.css" rel="stylesheet">
<script src="https://cdn.bootcss.com/jquery/3.1.1/jquery.min.js"></script>
<script src="/js/flat-ui.min.js"></script>
<script type="text/javascript">
function getMsg() {
var url = "${pageContext.request.contextPath}/api/msg";
$.ajax({
url:url,
type:"post",
dataType:"json",
success:function (result) {
var html1 = "<thead><tr><th>留言內容</th><th>留言人</th><th>留言時間</th></tr></thead>";
var innHtml = "";
for (var i=0;i<result.length;i++){
var jsonObject = result[i];
innHtml = innHtml + "<tr><td>"+jsonObject.context+"</td><td>"+jsonObject.name+"</td><td>"+jsonObject.createDate+"</td></tr>";
}
$("#msgtable").html(html1 + innHtml);
},
error:function () {
alert("請求失敗");
}
});
}
</script>
</head>
<body onload="getMsg()">
<div class="main">
<h2>請輸入留言</h2>
<div class="addMsg">
<form action="/addMsg" method="post" class="navbar-form navbar-left">
<div class="form-group">
<input class="form-control" type="text" name="context" placeholder="請輸入留言" />
<input class="form-control" type="text" name="name" placeholder="請輸入姓名" />
<button class="btn btn-primary btn-small" type="submit">留言</button>
<button class="btn btn-warning btn-small" type="reset">清空</button>
</div>
</form>
</div>
<div class="show">
<table class="table table-bordered table-hover" id="msgtable">
</table>
</div>
</div>
</body>
</html>
業務邏輯層(介面)
MsgService.class
package com.cs.qsx.service;
import com.cs.qsx.domain.Msg;
import java.util.List;
public interface MsgService {
//查詢
public List<Msg> findAll();
//新增
public void addMsg(Msg msg);
}
業務邏輯層(實現類)
MsgServiceImpl.class
package com.cs.qsx.service.impl;
import com.cs.qsx.dao.MsgDao;
import com.cs.qsx.domain.Msg;
import com.cs.qsx.service.MsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class MsgServiceImpl implements MsgService {
@Autowired
private MsgDao msgDao;
@Override
public List<Msg> findAll() {
return msgDao.queryAll();
}
@Override
public void addMsg(Msg msg) {
msgDao.insertMsg(msg);
}
}
資料持久層(介面)
MsgDao.class
package com.cs.qsx.dao;
import com.cs.qsx.domain.Msg;
import java.util.List;
public interface MsgDao {
//查詢
public List<Msg> queryAll();
//新增
public void insertMsg(Msg msg);
}
資料持久層(實現類)
MsgDaoImpl.class
package com.cs.qsx.dao.impl;
import com.cs.qsx.dao.MsgDao;
import com.cs.qsx.domain.Msg;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import javax.annotation.PostConstruct;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
@Repository("msgDao")
public class MsgDaoImpl extends HibernateDaoSupport implements MsgDao {
@Autowired
private SessionFactory sessionFactory;
@PostConstruct
protected void injectSessionFactory() {
super.setSessionFactory(sessionFactory);
}
@Override
public List<Msg> queryAll() {
return (List<Msg>) getHibernateTemplate().find("from Msg");
}
@Override
public void insertMsg(Msg msg) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
try {
date = simpleDateFormat.parse(simpleDateFormat.format(new Date()));
} catch (ParseException e) {
e.printStackTrace();
}
msg.setCreateDate(date);
getHibernateTemplate().save(msg);
}
}
控制器
MsgController.class
package com.cs.qsx.controller;
import com.cs.qsx.domain.Msg;
import com.cs.qsx.service.MsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http
相關推薦
SSH整合之入門級例子:簡易留言板(完整原始碼)
一.環境說明
※Windows 10 ver 1703
※IDEA 2017 2.5
※JDK 1.8
※MySQL 5.7
※Maven 3.5
二.框架說明
※Spring 4.3.11
※SpringMVC 4.3.11
※Hibernat
mybatis 不整合spring 入門小例子
ole batis system [] commit print 管理 args private 先上一個搭建完的項目結構截圖:
相對比較重要的配置文件有 db.properties , SqlMappingConfig.xml , mapper/User.xm
暑假專案天地圖SSH整合之架構簡介
(一)SSH簡介
首先,SSH不是一個框架,而是多個框架(struts+spring+hibernate)的整合,是目前較流行的一種Web應用程式開源整合框架,用於構建靈活、易於擴充套件的多
java實現遞迴函式入門級例子:用遞迴函式求一個數組中的最大值
我們開始把陣列分為兩半,分別找出最大值,那麼這個最大值就是最後的最大值:同時我們左右兩邊繼續細分,停止條件就是細分到單個數值為止。
package chapter1;
//使用遞迴求出一個數組中的最小值
public class FindMax {
public sta
Jenkins學習筆記 ---Jenkins持續整合之入門篇
1.1 編寫目的
編寫本文件,旨在與大家交流經驗、分享成果。該文件主要說明了如何結合安裝配置Tomcat、JDK、Ant、Subversion等工具搭建基於Jenkins的持續整合環境。
編者按:文件涉及到Jenkins的安裝配置,但並未涵蓋Jenki
三大框架(ssh)整合之配置檔案
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-
SSH整合之Spring版本與jdk版本問題
異常如下:二月 09, 2018 3:56:00 下午 org.apache.catalina.core.StandardContext listenerStart
嚴重: Exception sending context initialized event to list
ssh整合之三(實現邏輯)
1、基本屬性
private User user;//用於實現save
private Long id;//用於實現刪、改、查(findById()的情況)
2、CRUD模板
public Str
ssh整合之struts.xml
開啟struts2.core核心包,找到struts-2.3.dtd複製一段宣告就可以了<!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN""ht
ssh整合之HibernateDaoSupport的使用
繼承HibernateDaoSupport有三種方式:
1.
Java程式碼
@Component("logDAO")
public class LogDAOImpl extends HibernateDaoSupport implements Lo
SSH整合之配置檔案
來自:http://www.cnblogs.com/luoyun/archive/2013/01/03/2843322.html
為web應用提供Struts2支援
1.編輯web應用的web.xml配置檔案,配置Struts2的核心Filter來攔截使用者請求。
ssh整合之最後一步擴大session的作用範圍
在web.xml中<!-- 擴大session作用範圍 注意: 任何filter一定要在struts的filter之前呼叫 --> <filter> <filter-name>openSessionInView</filt
SSH整合之個人部落格專案開發--知識點總結
有簡單的資料壓縮,jqery和js非同步重新整理等..在我的後臺管理系統中,為了減省事,我只用了一個頁面,但是檢視記錄上我目前只能使用ajax非同步刷新發送一個請求,獲取一個json物件,然後遍歷json物件獲取到我需要的/**
* 文章操作
* author:盼
SSH整合之Struts.xml 常用的引數
Struts.xml常用的引數1、<include> 利用include標籤,可以將一個struts.xml配置檔案分割成多個配置檔案,然後在struts.xml中使用<include>標籤引入其他配置檔案。比如一個購物程式,可以把使用者配置、商品配
Flask學習之旅--簡易留言板
一、寫在前面
正所謂“紙上得來終覺淺,方知此事要躬行”,在看文件和視訊之餘,我覺得還是要動手做點什麼東西才能更好地學習吧,畢竟有些東西光看文件真的難以理解,於是就試著使用Flask框架做了一個簡易留言板,實現了註冊、登入和留言的功能。
二、開發環境
系統:
jsp做的一個簡易留言板功能
cti var llb onload 計算 bst image message html 1.先登錄才能留言
2.可以留言
3.可以回復
4.自己寫的留言可以自己刪除
5.留言進行分頁顯示,避免留言只能顯示一部分
1.登錄後會有
2.寫留言
提交按鈕在你沒寫東西前是
微信小程序實現簡易留言板
fault .com icon index 們的 聽說 裏的 暫時 學習 微信小程序現在很火,於是也就玩玩,做了一個簡易的留言板,讓大家看看,你們會說no picture you say a j8 a,好吧先上圖。
樣子就是的,功能一目了然,下面我們就貼實現的代碼,首先是H
vue學習筆記第一天-vue.js簡易留言板
fad ima con targe right 彈出框 n) ade ack
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
&l
js簡易留言板
() != date() turn eth create rip eply doc
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
js 實現簡易留言板功能
con wid NPU children 標準 element 出錯 hdel focus
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xh