文档章节

IDEA用maven整合ssh框架

lcyanxi
 lcyanxi
发布于 2016/09/24 11:58
字数 2209
阅读 345
收藏 1
web.xml配置
<?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"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"   version="3.1">
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <!-- 根据默认配置文件来初始化Spring容器 -->
  <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  <!-- default: /WEB-INF/applicationContext.xml -->
</listener>
  <!-- 配置spring的用于初始化容器对象的监听器-->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:beans.xml</param-value>
  </context-param>
  <!-- 配置请求过滤器,编码格式设为UTF-8,避免中文乱码-->
  <filter>
    <filter-name>encodingFilter</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>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <!-- 配置Session -->
  <filter>
    <filter-name>openSession</filter-name>
    <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
    <!-- 请注意,可能出如下错误Write operations are not allowed in read-only mode (FlushMode.MANUAL): -->
    <init-param>
      <param-name>sessionFactoryBeanName</param-name>
      <param-value>sessionFactory</param-value>
    </init-param>
    <init-param>
      <param-name>singleSession</param-name>
      <param-value>true</param-value>
    </init-param>
    <init-param>
      <param-name> flushMode </param-name>
      <param-value>AUTO </param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>openSession</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <!-- Struts2 核心拦截器配置-->
  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>
sturts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts  PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
       "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
    <constant value="false" name="struts.enable.DynamicMethodInvocation"/>
     <!--配置为开发模式,一旦文件改动,不需要重启,会立即生效 -->
    <constant name="struts.devMode" value="true" />
     <!--把主题配置为simple -->
    <constant name="struts.ui.theme" value="simple" />
    <include file="struts_default.xml"/>
</struts>
struts_default.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts  PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
        "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
    <package name="login" namespace="/"  extends="struts-default"><!--需要将struts-default改为-->
        <action name="book_update" class="com.yanxi.action.BookAction" method="updateBook">
            <result>/jsp/BookEditForm.jsp</result>
        </action>
        <action name="book_save" class="com.yanxi.action.BookAction" method="saveBook">
            <result type="redirect">book_list</result>
        </action>
        <action name="book_list" class="com.yanxi.action.BookAction" method="findBooks">
            <result>/jsp/BookList.jsp</result>
        </action>
        <action name="book_delete" class="com.yanxi.action.BookAction" method="deleteBook">
            <result type="redirect">book_list</result>
        </action>
        <action name="book_edit" class="com.yanxi.action.BookAction" method="editBook">
            <result>/jsp/BookEditForm.jsp</result>
        </action>
    </package>
</struts>
beans.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"
       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">
    <context:annotation-config/>
    <!-- 对包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 -->
    <context:component-scan base-package="com.yanxi"/>
    <!-- 导入外部的properties的文件 -->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <!-- 集成hibernate -->
    <!-- 配置SessionFactory -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <!-- 指定hibernate的配置文件位置 -->
        <property name="configLocation" value="classpath:hibernate.cfg.xml"/>
        <!-- 配置c3p0数据库连接池 -->
        <property name="dataSource">
            <bean class="com.mchange.v2.c3p0.ComboPooledDataSource">
                <!-- 数据连接信息 -->
                <property name="jdbcUrl" value="${jdbc.url}"/>
                <property name="driverClass" value="${jdbc.driverClassName}"/>
                <property name="user" value="${jdbc.username}"/>
                <property name="password" value="${jdbc.password}"/>
                <!-- 其他配置 -->
                <!-- 初始化获取三个连接,取值应该在minPoolSize和maxPoolSize之间 -->
                <property name="initialPoolSize" value="3"/>
                <!-- 连接池中保留的最小连接数 -->
                <property name="maxPoolSize" value="3"/>
                <!-- 连接池中保留的最大连接数 -->
                <property name="minPoolSize" value="15"/>
                <!-- 当连接池中的连接耗尽的时候c3p0一次同时获取的连接数 -->
                <property name="acquireIncrement" value="3"/>
                <!-- 控制数据源加载的数量,若maxStatements和maxStatementsPerConnection均为0,则缓存被关闭 -->
                <property name="maxStatements" value="8"/>
                <!-- maxStatementsPerConnection定义了连接池中的单个连接所拥有的最大缓存statements数。 -->
                <property name="maxStatementsPerConnection" value="5"/>
                <!--最大空闲时间,1800秒未使用则连接被丢弃。若为0则永不丢弃 -->
                <property name="maxIdleTime" value="1800"/>
            </bean>
        </property>
    </bean>
    <!-- 配置事务管理器 -->
    <bean id="hibernateTemplate" class="org.springframework.orm.hibernate4.HibernateTemplate">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
    <!-- 配置Hibernate事务策略 -->
    <bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
    <!-- 声明一个切面 -->
    <tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
            <!--<tx:method name="add*" propagation="REQUIRED"/>-->
            <!--<tx:method name="exits" read-only="true"/>-->
            <tx:method name="*" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>
    <!-- 把切面注入到业务中 -->
    <aop:config>
        <aop:pointcut id="businessService" expression="execution(public * com.yanxi.service..*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="businessService"/>
    </aop:config>
</beans>
jdbc.properties
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/spring?characterEncoding=UTF8
jdbc.username=root
jdbc.password=123
hibernate.cfg.xml
<!DOCTYPE hibernate-configuration PUBLIC
		"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
		"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
	<session-factory>
		<!-- 1,数据库连接信息 -->
		<property name="dialect">
			org.hibernate.dialect.MySQL5InnoDBDialect
		</property>
		<!-- 2,其他配置 -->
		<property name="format_sql">true</property>
		<property name="show_sql">true</property>
		<property name="hbm2ddl.auto">update</property>
		<!-- 3,导入映射文件 -->
		<mapping class="com.yanxi.model.Book"/>
	</session-factory>
</hibernate-configuration>
java 代码

action层

public class BookAction extends ActionSupport{
    private Book book;//接收表单提交的数据
    private List<Book> books;//返回查询的数据带给前端页面显示
    private String id;//删除一本请求时带过来的参数
    private String result;//返回更新操作后的消息
    @Autowired
    private BookService bookService;
    public String saveBook(){
        System.out.println(book.getTitle());
        bookService.saveBook(book);
        return SUCCESS;
    }
    public String findBooks(){
        books= bookService.findAllBooks();
        return SUCCESS;
    }
    public String updateBook(){
        System.out.println("请求是带过来要更改book的id号为:"+book.getId());
        bookService.updateBook(book);
        result="恭喜您更改成功!!!";
        return SUCCESS;
    }
    public String deleteBook(){
        System.out.println("请求是带过来要删除的book的id号为:"+id);
        bookService.deleteBook(Integer.parseInt(id));
        return SUCCESS;
    }
    public String editBook(){
        book=bookService.getBook(Integer.parseInt(id));
        return SUCCESS;
    }
    public Book getBook() {
        return book;
    }

    public void setBook(Book book) {
        this.book = book;
    }

    public List<Book> getBooks() {
        return books;
    }

    public void setBooks(List<Book> books) {
        this.books = books;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getResult() {
        return result;
    }

    public void setResult(String result) {
        this.result = result;
    }
}

service层

public interface BookService {
     public abstract void saveBook(Book book);
     public abstract List<Book> findAllBooks();
     public abstract void updateBook(Book book);
     public abstract boolean deleteBook(int  id);
     public abstract boolean exitBookByName(String name);
     public abstract Book getBook(int id);
}

serviceImpl层

@Component
public class BookServiceImpl implements BookService {
    @Resource
    private BookDao bookDao;
    public void saveBook(Book book) {
        bookDao.save(book);
        System.out.println("a book save success");
    }
    public List<Book> findAllBooks() {
       List<Book> lists=bookDao.findAll("from Book");
        return lists;
    }
    public void updateBook(Book book) {
     
        bookDao.update(book);
    }

    public Book getBook(int id) {
        Book book=bookDao.get(Book.class,id);
        return book;
    }

    public boolean deleteBook(int  id) {
        bookDao.delete(bookDao.get(Book.class,id));
        return true;
    }
    public boolean exitBookByName(String name) {
        return false;
    }
}

dao层

public interface BookDao extends BaseDao {
}

daoImpl层

@Component
public class BookDaoImpl extends BaseDaoImpl implements BookDao {
}

baseDao层

public interface BaseDao {
    public abstract void  save(Object object);
    public abstract <T> List<T>  findAll(String sql);
    public abstract void update(Object object);
    public abstract  boolean delete(Object object);
    public abstract <T> T get(Class<T> tClass,int id);
}
baseDaoImpl层

public class BaseDaoImpl implements BaseDao {
    @Autowired
    private HibernateTemplate hibernateTemplate;

    public void save(Object object) {
        hibernateTemplate.save(object);
    }

    public <T> List<T> findAll(String sql) {
       List<T> list=(List<T>) hibernateTemplate.find(sql);
        System.out.println("find the data:"+list);
        return list;
    }

    public void update(Object object) {
        hibernateTemplate.update(object);

    }

    public boolean delete(Object object) {
        hibernateTemplate.delete(object);
        return true;
    }

    public <T> T get(Class<T> tClass, int id) {
         T t= (T)hibernateTemplate.get(tClass,id);
        return t;
    }
}
jsp页面

index.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page language="java" import="java.util.*" pageEncoding="Utf-8"%>
<html>
<head>
    <title>ssh整合首页</title>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- 新 Bootstrap 核心 CSS 文件 -->
    <style type="text/css">@import url("<c:url value="/css/bootstrap.min.css"/>");</style>
</head>
<body>
<h2>struts2+hibernate+spring 配置成功!</h2>
<hr>
<ul class="list-group">
    <li class="list-group-item">实现对书本的增删改查</li>
    <li class="list-group-item">图书的类型</li>
    <li class="list-group-item">图书的数量</li>
    <li class="list-group-item">
        <span class="badge">添</span>
        <a href="jsp/BookAddForm.jsp" >添加一本新书</a>
    </li>
    <li class="list-group-item">bootstrap真心好用</li>
    <li class="list-group-item">
        <span class="badge">查</span>
       <a href="book_list">查看所有的书籍</a>
    </li>
</ul>
</body>
</html>

BookAddForm.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>Book Add Form</title>
    <style type="text/css">@import url("<c:url value="/css/bootstrap.min.css"/>");</style>
</head>
<body>
<div class="container">
    <h1>SpringMVC 添加书籍</h1>
    <hr>
    <form commandName="book" action="book_save" method="post">
        <div class="form-group">
            <label for="category">Category:</label>
            <select class="form-control" id="category" name="book.category">
                <option>文学</option>
                <option>计算机编程</option>
                <option>健康</option>
                <option>旅行</option>
                <option>科普</option>
                <option>天文学</option>
                <option>医学</option>
                <option>人文杂志</option>
            </select>
        </div>
        <div class="form-group">
            <label for="title">Title:</label>
            <input class="form-control" id="title" name="book.title" placeholder="Enter Title:">
        </div>
        <div class="form-group">
            <label for="author">Author</label>
            <input class="form-control" id="author" name="book.author" placeholder="Enter Author:">
        </div>
        <div class="form-group">
            <label for="isbn">ISBN</label>
            <input class="form-control" id="isbn" name="book.isbn" placeholder="Enter ISBN:">
        </div>
        <div class="form-group">
            <label for="price">Price</label>
            <input class="form-control" id="price" name="book.price" placeholder="Enter Price:">
        </div>
        <div class="form-group">
            <input id="reset" type="reset" tabindex="4" value="reset" class="btn btn-sm btn-success">
            <input id="submit" type="submit" tabindex="5" value="add Book" class="btn btn-sm btn-success">
        </div>
    </form>
</div>
</body>
</html>

BookList.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>Book List</title>
    <style type="text/css">@import url("<c:url value="/css/bootstrap.min.css"/>");</style>
</head>
<body>
<div id="global">
    <h1>Book List</h1><br>
    <a href="jsp/BookAddForm.jsp">Add Book</a>
    <table class="table tavble-bordered table-striped">
        <tr>
            <th>Category</th>
            <th>Title</th>
            <th>ISBN</th>
            <th>Author</th>
            <th>Price</th>
            <th>Operate</th>
        </tr>
        <c:forEach items="${books}" var="book">
            <tr>
                <td>${book.category}</td>
                <td>${book.title}</td>
                <td>${book.isbn}</td>
                <td>${book.author}</td>
                <td>${book.price}</td>
                <td>
                    <a href="#" type="button" class="btn btn-sm btn-info">details</a>
                    <a href="book_edit?id=${book.id}" type="button" class="btn btn-success btn-sm">Edit</a>
                    <a href="book_delete?id=${book.id}" type="button" class="btn btn-sm btn-danger">delete</a>
                </td>
            </tr>
        </c:forEach>
    </table>
</div>
</body>
</html>

BookEditForm.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>Book Edit Form</title>
    <style type="text/css">@import url("<c:url value="/css/bootstrap.min.css"/>");</style>
</head>
<body>
<div id="container">
    <h1>Edit a Book</h1>
    <hr>
    <h3>${result}</h3>
    <form action="book_update" method="post">
        <input type="hidden" name="book.id" value="${book.id}">
        <div class="form-group">
            <label for="category">Category:</label>
            <select class="form-control" id="category" name="book.category">
                <option hidden>${book.category}</option>
                <option>文学</option>
                <option>计算机编程</option>
                <option>健康</option>
                <option>旅行</option>
                <option>科普</option>
                <option>天文学</option>
                <option>医学</option>
                <option>人文杂志</option>
            </select>
        </div>
        <div class="form-group">
            <label for="title">Title:</label>
            <input id="title" class="form-control" name="book.title" value="${book.title}"/>
        </div>
        <div class="form-group">
            <label for="author">Author:</label>
            <input id="author" class="form-control" name="book.author" value="${book.author}"/>
        </div>
        <div class="form-group">
            <label for="isbn">ISBN</label>
            <input class="form-control" id="isbn" name="book.isbn" value="${book.isbn}"/>
        </div>
        <div class="form-group">
            <label for="price">Price</label>
            <input class="form-control" id="price" name="book.price" value="${book.price}"/>
        </div>
        <div class="form-group">
            <input type="reset" class="btn btn-sm btn-success" id="reset" value="重置"/>
            <input type="submit" class="btn btn-sm btn-success" id="submit" value="更改"/>
        </div>

    </form>
</div>
</body>
</html>

效果图 输入local host:8080/springtest02

输入图片说明

输入图片说明

输入图片说明

输入图片说明

© 著作权归作者所有

共有 人打赏支持
lcyanxi
粉丝 7
博文 31
码字总数 15872
作品 0
黔江
程序员
私信 提问
基于 Java 的后台管理系统 - AdminStore

简要介绍 大家回想我们开发的时候搭建项目工程的时候,配置pom,各种拷贝。每次都这样是不是很麻烦。那么我们能不能整理一个基础项目基础模板出来,就这样adminstore诞生了。adminstore整合了...

ada_young
2017/12/27
0
16
10.08-Idea的相关配置

一、Idea导入新项目Maven路径发生改变的问题 1 --> 项目的设置 2 -->全局的设置 在 2 中的Default Setting中设置Maven,就可以避免导入新项目maven的路径发生改变的情况(当然1中部分设置也是...

静以修身2025
03/11
0
0
IntelliJ IDEA 12.0.3 更新版发布

IntelliJ IDEA 12.0.3 更新版发布了,该版本更多还是 bug 的修复,详情请看发行说明。 Android IDEA-95988 (Usability Problem) Export signed APK keyboard usability issue IDEA-98282 (Bu......

YANGL
2013/02/01
4.7K
14
Java 开发必装的 IntelliJ IDEA 插件

IntelliJ IDEA 是目前最好最强最智能的 Java IDE 之一,默认已经集成了几乎所有主流的开发工具和框架。 IDEA 插件简介 常见的IDEA插件主要有如下几类: 常用工具支持 Java日常开发需要接触到...

oschina
2016/01/14
82.1K
36
公司环境搭建(windows10)

安装java运行环境 资源列表 idea webstorm git sourceTree edas alibaba tomcat apache tomcat xshell xftp jdk maven 安装环境变量 安装maven maven 配置文件详见资源列表 安装edas运行环境...

paascloud
2018/01/19
0
0

没有更多内容

加载失败,请刷新页面

加载更多

容器服务

简介 容器服务提供高性能可伸缩的容器应用管理服务,支持用 Docker 和 Kubernetes 进行容器化应用的生命周期管理,提供多种应用发布方式和持续交付能力并支持微服务架构。 产品架构 容器服务...

狼王黄师傅
昨天
3
0
高性能应用缓存设计方案

为什么 不管是刻意或者偶尔看其他大神或者大师在讨论高性能架构时,自己都是认真的去看缓存是怎么用呢?认认真真的看完发现缓存这一块他们说的都是一个WebApp或者服务的缓存结构或者缓存实现...

呼呼南风
昨天
12
0
寻找一种易于理解的一致性算法(扩展版)

摘要 Raft 是一种为了管理复制日志的一致性算法。它提供了和 Paxos 算法相同的功能和性能,但是它的算法结构和 Paxos 不同,使得 Raft 算法更加容易理解并且更容易构建实际的系统。为了提升可...

Tiny熊
昨天
2
0
聊聊GarbageCollectionNotificationInfo

序 本文主要研究一下GarbageCollectionNotificationInfo CompositeData java.management/javax/management/openmbean/CompositeData.java public interface CompositeData { public Co......

go4it
昨天
3
0
阿里云ECS的1M带宽理解

本文就给大家科普下阿里云ECS的固定1M带宽的含义。 “下行带宽”和“上行带宽” 为了更好的理解,需要先给大家解释个词“下行带宽”和“上行带宽”: 下行带宽:粗略的解释就是下载数据的最大...

echojson
昨天
10
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部